mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-29 05:15:04 +00:00
Fix rewrite_root
when there's only 1 replacement
This commit is contained in:
parent
246c66a7f7
commit
e9eb54c617
1 changed files with 8 additions and 0 deletions
|
@ -290,6 +290,11 @@ impl<'a> SyntaxRewriter<'a> {
|
||||||
N::cast(self.rewrite(node.syntax())).unwrap()
|
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<SyntaxNode> {
|
pub fn rewrite_root(&self) -> Option<SyntaxNode> {
|
||||||
assert!(self.f.is_none());
|
assert!(self.f.is_none());
|
||||||
self.replacements
|
self.replacements
|
||||||
|
@ -298,6 +303,9 @@ impl<'a> SyntaxRewriter<'a> {
|
||||||
SyntaxElement::Node(it) => it.clone(),
|
SyntaxElement::Node(it) => it.clone(),
|
||||||
SyntaxElement::Token(it) => it.parent(),
|
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())
|
.fold1(|a, b| least_common_ancestor(&a, &b).unwrap())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue