aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2019-01-08 18:04:08 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2019-01-08 18:04:08 +0000
commitc9e42fcf245be16958dca6571e4bccc6c29199df (patch)
treecaa02f8086ad15fb6f884e56bc6a0231b203215f /crates/ra_syntax/src
parent1c25bf05d714680c048d250a5d39e8a4c25f0c31 (diff)
parent695294bbb974cdbac136e260029403e90a17d953 (diff)
Merge #468
468: decouple ra_editor from other stuff r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_syntax/src')
-rw-r--r--crates/ra_syntax/src/algo.rs15
1 files changed, 14 insertions, 1 deletions
diff --git a/crates/ra_syntax/src/algo.rs b/crates/ra_syntax/src/algo.rs
index 13f50d2ef..45747e21d 100644
--- a/crates/ra_syntax/src/algo.rs
+++ b/crates/ra_syntax/src/algo.rs
@@ -2,7 +2,7 @@ pub mod visit;
2 2
3use rowan::TransparentNewType; 3use rowan::TransparentNewType;
4 4
5use crate::{SyntaxNode, TextRange, TextUnit}; 5use crate::{SyntaxNode, TextRange, TextUnit, AstNode};
6 6
7pub use rowan::LeafAtOffset; 7pub use rowan::LeafAtOffset;
8 8
@@ -16,6 +16,19 @@ pub fn find_leaf_at_offset(node: &SyntaxNode, offset: TextUnit) -> LeafAtOffset<
16 } 16 }
17} 17}
18 18
19/// Finds a node of specific Ast type at offset. Note that this is slightly
20/// impercise: if the cursor is strictly betwen two nodes of the desired type,
21/// as in
22///
23/// ```no-run
24/// struct Foo {}|struct Bar;
25/// ```
26///
27/// then the left node will be silently prefered.
28pub fn find_node_at_offset<N: AstNode>(syntax: &SyntaxNode, offset: TextUnit) -> Option<&N> {
29 find_leaf_at_offset(syntax, offset).find_map(|leaf| leaf.ancestors().find_map(N::cast))
30}
31
19pub fn find_covering_node(root: &SyntaxNode, range: TextRange) -> &SyntaxNode { 32pub fn find_covering_node(root: &SyntaxNode, range: TextRange) -> &SyntaxNode {
20 SyntaxNode::from_repr(root.0.covering_node(range)) 33 SyntaxNode::from_repr(root.0.covering_node(range))
21} 34}