From e9eb54c617f3712e3affaa9ab56bc9bfb21bf728 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Sat, 13 Jun 2020 18:55:10 +0200 Subject: Fix `rewrite_root` when there's only 1 replacement --- crates/ra_syntax/src/algo.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/crates/ra_syntax/src/algo.rs b/crates/ra_syntax/src/algo.rs index 664894d1f..f7a885eb3 100644 --- a/crates/ra_syntax/src/algo.rs +++ b/crates/ra_syntax/src/algo.rs @@ -290,6 +290,11 @@ impl<'a> SyntaxRewriter<'a> { N::cast(self.rewrite(node.syntax())).unwrap() } + /// Returns a node that encompasses all replacements to be done by this rewriter. + /// + /// Passing the returned node to `rewrite` will apply all replacements queued up in `self`. + /// + /// Returns `None` when there are no replacements. pub fn rewrite_root(&self) -> Option { assert!(self.f.is_none()); self.replacements @@ -298,6 +303,9 @@ impl<'a> SyntaxRewriter<'a> { SyntaxElement::Node(it) => it.clone(), SyntaxElement::Token(it) => it.parent(), }) + // If we only have one replacement, we must return its parent node, since `rewrite` does + // not replace the node passed to it. + .map(|it| it.parent().unwrap_or(it)) .fold1(|a, b| least_common_ancestor(&a, &b).unwrap()) } -- cgit v1.2.3