diff options
Diffstat (limited to 'crates/ra_ide_api/src/completion')
5 files changed, 42 insertions, 24 deletions
diff --git a/crates/ra_ide_api/src/completion/complete_fn_param.rs b/crates/ra_ide_api/src/completion/complete_fn_param.rs index ffdc744b2..f87ccdeb9 100644 --- a/crates/ra_ide_api/src/completion/complete_fn_param.rs +++ b/crates/ra_ide_api/src/completion/complete_fn_param.rs | |||
@@ -17,7 +17,7 @@ pub(super) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext) | |||
17 | } | 17 | } |
18 | 18 | ||
19 | let mut params = FxHashMap::default(); | 19 | let mut params = FxHashMap::default(); |
20 | for node in ctx.leaf.ancestors() { | 20 | for node in ctx.token.parent().ancestors() { |
21 | let _ = visitor_ctx(&mut params) | 21 | let _ = visitor_ctx(&mut params) |
22 | .visit::<ast::SourceFile, _>(process) | 22 | .visit::<ast::SourceFile, _>(process) |
23 | .visit::<ast::ItemList, _>(process) | 23 | .visit::<ast::ItemList, _>(process) |
diff --git a/crates/ra_ide_api/src/completion/complete_keyword.rs b/crates/ra_ide_api/src/completion/complete_keyword.rs index 841c0c554..718b83418 100644 --- a/crates/ra_ide_api/src/completion/complete_keyword.rs +++ b/crates/ra_ide_api/src/completion/complete_keyword.rs | |||
@@ -2,7 +2,7 @@ use ra_syntax::{ | |||
2 | algo::visit::{visitor, Visitor}, | 2 | algo::visit::{visitor, Visitor}, |
3 | AstNode, | 3 | AstNode, |
4 | ast::{self, LoopBodyOwner}, | 4 | ast::{self, LoopBodyOwner}, |
5 | SyntaxKind::*, SyntaxNode, | 5 | SyntaxKind::*, SyntaxToken, |
6 | }; | 6 | }; |
7 | 7 | ||
8 | use crate::completion::{CompletionContext, CompletionItem, Completions, CompletionKind, CompletionItemKind}; | 8 | use crate::completion::{CompletionContext, CompletionItem, Completions, CompletionKind, CompletionItemKind}; |
@@ -62,7 +62,7 @@ pub(super) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte | |||
62 | acc.add(keyword(ctx, "else", "else {$0}")); | 62 | acc.add(keyword(ctx, "else", "else {$0}")); |
63 | acc.add(keyword(ctx, "else if", "else if $0 {}")); | 63 | acc.add(keyword(ctx, "else if", "else if $0 {}")); |
64 | } | 64 | } |
65 | if is_in_loop_body(ctx.leaf) { | 65 | if is_in_loop_body(ctx.token) { |
66 | if ctx.can_be_stmt { | 66 | if ctx.can_be_stmt { |
67 | acc.add(keyword(ctx, "continue", "continue;")); | 67 | acc.add(keyword(ctx, "continue", "continue;")); |
68 | acc.add(keyword(ctx, "break", "break;")); | 68 | acc.add(keyword(ctx, "break", "break;")); |
@@ -74,8 +74,8 @@ pub(super) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte | |||
74 | acc.add_all(complete_return(ctx, fn_def, ctx.can_be_stmt)); | 74 | acc.add_all(complete_return(ctx, fn_def, ctx.can_be_stmt)); |
75 | } | 75 | } |
76 | 76 | ||
77 | fn is_in_loop_body(leaf: &SyntaxNode) -> bool { | 77 | fn is_in_loop_body(leaf: SyntaxToken) -> bool { |
78 | for node in leaf.ancestors() { | 78 | for node in leaf.parent().ancestors() { |
79 | if node.kind() == FN_DEF || node.kind() == LAMBDA_EXPR { | 79 | if node.kind() == FN_DEF || node.kind() == LAMBDA_EXPR { |
80 | break; | 80 | break; |
81 | } | 81 | } |
diff --git a/crates/ra_ide_api/src/completion/complete_path.rs b/crates/ra_ide_api/src/completion/complete_path.rs index 5ff1b9927..e54fe7b7e 100644 --- a/crates/ra_ide_api/src/completion/complete_path.rs +++ b/crates/ra_ide_api/src/completion/complete_path.rs | |||
@@ -19,11 +19,14 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) { | |||
19 | for (name, res) in module_scope.entries() { | 19 | for (name, res) in module_scope.entries() { |
20 | if Some(module) == ctx.module { | 20 | if Some(module) == ctx.module { |
21 | if let Some(import) = res.import { | 21 | if let Some(import) = res.import { |
22 | let path = module.import_source(ctx.db, import); | 22 | if let hir::ImportSource::UseTree(tree) = |
23 | if path.syntax().range().contains_inclusive(ctx.offset) { | 23 | module.import_source(ctx.db, import) |
24 | // for `use self::foo<|>`, don't suggest `foo` as a completion | 24 | { |
25 | tested_by!(dont_complete_current_use); | 25 | if tree.syntax().range().contains_inclusive(ctx.offset) { |
26 | continue; | 26 | // for `use self::foo<|>`, don't suggest `foo` as a completion |
27 | tested_by!(dont_complete_current_use); | ||
28 | continue; | ||
29 | } | ||
27 | } | 30 | } |
28 | } | 31 | } |
29 | } | 32 | } |
@@ -73,6 +76,18 @@ mod tests { | |||
73 | } | 76 | } |
74 | 77 | ||
75 | #[test] | 78 | #[test] |
79 | fn dont_complete_current_use_in_braces_with_glob() { | ||
80 | let completions = do_completion( | ||
81 | r" | ||
82 | mod foo { pub struct S; } | ||
83 | use self::{foo::*, bar<|>}; | ||
84 | ", | ||
85 | CompletionKind::Reference, | ||
86 | ); | ||
87 | assert_eq!(completions.len(), 2); | ||
88 | } | ||
89 | |||
90 | #[test] | ||
76 | fn completes_mod_with_docs() { | 91 | fn completes_mod_with_docs() { |
77 | check_reference_completion( | 92 | check_reference_completion( |
78 | "mod_with_docs", | 93 | "mod_with_docs", |
diff --git a/crates/ra_ide_api/src/completion/completion_context.rs b/crates/ra_ide_api/src/completion/completion_context.rs index 724d0dfbf..65dffa470 100644 --- a/crates/ra_ide_api/src/completion/completion_context.rs +++ b/crates/ra_ide_api/src/completion/completion_context.rs | |||
@@ -1,8 +1,8 @@ | |||
1 | use ra_text_edit::AtomTextEdit; | 1 | use ra_text_edit::AtomTextEdit; |
2 | use ra_syntax::{ | 2 | use ra_syntax::{ |
3 | AstNode, SyntaxNode, SourceFile, TextUnit, TextRange, | 3 | AstNode, SyntaxNode, SourceFile, TextUnit, TextRange, SyntaxToken, |
4 | ast, | 4 | ast, |
5 | algo::{find_leaf_at_offset, find_covering_node, find_node_at_offset}, | 5 | algo::{find_token_at_offset, find_covering_element, find_node_at_offset}, |
6 | SyntaxKind::*, | 6 | SyntaxKind::*, |
7 | }; | 7 | }; |
8 | use hir::{source_binder, Resolver}; | 8 | use hir::{source_binder, Resolver}; |
@@ -15,7 +15,7 @@ use crate::{db, FilePosition}; | |||
15 | pub(crate) struct CompletionContext<'a> { | 15 | pub(crate) struct CompletionContext<'a> { |
16 | pub(super) db: &'a db::RootDatabase, | 16 | pub(super) db: &'a db::RootDatabase, |
17 | pub(super) offset: TextUnit, | 17 | pub(super) offset: TextUnit, |
18 | pub(super) leaf: &'a SyntaxNode, | 18 | pub(super) token: SyntaxToken<'a>, |
19 | pub(super) resolver: Resolver, | 19 | pub(super) resolver: Resolver, |
20 | pub(super) module: Option<hir::Module>, | 20 | pub(super) module: Option<hir::Module>, |
21 | pub(super) function: Option<hir::Function>, | 21 | pub(super) function: Option<hir::Function>, |
@@ -49,10 +49,10 @@ impl<'a> CompletionContext<'a> { | |||
49 | ) -> Option<CompletionContext<'a>> { | 49 | ) -> Option<CompletionContext<'a>> { |
50 | let resolver = source_binder::resolver_for_position(db, position); | 50 | let resolver = source_binder::resolver_for_position(db, position); |
51 | let module = source_binder::module_from_position(db, position); | 51 | let module = source_binder::module_from_position(db, position); |
52 | let leaf = find_leaf_at_offset(original_file.syntax(), position.offset).left_biased()?; | 52 | let token = find_token_at_offset(original_file.syntax(), position.offset).left_biased()?; |
53 | let mut ctx = CompletionContext { | 53 | let mut ctx = CompletionContext { |
54 | db, | 54 | db, |
55 | leaf, | 55 | token, |
56 | offset: position.offset, | 56 | offset: position.offset, |
57 | resolver, | 57 | resolver, |
58 | module, | 58 | module, |
@@ -76,9 +76,9 @@ impl<'a> CompletionContext<'a> { | |||
76 | 76 | ||
77 | // The range of the identifier that is being completed. | 77 | // The range of the identifier that is being completed. |
78 | pub(crate) fn source_range(&self) -> TextRange { | 78 | pub(crate) fn source_range(&self) -> TextRange { |
79 | match self.leaf.kind() { | 79 | match self.token.kind() { |
80 | // workaroud when completion is triggered by trigger characters. | 80 | // workaroud when completion is triggered by trigger characters. |
81 | IDENT => self.leaf.range(), | 81 | IDENT => self.token.range(), |
82 | _ => TextRange::offset_len(self.offset, 0.into()), | 82 | _ => TextRange::offset_len(self.offset, 0.into()), |
83 | } | 83 | } |
84 | } | 84 | } |
@@ -139,10 +139,11 @@ impl<'a> CompletionContext<'a> { | |||
139 | _ => (), | 139 | _ => (), |
140 | } | 140 | } |
141 | 141 | ||
142 | self.use_item_syntax = self.leaf.ancestors().find_map(ast::UseItem::cast); | 142 | self.use_item_syntax = self.token.parent().ancestors().find_map(ast::UseItem::cast); |
143 | 143 | ||
144 | self.function_syntax = self | 144 | self.function_syntax = self |
145 | .leaf | 145 | .token |
146 | .parent() | ||
146 | .ancestors() | 147 | .ancestors() |
147 | .take_while(|it| it.kind() != SOURCE_FILE && it.kind() != MODULE) | 148 | .take_while(|it| it.kind() != SOURCE_FILE && it.kind() != MODULE) |
148 | .find_map(ast::FnDef::cast); | 149 | .find_map(ast::FnDef::cast); |
@@ -224,8 +225,7 @@ impl<'a> CompletionContext<'a> { | |||
224 | } | 225 | } |
225 | 226 | ||
226 | fn find_node_with_range<N: AstNode>(syntax: &SyntaxNode, range: TextRange) -> Option<&N> { | 227 | fn find_node_with_range<N: AstNode>(syntax: &SyntaxNode, range: TextRange) -> Option<&N> { |
227 | let node = find_covering_node(syntax, range); | 228 | find_covering_element(syntax, range).ancestors().find_map(N::cast) |
228 | node.ancestors().find_map(N::cast) | ||
229 | } | 229 | } |
230 | 230 | ||
231 | fn is_node<N: AstNode>(node: &SyntaxNode) -> bool { | 231 | fn is_node<N: AstNode>(node: &SyntaxNode) -> bool { |
diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__enum_variant_with_details.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__enum_variant_with_details.snap index 70ea96e1b..daccd9fba 100644 --- a/crates/ra_ide_api/src/completion/snapshots/completion_item__enum_variant_with_details.snap +++ b/crates/ra_ide_api/src/completion/snapshots/completion_item__enum_variant_with_details.snap | |||
@@ -1,6 +1,6 @@ | |||
1 | --- | 1 | --- |
2 | created: "2019-02-18T09:22:24.062138085Z" | 2 | created: "2019-04-02T07:43:12.954637543Z" |
3 | creator: insta@0.6.2 | 3 | creator: insta@0.7.4 |
4 | source: crates/ra_ide_api/src/completion/completion_item.rs | 4 | source: crates/ra_ide_api/src/completion/completion_item.rs |
5 | expression: kind_completions | 5 | expression: kind_completions |
6 | --- | 6 | --- |
@@ -33,6 +33,9 @@ expression: kind_completions | |||
33 | delete: [180; 180), | 33 | delete: [180; 180), |
34 | insert: "S", | 34 | insert: "S", |
35 | kind: EnumVariant, | 35 | kind: EnumVariant, |
36 | detail: "(S)" | 36 | detail: "(S)", |
37 | documentation: Documentation( | ||
38 | "" | ||
39 | ) | ||
37 | } | 40 | } |
38 | ] | 41 | ] |