aboutsummaryrefslogtreecommitdiff
path: root/crates/libeditor/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/libeditor/src/lib.rs')
-rw-r--r--crates/libeditor/src/lib.rs21
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;
8mod edit; 8mod edit;
9mod code_actions; 9mod code_actions;
10mod typing; 10mod typing;
11mod completion;
11 12
12use libsyntax2::{ 13use 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};
18pub use libsyntax2::AtomEdit; 19pub 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
144pub 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}