diff options
Diffstat (limited to 'crates/ra_syntax/src/algo.rs')
-rw-r--r-- | crates/ra_syntax/src/algo.rs | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/crates/ra_syntax/src/algo.rs b/crates/ra_syntax/src/algo.rs index 664894d1f..26b3c813a 100644 --- a/crates/ra_syntax/src/algo.rs +++ b/crates/ra_syntax/src/algo.rs | |||
@@ -41,6 +41,10 @@ pub fn find_node_at_offset<N: AstNode>(syntax: &SyntaxNode, offset: TextSize) -> | |||
41 | ancestors_at_offset(syntax, offset).find_map(N::cast) | 41 | ancestors_at_offset(syntax, offset).find_map(N::cast) |
42 | } | 42 | } |
43 | 43 | ||
44 | pub fn find_node_at_range<N: AstNode>(syntax: &SyntaxNode, range: TextRange) -> Option<N> { | ||
45 | find_covering_element(syntax, range).ancestors().find_map(N::cast) | ||
46 | } | ||
47 | |||
44 | /// Skip to next non `trivia` token | 48 | /// Skip to next non `trivia` token |
45 | pub fn skip_trivia_token(mut token: SyntaxToken, direction: Direction) -> Option<SyntaxToken> { | 49 | pub fn skip_trivia_token(mut token: SyntaxToken, direction: Direction) -> Option<SyntaxToken> { |
46 | while token.kind().is_trivia() { | 50 | while token.kind().is_trivia() { |
@@ -290,6 +294,11 @@ impl<'a> SyntaxRewriter<'a> { | |||
290 | N::cast(self.rewrite(node.syntax())).unwrap() | 294 | N::cast(self.rewrite(node.syntax())).unwrap() |
291 | } | 295 | } |
292 | 296 | ||
297 | /// Returns a node that encompasses all replacements to be done by this rewriter. | ||
298 | /// | ||
299 | /// Passing the returned node to `rewrite` will apply all replacements queued up in `self`. | ||
300 | /// | ||
301 | /// Returns `None` when there are no replacements. | ||
293 | pub fn rewrite_root(&self) -> Option<SyntaxNode> { | 302 | pub fn rewrite_root(&self) -> Option<SyntaxNode> { |
294 | assert!(self.f.is_none()); | 303 | assert!(self.f.is_none()); |
295 | self.replacements | 304 | self.replacements |
@@ -298,6 +307,9 @@ impl<'a> SyntaxRewriter<'a> { | |||
298 | SyntaxElement::Node(it) => it.clone(), | 307 | SyntaxElement::Node(it) => it.clone(), |
299 | SyntaxElement::Token(it) => it.parent(), | 308 | SyntaxElement::Token(it) => it.parent(), |
300 | }) | 309 | }) |
310 | // If we only have one replacement, we must return its parent node, since `rewrite` does | ||
311 | // not replace the node passed to it. | ||
312 | .map(|it| it.parent().unwrap_or(it)) | ||
301 | .fold1(|a, b| least_common_ancestor(&a, &b).unwrap()) | 313 | .fold1(|a, b| least_common_ancestor(&a, &b).unwrap()) |
302 | } | 314 | } |
303 | 315 | ||