aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_analysis/src/completion
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_analysis/src/completion')
-rw-r--r--crates/ra_analysis/src/completion/complete_fn_param.rs4
-rw-r--r--crates/ra_analysis/src/completion/complete_keyword.rs4
-rw-r--r--crates/ra_analysis/src/completion/complete_path.rs4
-rw-r--r--crates/ra_analysis/src/completion/complete_scope.rs4
-rw-r--r--crates/ra_analysis/src/completion/complete_snippet.rs6
-rw-r--r--crates/ra_analysis/src/completion/completion_context.rs156
6 files changed, 167 insertions, 11 deletions
diff --git a/crates/ra_analysis/src/completion/complete_fn_param.rs b/crates/ra_analysis/src/completion/complete_fn_param.rs
index d05a5e3cf..3ec507fdf 100644
--- a/crates/ra_analysis/src/completion/complete_fn_param.rs
+++ b/crates/ra_analysis/src/completion/complete_fn_param.rs
@@ -8,14 +8,14 @@ use ra_syntax::{
8use rustc_hash::{FxHashMap}; 8use rustc_hash::{FxHashMap};
9 9
10use crate::{ 10use crate::{
11 completion::{SyntaxContext, Completions, CompletionKind, CompletionItem}, 11 completion::{CompletionContext, Completions, CompletionKind, CompletionItem},
12}; 12};
13 13
14/// Complete repeated parametes, both name and type. For example, if all 14/// Complete repeated parametes, both name and type. For example, if all
15/// functions in a file have a `spam: &mut Spam` parameter, a completion with 15/// functions in a file have a `spam: &mut Spam` parameter, a completion with
16/// `spam: &mut Spam` insert text/label and `spam` lookup string will be 16/// `spam: &mut Spam` insert text/label and `spam` lookup string will be
17/// suggested. 17/// suggested.
18pub(super) fn complete_fn_param(acc: &mut Completions, ctx: &SyntaxContext) { 18pub(super) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext) {
19 if !ctx.is_param { 19 if !ctx.is_param {
20 return; 20 return;
21 } 21 }
diff --git a/crates/ra_analysis/src/completion/complete_keyword.rs b/crates/ra_analysis/src/completion/complete_keyword.rs
index d0a6ec19e..2ee36430e 100644
--- a/crates/ra_analysis/src/completion/complete_keyword.rs
+++ b/crates/ra_analysis/src/completion/complete_keyword.rs
@@ -6,10 +6,10 @@ use ra_syntax::{
6}; 6};
7 7
8use crate::{ 8use crate::{
9 completion::{SyntaxContext, CompletionItem, Completions, CompletionKind::*}, 9 completion::{CompletionContext, CompletionItem, Completions, CompletionKind::*},
10}; 10};
11 11
12pub(super) fn complete_expr_keyword(acc: &mut Completions, ctx: &SyntaxContext) { 12pub(super) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionContext) {
13 if !ctx.is_trivial_path { 13 if !ctx.is_trivial_path {
14 return; 14 return;
15 } 15 }
diff --git a/crates/ra_analysis/src/completion/complete_path.rs b/crates/ra_analysis/src/completion/complete_path.rs
index 8374ec346..41e439b1b 100644
--- a/crates/ra_analysis/src/completion/complete_path.rs
+++ b/crates/ra_analysis/src/completion/complete_path.rs
@@ -1,9 +1,9 @@
1use crate::{ 1use crate::{
2 completion::{CompletionItem, Completions, CompletionKind::*, SyntaxContext}, 2 completion::{CompletionItem, Completions, CompletionKind::*, CompletionContext},
3 Cancelable, 3 Cancelable,
4}; 4};
5 5
6pub(super) fn complete_path(acc: &mut Completions, ctx: &SyntaxContext) -> Cancelable<()> { 6pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) -> Cancelable<()> {
7 let (path, module) = match (&ctx.path_prefix, &ctx.module) { 7 let (path, module) = match (&ctx.path_prefix, &ctx.module) {
8 (Some(path), Some(module)) => (path.clone(), module), 8 (Some(path), Some(module)) => (path.clone(), module),
9 _ => return Ok(()), 9 _ => return Ok(()),
diff --git a/crates/ra_analysis/src/completion/complete_scope.rs b/crates/ra_analysis/src/completion/complete_scope.rs
index ddaf13b88..c1ab19d5b 100644
--- a/crates/ra_analysis/src/completion/complete_scope.rs
+++ b/crates/ra_analysis/src/completion/complete_scope.rs
@@ -2,11 +2,11 @@ use rustc_hash::FxHashSet;
2use ra_syntax::TextUnit; 2use ra_syntax::TextUnit;
3 3
4use crate::{ 4use crate::{
5 completion::{CompletionItem, Completions, CompletionKind::*, SyntaxContext}, 5 completion::{CompletionItem, Completions, CompletionKind::*, CompletionContext},
6 Cancelable 6 Cancelable
7}; 7};
8 8
9pub(super) fn complete_scope(acc: &mut Completions, ctx: &SyntaxContext) -> Cancelable<()> { 9pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) -> Cancelable<()> {
10 if !ctx.is_trivial_path { 10 if !ctx.is_trivial_path {
11 return Ok(()); 11 return Ok(());
12 } 12 }
diff --git a/crates/ra_analysis/src/completion/complete_snippet.rs b/crates/ra_analysis/src/completion/complete_snippet.rs
index 5d6cc5dc9..6816ae695 100644
--- a/crates/ra_analysis/src/completion/complete_snippet.rs
+++ b/crates/ra_analysis/src/completion/complete_snippet.rs
@@ -1,8 +1,8 @@
1use crate::{ 1use crate::{
2 completion::{CompletionItem, Completions, CompletionKind::*, SyntaxContext}, 2 completion::{CompletionItem, Completions, CompletionKind::*, CompletionContext},
3}; 3};
4 4
5pub(super) fn complete_expr_snippet(acc: &mut Completions, ctx: &SyntaxContext) { 5pub(super) fn complete_expr_snippet(acc: &mut Completions, ctx: &CompletionContext) {
6 if !(ctx.is_trivial_path && ctx.enclosing_fn.is_some()) { 6 if !(ctx.is_trivial_path && ctx.enclosing_fn.is_some()) {
7 return; 7 return;
8 } 8 }
@@ -16,7 +16,7 @@ pub(super) fn complete_expr_snippet(acc: &mut Completions, ctx: &SyntaxContext)
16 .add_to(acc); 16 .add_to(acc);
17} 17}
18 18
19pub(super) fn complete_item_snippet(acc: &mut Completions, ctx: &SyntaxContext) { 19pub(super) fn complete_item_snippet(acc: &mut Completions, ctx: &CompletionContext) {
20 if !ctx.is_new_item { 20 if !ctx.is_new_item {
21 return; 21 return;
22 } 22 }
diff --git a/crates/ra_analysis/src/completion/completion_context.rs b/crates/ra_analysis/src/completion/completion_context.rs
new file mode 100644
index 000000000..064fbc6f7
--- /dev/null
+++ b/crates/ra_analysis/src/completion/completion_context.rs
@@ -0,0 +1,156 @@
1use ra_editor::find_node_at_offset;
2use ra_text_edit::AtomTextEdit;
3use ra_syntax::{
4 algo::find_leaf_at_offset,
5 ast,
6 AstNode,
7 SyntaxNodeRef,
8 SourceFileNode,
9 TextUnit,
10 SyntaxKind::*,
11};
12use hir::source_binder;
13
14use crate::{db, FilePosition, Cancelable};
15
16/// `CompletionContext` is created early during completion to figure out, where
17/// exactly is the cursor, syntax-wise.
18#[derive(Debug)]
19pub(super) struct CompletionContext<'a> {
20 pub(super) db: &'a db::RootDatabase,
21 pub(super) offset: TextUnit,
22 pub(super) leaf: SyntaxNodeRef<'a>,
23 pub(super) module: Option<hir::Module>,
24 pub(super) enclosing_fn: Option<ast::FnDef<'a>>,
25 pub(super) is_param: bool,
26 /// A single-indent path, like `foo`.
27 pub(super) is_trivial_path: bool,
28 /// If not a trivial, path, the prefix (qualifier).
29 pub(super) path_prefix: Option<hir::Path>,
30 pub(super) after_if: bool,
31 pub(super) is_stmt: bool,
32 /// Something is typed at the "top" level, in module or impl/trait.
33 pub(super) is_new_item: bool,
34}
35
36impl<'a> CompletionContext<'a> {
37 pub(super) fn new(
38 db: &'a db::RootDatabase,
39 original_file: &'a SourceFileNode,
40 position: FilePosition,
41 ) -> Cancelable<Option<CompletionContext<'a>>> {
42 let module = source_binder::module_from_position(db, position)?;
43 let leaf =
44 ctry!(find_leaf_at_offset(original_file.syntax(), position.offset).left_biased());
45 let mut ctx = CompletionContext {
46 db,
47 leaf,
48 offset: position.offset,
49 module,
50 enclosing_fn: None,
51 is_param: false,
52 is_trivial_path: false,
53 path_prefix: None,
54 after_if: false,
55 is_stmt: false,
56 is_new_item: false,
57 };
58 ctx.fill(original_file, position.offset);
59 Ok(Some(ctx))
60 }
61
62 fn fill(&mut self, original_file: &SourceFileNode, offset: TextUnit) {
63 // Insert a fake ident to get a valid parse tree. We will use this file
64 // to determine context, though the original_file will be used for
65 // actual completion.
66 let file = {
67 let edit = AtomTextEdit::insert(offset, "intellijRulezz".to_string());
68 original_file.reparse(&edit)
69 };
70
71 // First, let's try to complete a reference to some declaration.
72 if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(file.syntax(), offset) {
73 // Special case, `trait T { fn foo(i_am_a_name_ref) {} }`.
74 // See RFC#1685.
75 if is_node::<ast::Param>(name_ref.syntax()) {
76 self.is_param = true;
77 return;
78 }
79 self.classify_name_ref(&file, name_ref);
80 }
81
82 // Otherwise, see if this is a declaration. We can use heuristics to
83 // suggest declaration names, see `CompletionKind::Magic`.
84 if let Some(name) = find_node_at_offset::<ast::Name>(file.syntax(), offset) {
85 if is_node::<ast::Param>(name.syntax()) {
86 self.is_param = true;
87 return;
88 }
89 }
90 }
91 fn classify_name_ref(&mut self, file: &SourceFileNode, name_ref: ast::NameRef) {
92 let name_range = name_ref.syntax().range();
93 let top_node = name_ref
94 .syntax()
95 .ancestors()
96 .take_while(|it| it.range() == name_range)
97 .last()
98 .unwrap();
99
100 match top_node.parent().map(|it| it.kind()) {
101 Some(SOURCE_FILE) | Some(ITEM_LIST) => {
102 self.is_new_item = true;
103 return;
104 }
105 _ => (),
106 }
107
108 let parent = match name_ref.syntax().parent() {
109 Some(it) => it,
110 None => return,
111 };
112 if let Some(segment) = ast::PathSegment::cast(parent) {
113 let path = segment.parent_path();
114 if let Some(mut path) = hir::Path::from_ast(path) {
115 if !path.is_ident() {
116 path.segments.pop().unwrap();
117 self.path_prefix = Some(path);
118 return;
119 }
120 }
121 if path.qualifier().is_none() {
122 self.is_trivial_path = true;
123 self.enclosing_fn = self
124 .leaf
125 .ancestors()
126 .take_while(|it| it.kind() != SOURCE_FILE && it.kind() != MODULE)
127 .find_map(ast::FnDef::cast);
128
129 self.is_stmt = match name_ref
130 .syntax()
131 .ancestors()
132 .filter_map(ast::ExprStmt::cast)
133 .next()
134 {
135 None => false,
136 Some(expr_stmt) => expr_stmt.syntax().range() == name_ref.syntax().range(),
137 };
138
139 if let Some(off) = name_ref.syntax().range().start().checked_sub(2.into()) {
140 if let Some(if_expr) = find_node_at_offset::<ast::IfExpr>(file.syntax(), off) {
141 if if_expr.syntax().range().end() < name_ref.syntax().range().start() {
142 self.after_if = true;
143 }
144 }
145 }
146 }
147 }
148 }
149}
150
151fn is_node<'a, N: AstNode<'a>>(node: SyntaxNodeRef<'a>) -> bool {
152 match node.ancestors().filter_map(N::cast).next() {
153 None => false,
154 Some(n) => n.syntax().range() == node.range(),
155 }
156}