aboutsummaryrefslogtreecommitdiff
path: root/crates/libeditor/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/libeditor/src')
-rw-r--r--crates/libeditor/src/code_actions.rs2
-rw-r--r--crates/libeditor/src/completion.rs42
2 files changed, 37 insertions, 7 deletions
diff --git a/crates/libeditor/src/code_actions.rs b/crates/libeditor/src/code_actions.rs
index f53a8f9c6..e6ba83d2e 100644
--- a/crates/libeditor/src/code_actions.rs
+++ b/crates/libeditor/src/code_actions.rs
@@ -9,7 +9,7 @@ use libsyntax2::{
9 SyntaxNodeRef, 9 SyntaxNodeRef,
10 algo::{ 10 algo::{
11 Direction, siblings, 11 Direction, siblings,
12 find_leaf_at_offset, ancestors, 12 find_leaf_at_offset,
13 }, 13 },
14}; 14};
15 15
diff --git a/crates/libeditor/src/completion.rs b/crates/libeditor/src/completion.rs
index cf61ec784..16c9ead74 100644
--- a/crates/libeditor/src/completion.rs
+++ b/crates/libeditor/src/completion.rs
@@ -1,7 +1,11 @@
1use libsyntax2::{ 1use libsyntax2::{
2 File, TextUnit, 2 File, TextUnit, AstNode, SyntaxNodeRef,
3 ast, 3 ast::{self, NameOwner},
4 algo::find_leaf_at_offset, 4 algo::{
5 ancestors,
6 visit::{visitor_ctx, VisitorCtx},
7 walk::preorder,
8 },
5}; 9};
6 10
7use { 11use {
@@ -25,7 +29,33 @@ pub fn scope_completion(file: &File, offset: TextUnit) -> Option<Vec<CompletionI
25} 29}
26 30
27fn complete(name_ref: ast::NameRef) -> Vec<CompletionItem> { 31fn complete(name_ref: ast::NameRef) -> Vec<CompletionItem> {
28 vec![CompletionItem { 32 let mut res = Vec::new();
29 name: "foo".to_string() 33 for node in ancestors(name_ref.syntax()) {
30 }] 34 process_scope(node, &mut res);
35 }
36 res
37}
38
39fn process_scope(node: SyntaxNodeRef, sink: &mut Vec<CompletionItem>) {
40 let _ = visitor_ctx(sink)
41 .visit::<ast::Block, _>(|block, sink| {
42 block.let_stmts()
43 .filter_map(|it| it.pat())
44 .for_each(move |it| process_pat(it, sink))
45 })
46 .visit::<ast::FnDef, _>(|fn_def, sink| {
47 fn_def.param_list().into_iter()
48 .flat_map(|it| it.params())
49 .filter_map(|it| it.pat())
50 .for_each(move |it| process_pat(it, sink))
51 })
52 .accept(node);
53
54 fn process_pat(pat: ast::Pat, sink: &mut Vec<CompletionItem>) {
55 let items = preorder(pat.syntax())
56 .filter_map(ast::BindPat::cast)
57 .filter_map(ast::BindPat::name)
58 .map(|name| CompletionItem { name: name.text().to_string() });
59 sink.extend(items);
60 }
31} 61}