diff options
author | Aleksey Kladov <[email protected]> | 2018-08-26 10:09:28 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-08-26 10:09:28 +0100 |
commit | ac226021cfd26a9332b5971f3e05118d77822af5 (patch) | |
tree | 04e9d19956b74d551f798bf26f420fa978b8f30f /crates/libeditor/src | |
parent | 4c121bfa2f2a7a06f01143e3203c650156e2fb4e (diff) |
scope based comletion
Diffstat (limited to 'crates/libeditor/src')
-rw-r--r-- | crates/libeditor/src/code_actions.rs | 2 | ||||
-rw-r--r-- | crates/libeditor/src/completion.rs | 42 |
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 @@ | |||
1 | use libsyntax2::{ | 1 | use 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 | ||
7 | use { | 11 | use { |
@@ -25,7 +29,33 @@ pub fn scope_completion(file: &File, offset: TextUnit) -> Option<Vec<CompletionI | |||
25 | } | 29 | } |
26 | 30 | ||
27 | fn complete(name_ref: ast::NameRef) -> Vec<CompletionItem> { | 31 | fn 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 | |||
39 | fn 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 | } |