aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_analysis/src/completion.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_analysis/src/completion.rs')
-rw-r--r--crates/ra_analysis/src/completion.rs33
1 files changed, 28 insertions, 5 deletions
diff --git a/crates/ra_analysis/src/completion.rs b/crates/ra_analysis/src/completion.rs
index a11e98ac0..ae1280256 100644
--- a/crates/ra_analysis/src/completion.rs
+++ b/crates/ra_analysis/src/completion.rs
@@ -8,6 +8,8 @@ use ra_syntax::{
8 ast, 8 ast,
9 AstNode, 9 AstNode,
10 SyntaxNodeRef, 10 SyntaxNodeRef,
11 SourceFileNode,
12 TextUnit,
11}; 13};
12use ra_db::SyntaxDatabase; 14use ra_db::SyntaxDatabase;
13use rustc_hash::{FxHashMap}; 15use rustc_hash::{FxHashMap};
@@ -27,11 +29,6 @@ pub(crate) fn completions(
27) -> Cancelable<Option<Completions>> { 29) -> Cancelable<Option<Completions>> {
28 let original_file = db.source_file(position.file_id); 30 let original_file = db.source_file(position.file_id);
29 // Insert a fake ident to get a valid parse tree 31 // Insert a fake ident to get a valid parse tree
30 let file = {
31 let edit = AtomTextEdit::insert(position.offset, "intellijRulezz".to_string());
32 original_file.reparse(&edit)
33 };
34
35 let module = ctry!(source_binder::module_from_position(db, position)?); 32 let module = ctry!(source_binder::module_from_position(db, position)?);
36 33
37 let mut acc = Completions::default(); 34 let mut acc = Completions::default();
@@ -59,6 +56,32 @@ pub(crate) fn completions(
59 Ok(Some(acc)) 56 Ok(Some(acc))
60} 57}
61 58
59/// `SyntaxContext` is created early during completion to figure out, where
60/// exactly is the cursor, syntax-wise.
61#[derive(Debug)]
62pub(super) enum SyntaxContext<'a> {
63 ParameterName(SyntaxNodeRef<'a>),
64 Other,
65}
66
67impl SyntaxContext {
68 pub(super) fn new(original_file: &SourceFileNode, offset: TextUnit) -> SyntaxContext {
69 let file = {
70 let edit = AtomTextEdit::insert(offset, "intellijRulezz".to_string());
71 original_file.reparse(&edit)
72 };
73 if let Some(name) = find_node_at_offset::<ast::Name>(file.syntax(), offset) {
74 if is_node::<ast::Param>(name.syntax()) {
75 if let Some(node) = find_leaf_at_offset(original_file, offset).left_biased() {
76 return SyntaxContext::ParameterName(node);
77 }
78 }
79 }
80
81 SyntaxContext::Other
82 }
83}
84
62/// Complete repeated parametes, both name and type. For example, if all 85/// Complete repeated parametes, both name and type. For example, if all
63/// functions in a file have a `spam: &mut Spam` parameter, a completion with 86/// functions in a file have a `spam: &mut Spam` parameter, a completion with
64/// `spam: &mut Spam` insert text/label and `spam` lookup string will be 87/// `spam: &mut Spam` insert text/label and `spam` lookup string will be