Skip to content

Commit e9eb54c

Browse files
Fix rewrite_root when there's only 1 replacement
1 parent 246c66a commit e9eb54c

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

crates/ra_syntax/src/algo.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,11 @@ impl<'a> SyntaxRewriter<'a> {
290290
N::cast(self.rewrite(node.syntax())).unwrap()
291291
}
292292

293+
/// Returns a node that encompasses all replacements to be done by this rewriter.
294+
///
295+
/// Passing the returned node to `rewrite` will apply all replacements queued up in `self`.
296+
///
297+
/// Returns `None` when there are no replacements.
293298
pub fn rewrite_root(&self) -> Option<SyntaxNode> {
294299
assert!(self.f.is_none());
295300
self.replacements
@@ -298,6 +303,9 @@ impl<'a> SyntaxRewriter<'a> {
298303
SyntaxElement::Node(it) => it.clone(),
299304
SyntaxElement::Token(it) => it.parent(),
300305
})
306+
// If we only have one replacement, we must return its parent node, since `rewrite` does
307+
// not replace the node passed to it.
308+
.map(|it| it.parent().unwrap_or(it))
301309
.fold1(|a, b| least_common_ancestor(&a, &b).unwrap())
302310
}
303311

0 commit comments

Comments
 (0)