aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_completion/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide_completion/src')
-rw-r--r--crates/ide_completion/src/completions/dot.rs2
-rw-r--r--crates/ide_completion/src/completions/flyimport.rs6
-rw-r--r--crates/ide_completion/src/completions/keyword.rs11
-rw-r--r--crates/ide_completion/src/completions/qualified_path.rs4
-rw-r--r--crates/ide_completion/src/completions/snippet.rs4
-rw-r--r--crates/ide_completion/src/completions/unqualified_path.rs2
-rw-r--r--crates/ide_completion/src/context.rs71
-rw-r--r--crates/ide_completion/src/render.rs8
8 files changed, 62 insertions, 46 deletions
diff --git a/crates/ide_completion/src/completions/dot.rs b/crates/ide_completion/src/completions/dot.rs
index 22844c2ae..8ad57a069 100644
--- a/crates/ide_completion/src/completions/dot.rs
+++ b/crates/ide_completion/src/completions/dot.rs
@@ -33,7 +33,7 @@ fn complete_undotted_self(acc: &mut Completions, ctx: &CompletionContext) {
33 if !ctx.config.enable_self_on_the_fly { 33 if !ctx.config.enable_self_on_the_fly {
34 return; 34 return;
35 } 35 }
36 if !ctx.is_trivial_path || ctx.is_path_disallowed() { 36 if !ctx.is_trivial_path() || ctx.is_path_disallowed() {
37 return; 37 return;
38 } 38 }
39 ctx.scope.process_all_names(&mut |name, def| { 39 ctx.scope.process_all_names(&mut |name, def| {
diff --git a/crates/ide_completion/src/completions/flyimport.rs b/crates/ide_completion/src/completions/flyimport.rs
index d72bf13d3..7bf47bf75 100644
--- a/crates/ide_completion/src/completions/flyimport.rs
+++ b/crates/ide_completion/src/completions/flyimport.rs
@@ -161,13 +161,13 @@ pub(crate) fn position_for_import<'a>(
161) -> Option<&'a SyntaxNode> { 161) -> Option<&'a SyntaxNode> {
162 Some(match import_candidate { 162 Some(match import_candidate {
163 Some(ImportCandidate::Path(_)) => ctx.name_ref_syntax.as_ref()?.syntax(), 163 Some(ImportCandidate::Path(_)) => ctx.name_ref_syntax.as_ref()?.syntax(),
164 Some(ImportCandidate::TraitAssocItem(_)) => ctx.path_qual.as_ref()?.syntax(), 164 Some(ImportCandidate::TraitAssocItem(_)) => ctx.path_qual()?.syntax(),
165 Some(ImportCandidate::TraitMethod(_)) => ctx.dot_receiver()?.syntax(), 165 Some(ImportCandidate::TraitMethod(_)) => ctx.dot_receiver()?.syntax(),
166 None => ctx 166 None => ctx
167 .name_ref_syntax 167 .name_ref_syntax
168 .as_ref() 168 .as_ref()
169 .map(|name_ref| name_ref.syntax()) 169 .map(|name_ref| name_ref.syntax())
170 .or_else(|| ctx.path_qual.as_ref().map(|path| path.syntax())) 170 .or_else(|| ctx.path_qual().map(|path| path.syntax()))
171 .or_else(|| ctx.dot_receiver().map(|expr| expr.syntax()))?, 171 .or_else(|| ctx.dot_receiver().map(|expr| expr.syntax()))?,
172 }) 172 })
173} 173}
@@ -190,7 +190,7 @@ fn import_assets(ctx: &CompletionContext, fuzzy_name: String) -> Option<ImportAs
190 }; 190 };
191 let assets_for_path = ImportAssets::for_fuzzy_path( 191 let assets_for_path = ImportAssets::for_fuzzy_path(
192 current_module, 192 current_module,
193 ctx.path_qual.clone(), 193 ctx.path_qual().cloned(),
194 fuzzy_name, 194 fuzzy_name,
195 &ctx.sema, 195 &ctx.sema,
196 approximate_node, 196 approximate_node,
diff --git a/crates/ide_completion/src/completions/keyword.rs b/crates/ide_completion/src/completions/keyword.rs
index 1a7a484a4..0ca97a0e4 100644
--- a/crates/ide_completion/src/completions/keyword.rs
+++ b/crates/ide_completion/src/completions/keyword.rs
@@ -19,11 +19,12 @@ pub(crate) fn complete_use_tree_keyword(acc: &mut Completions, ctx: &CompletionC
19 }; 19 };
20 20
21 if ctx.use_item_syntax.is_some() { 21 if ctx.use_item_syntax.is_some() {
22 if ctx.path_qual.is_none() { 22 let qual = ctx.path_qual();
23 if qual.is_none() {
23 kw_completion("crate::").add_to(acc); 24 kw_completion("crate::").add_to(acc);
24 } 25 }
25 kw_completion("self").add_to(acc); 26 kw_completion("self").add_to(acc);
26 if iter::successors(ctx.path_qual.clone(), |p| p.qualifier()) 27 if iter::successors(qual.cloned(), |p| p.qualifier())
27 .all(|p| p.segment().and_then(|s| s.super_token()).is_some()) 28 .all(|p| p.segment().and_then(|s| s.super_token()).is_some())
28 { 29 {
29 kw_completion("super::").add_to(acc); 30 kw_completion("super::").add_to(acc);
@@ -128,7 +129,7 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
128 } 129 }
129 130
130 if ctx.in_loop_body { 131 if ctx.in_loop_body {
131 if ctx.can_be_stmt { 132 if ctx.can_be_stmt() {
132 add_keyword("continue", "continue;"); 133 add_keyword("continue", "continue;");
133 add_keyword("break", "break;"); 134 add_keyword("break", "break;");
134 } else { 135 } else {
@@ -137,7 +138,7 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
137 } 138 }
138 } 139 }
139 140
140 if !ctx.is_trivial_path { 141 if !ctx.is_trivial_path() {
141 return; 142 return;
142 } 143 }
143 let fn_def = match &ctx.function_def { 144 let fn_def = match &ctx.function_def {
@@ -147,7 +148,7 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
147 148
148 add_keyword( 149 add_keyword(
149 "return", 150 "return",
150 match (ctx.can_be_stmt, fn_def.ret_type().is_some()) { 151 match (ctx.can_be_stmt(), fn_def.ret_type().is_some()) {
151 (true, true) => "return $0;", 152 (true, true) => "return $0;",
152 (true, false) => "return;", 153 (true, false) => "return;",
153 (false, true) => "return $0", 154 (false, true) => "return $0",
diff --git a/crates/ide_completion/src/completions/qualified_path.rs b/crates/ide_completion/src/completions/qualified_path.rs
index de58ce1cd..c072de7b5 100644
--- a/crates/ide_completion/src/completions/qualified_path.rs
+++ b/crates/ide_completion/src/completions/qualified_path.rs
@@ -10,8 +10,8 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon
10 if ctx.is_path_disallowed() || ctx.expects_item() { 10 if ctx.is_path_disallowed() || ctx.expects_item() {
11 return; 11 return;
12 } 12 }
13 let path = match &ctx.path_qual { 13 let path = match ctx.path_qual() {
14 Some(path) => path.clone(), 14 Some(path) => path,
15 None => return, 15 None => return,
16 }; 16 };
17 17
diff --git a/crates/ide_completion/src/completions/snippet.rs b/crates/ide_completion/src/completions/snippet.rs
index 6e6a6eb92..59a338e7b 100644
--- a/crates/ide_completion/src/completions/snippet.rs
+++ b/crates/ide_completion/src/completions/snippet.rs
@@ -14,7 +14,7 @@ fn snippet(ctx: &CompletionContext, cap: SnippetCap, label: &str, snippet: &str)
14} 14}
15 15
16pub(crate) fn complete_expr_snippet(acc: &mut Completions, ctx: &CompletionContext) { 16pub(crate) fn complete_expr_snippet(acc: &mut Completions, ctx: &CompletionContext) {
17 if !(ctx.is_trivial_path && ctx.function_def.is_some()) { 17 if !(ctx.is_trivial_path() && ctx.function_def.is_some()) {
18 return; 18 return;
19 } 19 }
20 let cap = match ctx.config.snippet_cap { 20 let cap = match ctx.config.snippet_cap {
@@ -22,7 +22,7 @@ pub(crate) fn complete_expr_snippet(acc: &mut Completions, ctx: &CompletionConte
22 None => return, 22 None => return,
23 }; 23 };
24 24
25 if ctx.can_be_stmt { 25 if ctx.can_be_stmt() {
26 snippet(ctx, cap, "pd", "eprintln!(\"$0 = {:?}\", $0);").add_to(acc); 26 snippet(ctx, cap, "pd", "eprintln!(\"$0 = {:?}\", $0);").add_to(acc);
27 snippet(ctx, cap, "ppd", "eprintln!(\"$0 = {:#?}\", $0);").add_to(acc); 27 snippet(ctx, cap, "ppd", "eprintln!(\"$0 = {:#?}\", $0);").add_to(acc);
28 } 28 }
diff --git a/crates/ide_completion/src/completions/unqualified_path.rs b/crates/ide_completion/src/completions/unqualified_path.rs
index bd955aa85..f321ed52b 100644
--- a/crates/ide_completion/src/completions/unqualified_path.rs
+++ b/crates/ide_completion/src/completions/unqualified_path.rs
@@ -5,7 +5,7 @@ use hir::ScopeDef;
5use crate::{CompletionContext, Completions}; 5use crate::{CompletionContext, Completions};
6 6
7pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionContext) { 7pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionContext) {
8 if !ctx.is_trivial_path { 8 if !ctx.is_trivial_path() {
9 return; 9 return;
10 } 10 }
11 if ctx.is_path_disallowed() || ctx.expects_item() { 11 if ctx.is_path_disallowed() || ctx.expects_item() {
diff --git a/crates/ide_completion/src/context.rs b/crates/ide_completion/src/context.rs
index 6bd67c123..20e033d31 100644
--- a/crates/ide_completion/src/context.rs
+++ b/crates/ide_completion/src/context.rs
@@ -33,6 +33,16 @@ pub(crate) enum PatternRefutability {
33pub(crate) struct PathCompletionContext { 33pub(crate) struct PathCompletionContext {
34 /// If this is a call with () already there 34 /// If this is a call with () already there
35 call_kind: Option<CallKind>, 35 call_kind: Option<CallKind>,
36 /// A single-indent path, like `foo`. `::foo` should not be considered a trivial path.
37 pub(super) is_trivial_path: bool,
38 /// If not a trivial path, the prefix (qualifier).
39 pub(super) path_qual: Option<ast::Path>,
40 pub(super) is_path_type: bool,
41 pub(super) has_type_args: bool,
42 /// `true` if we are a statement or a last expr in the block.
43 pub(super) can_be_stmt: bool,
44 /// `true` if we expect an expression at the cursor position.
45 pub(super) is_expr: bool,
36} 46}
37 47
38#[derive(Copy, Clone, Debug, PartialEq, Eq)] 48#[derive(Copy, Clone, Debug, PartialEq, Eq)]
@@ -83,16 +93,6 @@ pub(crate) struct CompletionContext<'a> {
83 pub(super) path_context: Option<PathCompletionContext>, 93 pub(super) path_context: Option<PathCompletionContext>,
84 /// FIXME: `ActiveParameter` is string-based, which is very very wrong 94 /// FIXME: `ActiveParameter` is string-based, which is very very wrong
85 pub(super) active_parameter: Option<ActiveParameter>, 95 pub(super) active_parameter: Option<ActiveParameter>,
86 /// A single-indent path, like `foo`. `::foo` should not be considered a trivial path.
87 pub(super) is_trivial_path: bool,
88 /// If not a trivial path, the prefix (qualifier).
89 pub(super) path_qual: Option<ast::Path>,
90 /// `true` if we are a statement or a last expr in the block.
91 pub(super) can_be_stmt: bool,
92 /// `true` if we expect an expression at the cursor position.
93 pub(super) is_expr: bool,
94 pub(super) is_path_type: bool,
95 pub(super) has_type_args: bool,
96 pub(super) locals: Vec<(String, Local)>, 96 pub(super) locals: Vec<(String, Local)>,
97 97
98 pub(super) previous_token: Option<SyntaxToken>, 98 pub(super) previous_token: Option<SyntaxToken>,
@@ -156,13 +156,7 @@ impl<'a> CompletionContext<'a> {
156 is_label_ref: false, 156 is_label_ref: false,
157 is_param: false, 157 is_param: false,
158 is_pat_or_const: None, 158 is_pat_or_const: None,
159 is_trivial_path: false,
160 path_qual: None,
161 can_be_stmt: false,
162 is_expr: false,
163 path_context: None, 159 path_context: None,
164 is_path_type: false,
165 has_type_args: false,
166 previous_token: None, 160 previous_token: None,
167 in_loop_body: false, 161 in_loop_body: false,
168 completion_location: None, 162 completion_location: None,
@@ -280,11 +274,6 @@ impl<'a> CompletionContext<'a> {
280 matches!(self.completion_location, Some(ImmediateLocation::ItemList)) 274 matches!(self.completion_location, Some(ImmediateLocation::ItemList))
281 } 275 }
282 276
283 // fn expects_value(&self) -> bool {
284 pub(crate) fn expects_expression(&self) -> bool {
285 self.is_expr
286 }
287
288 pub(crate) fn has_block_expr_parent(&self) -> bool { 277 pub(crate) fn has_block_expr_parent(&self) -> bool {
289 matches!(self.completion_location, Some(ImmediateLocation::BlockExpr)) 278 matches!(self.completion_location, Some(ImmediateLocation::BlockExpr))
290 } 279 }
@@ -321,10 +310,26 @@ impl<'a> CompletionContext<'a> {
321 ) || self.attribute_under_caret.is_some() 310 ) || self.attribute_under_caret.is_some()
322 } 311 }
323 312
313 pub(crate) fn expects_expression(&self) -> bool {
314 self.path_context.as_ref().map_or(false, |it| it.is_expr)
315 }
316
324 pub(crate) fn path_call_kind(&self) -> Option<CallKind> { 317 pub(crate) fn path_call_kind(&self) -> Option<CallKind> {
325 self.path_context.as_ref().and_then(|it| it.call_kind) 318 self.path_context.as_ref().and_then(|it| it.call_kind)
326 } 319 }
327 320
321 pub(crate) fn is_trivial_path(&self) -> bool {
322 self.path_context.as_ref().map_or(false, |it| it.is_trivial_path)
323 }
324
325 pub(crate) fn path_qual(&self) -> Option<&ast::Path> {
326 self.path_context.as_ref().and_then(|it| it.path_qual.as_ref())
327 }
328
329 pub(crate) fn can_be_stmt(&self) -> bool {
330 self.path_context.as_ref().map_or(false, |it| it.can_be_stmt)
331 }
332
328 fn fill_impl_def(&mut self) { 333 fn fill_impl_def(&mut self) {
329 self.impl_def = self 334 self.impl_def = self
330 .sema 335 .sema
@@ -577,7 +582,15 @@ impl<'a> CompletionContext<'a> {
577 }; 582 };
578 583
579 if let Some(segment) = ast::PathSegment::cast(parent) { 584 if let Some(segment) = ast::PathSegment::cast(parent) {
580 let mut path_ctx = PathCompletionContext { call_kind: None }; 585 let path_ctx = self.path_context.get_or_insert(PathCompletionContext {
586 call_kind: None,
587 is_trivial_path: false,
588 path_qual: None,
589 has_type_args: false,
590 is_path_type: false,
591 can_be_stmt: false,
592 is_expr: false,
593 });
581 let path = segment.parent_path(); 594 let path = segment.parent_path();
582 595
583 if let Some(p) = path.syntax().parent() { 596 if let Some(p) = path.syntax().parent() {
@@ -590,13 +603,11 @@ impl<'a> CompletionContext<'a> {
590 } 603 }
591 }; 604 };
592 } 605 }
593 self.path_context = Some(path_ctx); 606 path_ctx.is_path_type = path.syntax().parent().and_then(ast::PathType::cast).is_some();
594 dbg!(&self.path_context); 607 path_ctx.has_type_args = segment.generic_arg_list().is_some();
595 self.is_path_type = path.syntax().parent().and_then(ast::PathType::cast).is_some();
596 self.has_type_args = segment.generic_arg_list().is_some();
597 608
598 if let Some(path) = path_or_use_tree_qualifier(&path) { 609 if let Some(path) = path_or_use_tree_qualifier(&path) {
599 self.path_qual = path 610 path_ctx.path_qual = path
600 .segment() 611 .segment()
601 .and_then(|it| { 612 .and_then(|it| {
602 find_node_with_range::<ast::PathSegment>( 613 find_node_with_range::<ast::PathSegment>(
@@ -614,11 +625,11 @@ impl<'a> CompletionContext<'a> {
614 } 625 }
615 } 626 }
616 627
617 self.is_trivial_path = true; 628 path_ctx.is_trivial_path = true;
618 629
619 // Find either enclosing expr statement (thing with `;`) or a 630 // Find either enclosing expr statement (thing with `;`) or a
620 // block. If block, check that we are the last expr. 631 // block. If block, check that we are the last expr.
621 self.can_be_stmt = name_ref 632 path_ctx.can_be_stmt = name_ref
622 .syntax() 633 .syntax()
623 .ancestors() 634 .ancestors()
624 .find_map(|node| { 635 .find_map(|node| {
@@ -634,7 +645,7 @@ impl<'a> CompletionContext<'a> {
634 None 645 None
635 }) 646 })
636 .unwrap_or(false); 647 .unwrap_or(false);
637 self.is_expr = path.syntax().parent().and_then(ast::PathExpr::cast).is_some(); 648 path_ctx.is_expr = path.syntax().parent().and_then(ast::PathExpr::cast).is_some();
638 } 649 }
639 } 650 }
640} 651}
diff --git a/crates/ide_completion/src/render.rs b/crates/ide_completion/src/render.rs
index a49a60711..750694432 100644
--- a/crates/ide_completion/src/render.rs
+++ b/crates/ide_completion/src/render.rs
@@ -275,8 +275,12 @@ impl<'a> Render<'a> {
275 }; 275 };
276 276
277 // Add `<>` for generic types 277 // Add `<>` for generic types
278 if self.ctx.completion.is_path_type 278 if self
279 && !self.ctx.completion.has_type_args 279 .ctx
280 .completion
281 .path_context
282 .as_ref()
283 .map_or(false, |it| it.is_path_type && !it.has_type_args)
280 && self.ctx.completion.config.add_call_parenthesis 284 && self.ctx.completion.config.add_call_parenthesis
281 { 285 {
282 if let Some(cap) = self.ctx.snippet_cap() { 286 if let Some(cap) = self.ctx.snippet_cap() {