diff options
Diffstat (limited to 'crates/libeditor/src/lib.rs')
-rw-r--r-- | crates/libeditor/src/lib.rs | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/crates/libeditor/src/lib.rs b/crates/libeditor/src/lib.rs index 55302265f..60489f7e3 100644 --- a/crates/libeditor/src/lib.rs +++ b/crates/libeditor/src/lib.rs | |||
@@ -8,11 +8,12 @@ mod line_index; | |||
8 | mod edit; | 8 | mod edit; |
9 | mod code_actions; | 9 | mod code_actions; |
10 | mod typing; | 10 | mod typing; |
11 | mod completion; | ||
11 | 12 | ||
12 | use libsyntax2::{ | 13 | use libsyntax2::{ |
13 | File, TextUnit, TextRange, | 14 | File, TextUnit, TextRange, SyntaxNodeRef, |
14 | ast::{AstNode, NameOwner}, | 15 | ast::{AstNode, NameOwner}, |
15 | algo::{walk, find_leaf_at_offset}, | 16 | algo::{walk, find_leaf_at_offset, ancestors}, |
16 | SyntaxKind::{self, *}, | 17 | SyntaxKind::{self, *}, |
17 | }; | 18 | }; |
18 | pub use libsyntax2::AtomEdit; | 19 | pub use libsyntax2::AtomEdit; |
@@ -22,10 +23,11 @@ pub use self::{ | |||
22 | symbols::{StructureNode, file_structure, FileSymbol, file_symbols}, | 23 | symbols::{StructureNode, file_structure, FileSymbol, file_symbols}, |
23 | edit::{EditBuilder, Edit}, | 24 | edit::{EditBuilder, Edit}, |
24 | code_actions::{ | 25 | code_actions::{ |
25 | ActionResult, find_node, | 26 | ActionResult, |
26 | flip_comma, add_derive, add_impl, | 27 | flip_comma, add_derive, add_impl, |
27 | }, | 28 | }, |
28 | typing::join_lines, | 29 | typing::join_lines, |
30 | completion::scope_completion, | ||
29 | }; | 31 | }; |
30 | 32 | ||
31 | #[derive(Debug)] | 33 | #[derive(Debug)] |
@@ -138,3 +140,16 @@ pub fn runnables(file: &File) -> Vec<Runnable> { | |||
138 | }) | 140 | }) |
139 | .collect() | 141 | .collect() |
140 | } | 142 | } |
143 | |||
144 | pub fn find_node_at_offset<'a, N: AstNode<'a>>( | ||
145 | syntax: SyntaxNodeRef<'a>, | ||
146 | offset: TextUnit, | ||
147 | ) -> Option<N> { | ||
148 | let leaves = find_leaf_at_offset(syntax, offset); | ||
149 | let leaf = leaves.clone() | ||
150 | .find(|leaf| !leaf.kind().is_trivia()) | ||
151 | .or_else(|| leaves.right_biased())?; | ||
152 | ancestors(leaf) | ||
153 | .filter_map(N::cast) | ||
154 | .next() | ||
155 | } | ||