aboutsummaryrefslogtreecommitdiff
path: root/crates/completion/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/completion/src')
-rw-r--r--crates/completion/src/completions/pattern.rs57
-rw-r--r--crates/completion/src/completions/record.rs4
-rw-r--r--crates/completion/src/completions/unqualified_path.rs60
-rw-r--r--crates/completion/src/config.rs4
-rw-r--r--crates/completion/src/context.rs3
-rw-r--r--crates/completion/src/item.rs38
-rw-r--r--crates/completion/src/lib.rs7
-rw-r--r--crates/completion/src/render.rs26
-rw-r--r--crates/completion/src/render/enum_variant.rs15
-rw-r--r--crates/completion/src/render/function.rs17
-rw-r--r--crates/completion/src/render/macro_.rs17
11 files changed, 150 insertions, 98 deletions
diff --git a/crates/completion/src/completions/pattern.rs b/crates/completion/src/completions/pattern.rs
index 7ab7f09fe..4f63ff0ef 100644
--- a/crates/completion/src/completions/pattern.rs
+++ b/crates/completion/src/completions/pattern.rs
@@ -4,7 +4,7 @@ use crate::{CompletionContext, Completions};
4 4
5/// Completes constats and paths in patterns. 5/// Completes constats and paths in patterns.
6pub(crate) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) { 6pub(crate) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) {
7 if !ctx.is_pat_binding_or_const { 7 if !(ctx.is_pat_binding_or_const || ctx.is_irrefutable_let_pat_binding) {
8 return; 8 return;
9 } 9 }
10 if ctx.record_pat_syntax.is_some() { 10 if ctx.record_pat_syntax.is_some() {
@@ -14,20 +14,27 @@ pub(crate) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) {
14 // FIXME: ideally, we should look at the type we are matching against and 14 // FIXME: ideally, we should look at the type we are matching against and
15 // suggest variants + auto-imports 15 // suggest variants + auto-imports
16 ctx.scope.process_all_names(&mut |name, res| { 16 ctx.scope.process_all_names(&mut |name, res| {
17 match &res { 17 let add_resolution = match &res {
18 hir::ScopeDef::ModuleDef(def) => match def { 18 hir::ScopeDef::ModuleDef(def) => {
19 hir::ModuleDef::Adt(hir::Adt::Enum(..)) 19 if ctx.is_irrefutable_let_pat_binding {
20 | hir::ModuleDef::Adt(hir::Adt::Struct(..)) 20 matches!(def, hir::ModuleDef::Adt(hir::Adt::Struct(_)))
21 | hir::ModuleDef::EnumVariant(..) 21 } else {
22 | hir::ModuleDef::Const(..) 22 matches!(
23 | hir::ModuleDef::Module(..) => (), 23 def,
24 _ => return, 24 hir::ModuleDef::Adt(hir::Adt::Enum(..))
25 }, 25 | hir::ModuleDef::Adt(hir::Adt::Struct(..))
26 hir::ScopeDef::MacroDef(_) => (), 26 | hir::ModuleDef::EnumVariant(..)
27 _ => return, 27 | hir::ModuleDef::Const(..)
28 | hir::ModuleDef::Module(..)
29 )
30 }
31 }
32 hir::ScopeDef::MacroDef(_) => true,
33 _ => false,
28 }; 34 };
29 35 if add_resolution {
30 acc.add_resolution(ctx, name.to_string(), &res) 36 acc.add_resolution(ctx, name.to_string(), &res);
37 }
31 }); 38 });
32} 39}
33 40
@@ -85,4 +92,26 @@ fn foo() {
85 "#]], 92 "#]],
86 ); 93 );
87 } 94 }
95
96 #[test]
97 fn completes_in_irrefutable_let() {
98 check(
99 r#"
100enum E { X }
101use self::E::X;
102const Z: E = E::X;
103mod m {}
104
105static FOO: E = E::X;
106struct Bar { f: u32 }
107
108fn foo() {
109 let <|>
110}
111"#,
112 expect![[r#"
113 st Bar
114 "#]],
115 );
116 }
88} 117}
diff --git a/crates/completion/src/completions/record.rs b/crates/completion/src/completions/record.rs
index 2049b9d09..eaa44c97d 100644
--- a/crates/completion/src/completions/record.rs
+++ b/crates/completion/src/completions/record.rs
@@ -1,5 +1,5 @@
1//! Complete fields in record literals and patterns. 1//! Complete fields in record literals and patterns.
2use assists::utils::FamousDefs; 2use ide_db::helpers::FamousDefs;
3use syntax::ast::Expr; 3use syntax::ast::Expr;
4 4
5use crate::{ 5use crate::{
@@ -45,8 +45,8 @@ pub(crate) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) ->
45 45
46#[cfg(test)] 46#[cfg(test)]
47mod tests { 47mod tests {
48 use assists::utils::FamousDefs;
49 use expect_test::{expect, Expect}; 48 use expect_test::{expect, Expect};
49 use ide_db::helpers::FamousDefs;
50 50
51 use crate::{test_utils::completion_list, CompletionKind}; 51 use crate::{test_utils::completion_list, CompletionKind};
52 52
diff --git a/crates/completion/src/completions/unqualified_path.rs b/crates/completion/src/completions/unqualified_path.rs
index 86c143b63..81691cd7f 100644
--- a/crates/completion/src/completions/unqualified_path.rs
+++ b/crates/completion/src/completions/unqualified_path.rs
@@ -1,8 +1,8 @@
1//! Completion of names from the current scope, e.g. locals and imported items. 1//! Completion of names from the current scope, e.g. locals and imported items.
2 2
3use assists::utils::ImportScope;
4use either::Either; 3use either::Either;
5use hir::{Adt, ModuleDef, ScopeDef, Type}; 4use hir::{Adt, ModuleDef, ScopeDef, Type};
5use ide_db::helpers::insert_use::ImportScope;
6use ide_db::imports_locator; 6use ide_db::imports_locator;
7use syntax::AstNode; 7use syntax::AstNode;
8use test_utils::mark; 8use test_utils::mark;
@@ -44,7 +44,9 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
44 acc.add_resolution(ctx, name.to_string(), &res) 44 acc.add_resolution(ctx, name.to_string(), &res)
45 }); 45 });
46 46
47 fuzzy_completion(acc, ctx).unwrap_or_default() 47 if ctx.config.enable_experimental_completions {
48 fuzzy_completion(acc, ctx).unwrap_or_default()
49 }
48} 50}
49 51
50fn complete_enum_variants(acc: &mut Completions, ctx: &CompletionContext, ty: &Type) { 52fn complete_enum_variants(acc: &mut Completions, ctx: &CompletionContext, ty: &Type) {
@@ -79,32 +81,34 @@ fn fuzzy_completion(acc: &mut Completions, ctx: &CompletionContext) -> Option<()
79 81
80 let potential_import_name = ctx.token.to_string(); 82 let potential_import_name = ctx.token.to_string();
81 83
82 let possible_imports = 84 let possible_imports = imports_locator::find_similar_imports(
83 imports_locator::find_similar_imports(&ctx.sema, ctx.krate?, &potential_import_name, 400) 85 &ctx.sema,
84 .filter_map(|import_candidate| match import_candidate { 86 ctx.krate?,
85 // when completing outside the use declaration, modules are pretty useless 87 &potential_import_name,
86 // and tend to bloat the completion suggestions a lot 88 50,
87 Either::Left(ModuleDef::Module(_)) => None, 89 true,
88 Either::Left(module_def) => Some(( 90 )
89 current_module.find_use_path(ctx.db, module_def)?, 91 .filter_map(|import_candidate| {
90 ScopeDef::ModuleDef(module_def), 92 Some(match import_candidate {
91 )), 93 Either::Left(module_def) => {
92 Either::Right(macro_def) => Some(( 94 (current_module.find_use_path(ctx.db, module_def)?, ScopeDef::ModuleDef(module_def))
93 current_module.find_use_path(ctx.db, macro_def)?, 95 }
94 ScopeDef::MacroDef(macro_def), 96 Either::Right(macro_def) => {
95 )), 97 (current_module.find_use_path(ctx.db, macro_def)?, ScopeDef::MacroDef(macro_def))
96 }) 98 }
97 .filter(|(mod_path, _)| mod_path.len() > 1) 99 })
98 .filter_map(|(import_path, definition)| { 100 })
99 render_resolution_with_import( 101 .filter(|(mod_path, _)| mod_path.len() > 1)
100 RenderContext::new(ctx), 102 .take(20)
101 import_path.clone(), 103 .filter_map(|(import_path, definition)| {
102 import_scope.clone(), 104 render_resolution_with_import(
103 ctx.config.merge, 105 RenderContext::new(ctx),
104 &definition, 106 import_path.clone(),
105 ) 107 import_scope.clone(),
106 }) 108 ctx.config.merge,
107 .take(20); 109 &definition,
110 )
111 });
108 112
109 acc.add_all(possible_imports); 113 acc.add_all(possible_imports);
110 Some(()) 114 Some(())
diff --git a/crates/completion/src/config.rs b/crates/completion/src/config.rs
index 82874ff25..654a76f7b 100644
--- a/crates/completion/src/config.rs
+++ b/crates/completion/src/config.rs
@@ -4,11 +4,12 @@
4//! module, and we use to statically check that we only produce snippet 4//! module, and we use to statically check that we only produce snippet
5//! completions if we are allowed to. 5//! completions if we are allowed to.
6 6
7use assists::utils::MergeBehaviour; 7use ide_db::helpers::insert_use::MergeBehaviour;
8 8
9#[derive(Clone, Debug, PartialEq, Eq)] 9#[derive(Clone, Debug, PartialEq, Eq)]
10pub struct CompletionConfig { 10pub struct CompletionConfig {
11 pub enable_postfix_completions: bool, 11 pub enable_postfix_completions: bool,
12 pub enable_experimental_completions: bool,
12 pub add_call_parenthesis: bool, 13 pub add_call_parenthesis: bool,
13 pub add_call_argument_snippets: bool, 14 pub add_call_argument_snippets: bool,
14 pub snippet_cap: Option<SnippetCap>, 15 pub snippet_cap: Option<SnippetCap>,
@@ -30,6 +31,7 @@ impl Default for CompletionConfig {
30 fn default() -> Self { 31 fn default() -> Self {
31 CompletionConfig { 32 CompletionConfig {
32 enable_postfix_completions: true, 33 enable_postfix_completions: true,
34 enable_experimental_completions: true,
33 add_call_parenthesis: true, 35 add_call_parenthesis: true,
34 add_call_argument_snippets: true, 36 add_call_argument_snippets: true,
35 snippet_cap: Some(SnippetCap { _private: () }), 37 snippet_cap: Some(SnippetCap { _private: () }),
diff --git a/crates/completion/src/context.rs b/crates/completion/src/context.rs
index bf70ee478..5cd11cf77 100644
--- a/crates/completion/src/context.rs
+++ b/crates/completion/src/context.rs
@@ -51,6 +51,7 @@ pub(crate) struct CompletionContext<'a> {
51 /// If a name-binding or reference to a const in a pattern. 51 /// If a name-binding or reference to a const in a pattern.
52 /// Irrefutable patterns (like let) are excluded. 52 /// Irrefutable patterns (like let) are excluded.
53 pub(super) is_pat_binding_or_const: bool, 53 pub(super) is_pat_binding_or_const: bool,
54 pub(super) is_irrefutable_let_pat_binding: bool,
54 /// A single-indent path, like `foo`. `::foo` should not be considered a trivial path. 55 /// A single-indent path, like `foo`. `::foo` should not be considered a trivial path.
55 pub(super) is_trivial_path: bool, 56 pub(super) is_trivial_path: bool,
56 /// If not a trivial path, the prefix (qualifier). 57 /// If not a trivial path, the prefix (qualifier).
@@ -146,6 +147,7 @@ impl<'a> CompletionContext<'a> {
146 active_parameter: ActiveParameter::at(db, position), 147 active_parameter: ActiveParameter::at(db, position),
147 is_param: false, 148 is_param: false,
148 is_pat_binding_or_const: false, 149 is_pat_binding_or_const: false,
150 is_irrefutable_let_pat_binding: false,
149 is_trivial_path: false, 151 is_trivial_path: false,
150 path_qual: None, 152 path_qual: None,
151 after_if: false, 153 after_if: false,
@@ -330,6 +332,7 @@ impl<'a> CompletionContext<'a> {
330 if pat.syntax().text_range().contains_range(bind_pat.syntax().text_range()) 332 if pat.syntax().text_range().contains_range(bind_pat.syntax().text_range())
331 { 333 {
332 self.is_pat_binding_or_const = false; 334 self.is_pat_binding_or_const = false;
335 self.is_irrefutable_let_pat_binding = true;
333 } 336 }
334 } 337 }
335 } 338 }
diff --git a/crates/completion/src/item.rs b/crates/completion/src/item.rs
index b13c3f376..e85549fef 100644
--- a/crates/completion/src/item.rs
+++ b/crates/completion/src/item.rs
@@ -2,8 +2,11 @@
2 2
3use std::fmt; 3use std::fmt;
4 4
5use assists::utils::{insert_use, mod_path_to_ast, ImportScope, MergeBehaviour};
6use hir::{Documentation, ModPath, Mutability}; 5use hir::{Documentation, ModPath, Mutability};
6use ide_db::helpers::{
7 insert_use::{self, ImportScope, MergeBehaviour},
8 mod_path_to_ast,
9};
7use syntax::{algo, TextRange}; 10use syntax::{algo, TextRange};
8use text_edit::TextEdit; 11use text_edit::TextEdit;
9 12
@@ -201,7 +204,7 @@ impl CompletionItem {
201 trigger_call_info: None, 204 trigger_call_info: None,
202 score: None, 205 score: None,
203 ref_match: None, 206 ref_match: None,
204 import_data: None, 207 import_to_add: None,
205 } 208 }
206 } 209 }
207 210
@@ -255,13 +258,21 @@ impl CompletionItem {
255 } 258 }
256} 259}
257 260
261/// An extra import to add after the completion is applied.
262#[derive(Clone)]
263pub(crate) struct ImportToAdd {
264 pub(crate) import_path: ModPath,
265 pub(crate) import_scope: ImportScope,
266 pub(crate) merge_behaviour: Option<MergeBehaviour>,
267}
268
258/// A helper to make `CompletionItem`s. 269/// A helper to make `CompletionItem`s.
259#[must_use] 270#[must_use]
260#[derive(Clone)] 271#[derive(Clone)]
261pub(crate) struct Builder { 272pub(crate) struct Builder {
262 source_range: TextRange, 273 source_range: TextRange,
263 completion_kind: CompletionKind, 274 completion_kind: CompletionKind,
264 import_data: Option<(ModPath, ImportScope, Option<MergeBehaviour>)>, 275 import_to_add: Option<ImportToAdd>,
265 label: String, 276 label: String,
266 insert_text: Option<String>, 277 insert_text: Option<String>,
267 insert_text_format: InsertTextFormat, 278 insert_text_format: InsertTextFormat,
@@ -278,14 +289,16 @@ pub(crate) struct Builder {
278 289
279impl Builder { 290impl Builder {
280 pub(crate) fn build(self) -> CompletionItem { 291 pub(crate) fn build(self) -> CompletionItem {
292 let _p = profile::span("item::Builder::build");
293
281 let mut label = self.label; 294 let mut label = self.label;
282 let mut lookup = self.lookup; 295 let mut lookup = self.lookup;
283 let mut insert_text = self.insert_text; 296 let mut insert_text = self.insert_text;
284 let mut text_edits = TextEdit::builder(); 297 let mut text_edits = TextEdit::builder();
285 298
286 if let Some((import_path, import_scope, merge_behaviour)) = self.import_data { 299 if let Some(import_data) = self.import_to_add {
287 let import = mod_path_to_ast(&import_path); 300 let import = mod_path_to_ast(&import_data.import_path);
288 let mut import_path_without_last_segment = import_path; 301 let mut import_path_without_last_segment = import_data.import_path;
289 let _ = import_path_without_last_segment.segments.pop(); 302 let _ = import_path_without_last_segment.segments.pop();
290 303
291 if !import_path_without_last_segment.segments.is_empty() { 304 if !import_path_without_last_segment.segments.is_empty() {
@@ -298,7 +311,11 @@ impl Builder {
298 label = format!("{}::{}", import_path_without_last_segment, label); 311 label = format!("{}::{}", import_path_without_last_segment, label);
299 } 312 }
300 313
301 let rewriter = insert_use(&import_scope, import, merge_behaviour); 314 let rewriter = insert_use::insert_use(
315 &import_data.import_scope,
316 import,
317 import_data.merge_behaviour,
318 );
302 if let Some(old_ast) = rewriter.rewrite_root() { 319 if let Some(old_ast) = rewriter.rewrite_root() {
303 algo::diff(&old_ast, &rewriter.rewrite(&old_ast)).into_text_edit(&mut text_edits); 320 algo::diff(&old_ast, &rewriter.rewrite(&old_ast)).into_text_edit(&mut text_edits);
304 } 321 }
@@ -390,11 +407,8 @@ impl Builder {
390 self.trigger_call_info = Some(true); 407 self.trigger_call_info = Some(true);
391 self 408 self
392 } 409 }
393 pub(crate) fn import_data( 410 pub(crate) fn add_import(mut self, import_to_add: Option<ImportToAdd>) -> Builder {
394 mut self, 411 self.import_to_add = import_to_add;
395 import_data: Option<(ModPath, ImportScope, Option<MergeBehaviour>)>,
396 ) -> Builder {
397 self.import_data = import_data;
398 self 412 self
399 } 413 }
400 pub(crate) fn set_ref_match( 414 pub(crate) fn set_ref_match(
diff --git a/crates/completion/src/lib.rs b/crates/completion/src/lib.rs
index ac57683fb..1ec2e9be7 100644
--- a/crates/completion/src/lib.rs
+++ b/crates/completion/src/lib.rs
@@ -69,6 +69,13 @@ pub use crate::{
69// fn test_name() {} 69// fn test_name() {}
70// } 70// }
71// ``` 71// ```
72//
73// And experimental completions, enabled with the `rust-analyzer.completion.enableExperimental` setting.
74// This flag enables or disables:
75//
76// - Auto import: additional completion options with automatic `use` import and options from all project importable items, matched for the input
77//
78// Experimental completions might cause issues with performance and completion list look.
72 79
73/// Main entry point for completion. We run completion as a two-phase process. 80/// Main entry point for completion. We run completion as a two-phase process.
74/// 81///
diff --git a/crates/completion/src/render.rs b/crates/completion/src/render.rs
index e892d4de8..504757a6a 100644
--- a/crates/completion/src/render.rs
+++ b/crates/completion/src/render.rs
@@ -9,15 +9,15 @@ pub(crate) mod type_alias;
9 9
10mod builder_ext; 10mod builder_ext;
11 11
12use assists::utils::{ImportScope, MergeBehaviour};
13use hir::{Documentation, HasAttrs, HirDisplay, ModPath, Mutability, ScopeDef, Type}; 12use hir::{Documentation, HasAttrs, HirDisplay, ModPath, Mutability, ScopeDef, Type};
13use ide_db::helpers::insert_use::{ImportScope, MergeBehaviour};
14use ide_db::RootDatabase; 14use ide_db::RootDatabase;
15use syntax::TextRange; 15use syntax::TextRange;
16use test_utils::mark; 16use test_utils::mark;
17 17
18use crate::{ 18use crate::{
19 config::SnippetCap, CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, 19 config::SnippetCap, item::ImportToAdd, CompletionContext, CompletionItem, CompletionItemKind,
20 CompletionScore, 20 CompletionKind, CompletionScore,
21}; 21};
22 22
23use crate::render::{enum_variant::render_enum_variant, function::render_fn, macro_::render_macro}; 23use crate::render::{enum_variant::render_enum_variant, function::render_fn, macro_::render_macro};
@@ -48,15 +48,15 @@ pub(crate) fn render_resolution<'a>(
48 48
49pub(crate) fn render_resolution_with_import<'a>( 49pub(crate) fn render_resolution_with_import<'a>(
50 ctx: RenderContext<'a>, 50 ctx: RenderContext<'a>,
51 import: ModPath, 51 import_path: ModPath,
52 import_scope: ImportScope, 52 import_scope: ImportScope,
53 merge_behaviour: Option<MergeBehaviour>, 53 merge_behaviour: Option<MergeBehaviour>,
54 resolution: &ScopeDef, 54 resolution: &ScopeDef,
55) -> Option<CompletionItem> { 55) -> Option<CompletionItem> {
56 let local_name = import.segments.last()?.to_string(); 56 let local_name = import_path.segments.last()?.to_string();
57 Render::new(ctx).render_resolution( 57 Render::new(ctx).render_resolution(
58 local_name, 58 local_name,
59 Some((import, import_scope, merge_behaviour)), 59 Some(ImportToAdd { import_path, import_scope, merge_behaviour }),
60 resolution, 60 resolution,
61 ) 61 )
62} 62}
@@ -147,9 +147,10 @@ impl<'a> Render<'a> {
147 fn render_resolution( 147 fn render_resolution(
148 self, 148 self,
149 local_name: String, 149 local_name: String,
150 import_data: Option<(ModPath, ImportScope, Option<MergeBehaviour>)>, 150 import_to_add: Option<ImportToAdd>,
151 resolution: &ScopeDef, 151 resolution: &ScopeDef,
152 ) -> Option<CompletionItem> { 152 ) -> Option<CompletionItem> {
153 let _p = profile::span("render_resolution");
153 use hir::ModuleDef::*; 154 use hir::ModuleDef::*;
154 155
155 let completion_kind = match resolution { 156 let completion_kind = match resolution {
@@ -159,15 +160,16 @@ impl<'a> Render<'a> {
159 160
160 let kind = match resolution { 161 let kind = match resolution {
161 ScopeDef::ModuleDef(Function(func)) => { 162 ScopeDef::ModuleDef(Function(func)) => {
162 let item = render_fn(self.ctx, import_data, Some(local_name), *func); 163 let item = render_fn(self.ctx, import_to_add, Some(local_name), *func);
163 return Some(item); 164 return Some(item);
164 } 165 }
165 ScopeDef::ModuleDef(EnumVariant(var)) => { 166 ScopeDef::ModuleDef(EnumVariant(var)) => {
166 let item = render_enum_variant(self.ctx, import_data, Some(local_name), *var, None); 167 let item =
168 render_enum_variant(self.ctx, import_to_add, Some(local_name), *var, None);
167 return Some(item); 169 return Some(item);
168 } 170 }
169 ScopeDef::MacroDef(mac) => { 171 ScopeDef::MacroDef(mac) => {
170 let item = render_macro(self.ctx, import_data, local_name, *mac); 172 let item = render_macro(self.ctx, import_to_add, local_name, *mac);
171 return item; 173 return item;
172 } 174 }
173 175
@@ -192,7 +194,7 @@ impl<'a> Render<'a> {
192 local_name, 194 local_name,
193 ) 195 )
194 .kind(CompletionItemKind::UnresolvedReference) 196 .kind(CompletionItemKind::UnresolvedReference)
195 .import_data(import_data) 197 .add_import(import_to_add)
196 .build(); 198 .build();
197 return Some(item); 199 return Some(item);
198 } 200 }
@@ -247,7 +249,7 @@ impl<'a> Render<'a> {
247 249
248 let item = item 250 let item = item
249 .kind(kind) 251 .kind(kind)
250 .import_data(import_data) 252 .add_import(import_to_add)
251 .set_documentation(docs) 253 .set_documentation(docs)
252 .set_ref_match(ref_match) 254 .set_ref_match(ref_match)
253 .build(); 255 .build();
diff --git a/crates/completion/src/render/enum_variant.rs b/crates/completion/src/render/enum_variant.rs
index 6070e9b1d..f4bd02f25 100644
--- a/crates/completion/src/render/enum_variant.rs
+++ b/crates/completion/src/render/enum_variant.rs
@@ -1,23 +1,23 @@
1//! Renderer for `enum` variants. 1//! Renderer for `enum` variants.
2 2
3use assists::utils::{ImportScope, MergeBehaviour};
4use hir::{HasAttrs, HirDisplay, ModPath, StructKind}; 3use hir::{HasAttrs, HirDisplay, ModPath, StructKind};
5use itertools::Itertools; 4use itertools::Itertools;
6use test_utils::mark; 5use test_utils::mark;
7 6
8use crate::{ 7use crate::{
9 item::{CompletionItem, CompletionItemKind, CompletionKind}, 8 item::{CompletionItem, CompletionItemKind, CompletionKind, ImportToAdd},
10 render::{builder_ext::Params, RenderContext}, 9 render::{builder_ext::Params, RenderContext},
11}; 10};
12 11
13pub(crate) fn render_enum_variant<'a>( 12pub(crate) fn render_enum_variant<'a>(
14 ctx: RenderContext<'a>, 13 ctx: RenderContext<'a>,
15 import_data: Option<(ModPath, ImportScope, Option<MergeBehaviour>)>, 14 import_to_add: Option<ImportToAdd>,
16 local_name: Option<String>, 15 local_name: Option<String>,
17 variant: hir::EnumVariant, 16 variant: hir::EnumVariant,
18 path: Option<ModPath>, 17 path: Option<ModPath>,
19) -> CompletionItem { 18) -> CompletionItem {
20 EnumVariantRender::new(ctx, local_name, variant, path).render(import_data) 19 let _p = profile::span("render_enum_variant");
20 EnumVariantRender::new(ctx, local_name, variant, path).render(import_to_add)
21} 21}
22 22
23#[derive(Debug)] 23#[derive(Debug)]
@@ -62,10 +62,7 @@ impl<'a> EnumVariantRender<'a> {
62 } 62 }
63 } 63 }
64 64
65 fn render( 65 fn render(self, import_to_add: Option<ImportToAdd>) -> CompletionItem {
66 self,
67 import_data: Option<(ModPath, ImportScope, Option<MergeBehaviour>)>,
68 ) -> CompletionItem {
69 let mut builder = CompletionItem::new( 66 let mut builder = CompletionItem::new(
70 CompletionKind::Reference, 67 CompletionKind::Reference,
71 self.ctx.source_range(), 68 self.ctx.source_range(),
@@ -74,7 +71,7 @@ impl<'a> EnumVariantRender<'a> {
74 .kind(CompletionItemKind::EnumVariant) 71 .kind(CompletionItemKind::EnumVariant)
75 .set_documentation(self.variant.docs(self.ctx.db())) 72 .set_documentation(self.variant.docs(self.ctx.db()))
76 .set_deprecated(self.ctx.is_deprecated(self.variant)) 73 .set_deprecated(self.ctx.is_deprecated(self.variant))
77 .import_data(import_data) 74 .add_import(import_to_add)
78 .detail(self.detail()); 75 .detail(self.detail());
79 76
80 if self.variant_kind == StructKind::Tuple { 77 if self.variant_kind == StructKind::Tuple {
diff --git a/crates/completion/src/render/function.rs b/crates/completion/src/render/function.rs
index 9dd5cd18c..542383d7e 100644
--- a/crates/completion/src/render/function.rs
+++ b/crates/completion/src/render/function.rs
@@ -1,21 +1,21 @@
1//! Renderer for function calls. 1//! Renderer for function calls.
2 2
3use assists::utils::{ImportScope, MergeBehaviour}; 3use hir::{HasSource, Type};
4use hir::{HasSource, ModPath, Type};
5use syntax::{ast::Fn, display::function_declaration}; 4use syntax::{ast::Fn, display::function_declaration};
6 5
7use crate::{ 6use crate::{
8 item::{CompletionItem, CompletionItemKind, CompletionKind}, 7 item::{CompletionItem, CompletionItemKind, CompletionKind, ImportToAdd},
9 render::{builder_ext::Params, RenderContext}, 8 render::{builder_ext::Params, RenderContext},
10}; 9};
11 10
12pub(crate) fn render_fn<'a>( 11pub(crate) fn render_fn<'a>(
13 ctx: RenderContext<'a>, 12 ctx: RenderContext<'a>,
14 import_data: Option<(ModPath, ImportScope, Option<MergeBehaviour>)>, 13 import_to_add: Option<ImportToAdd>,
15 local_name: Option<String>, 14 local_name: Option<String>,
16 fn_: hir::Function, 15 fn_: hir::Function,
17) -> CompletionItem { 16) -> CompletionItem {
18 FunctionRender::new(ctx, local_name, fn_).render(import_data) 17 let _p = profile::span("render_fn");
18 FunctionRender::new(ctx, local_name, fn_).render(import_to_add)
19} 19}
20 20
21#[derive(Debug)] 21#[derive(Debug)]
@@ -38,10 +38,7 @@ impl<'a> FunctionRender<'a> {
38 FunctionRender { ctx, name, fn_, ast_node } 38 FunctionRender { ctx, name, fn_, ast_node }
39 } 39 }
40 40
41 fn render( 41 fn render(self, import_to_add: Option<ImportToAdd>) -> CompletionItem {
42 self,
43 import_data: Option<(ModPath, ImportScope, Option<MergeBehaviour>)>,
44 ) -> CompletionItem {
45 let params = self.params(); 42 let params = self.params();
46 CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), self.name.clone()) 43 CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), self.name.clone())
47 .kind(self.kind()) 44 .kind(self.kind())
@@ -49,7 +46,7 @@ impl<'a> FunctionRender<'a> {
49 .set_deprecated(self.ctx.is_deprecated(self.fn_)) 46 .set_deprecated(self.ctx.is_deprecated(self.fn_))
50 .detail(self.detail()) 47 .detail(self.detail())
51 .add_call_parens(self.ctx.completion, self.name, params) 48 .add_call_parens(self.ctx.completion, self.name, params)
52 .import_data(import_data) 49 .add_import(import_to_add)
53 .build() 50 .build()
54 } 51 }
55 52
diff --git a/crates/completion/src/render/macro_.rs b/crates/completion/src/render/macro_.rs
index fead59e41..b4ab32c6e 100644
--- a/crates/completion/src/render/macro_.rs
+++ b/crates/completion/src/render/macro_.rs
@@ -1,22 +1,22 @@
1//! Renderer for macro invocations. 1//! Renderer for macro invocations.
2 2
3use assists::utils::{ImportScope, MergeBehaviour}; 3use hir::{Documentation, HasSource};
4use hir::{Documentation, HasSource, ModPath};
5use syntax::display::macro_label; 4use syntax::display::macro_label;
6use test_utils::mark; 5use test_utils::mark;
7 6
8use crate::{ 7use crate::{
9 item::{CompletionItem, CompletionItemKind, CompletionKind}, 8 item::{CompletionItem, CompletionItemKind, CompletionKind, ImportToAdd},
10 render::RenderContext, 9 render::RenderContext,
11}; 10};
12 11
13pub(crate) fn render_macro<'a>( 12pub(crate) fn render_macro<'a>(
14 ctx: RenderContext<'a>, 13 ctx: RenderContext<'a>,
15 import_data: Option<(ModPath, ImportScope, Option<MergeBehaviour>)>, 14 import_to_add: Option<ImportToAdd>,
16 name: String, 15 name: String,
17 macro_: hir::MacroDef, 16 macro_: hir::MacroDef,
18) -> Option<CompletionItem> { 17) -> Option<CompletionItem> {
19 MacroRender::new(ctx, name, macro_).render(import_data) 18 let _p = profile::span("render_macro");
19 MacroRender::new(ctx, name, macro_).render(import_to_add)
20} 20}
21 21
22#[derive(Debug)] 22#[derive(Debug)]
@@ -38,10 +38,7 @@ impl<'a> MacroRender<'a> {
38 MacroRender { ctx, name, macro_, docs, bra, ket } 38 MacroRender { ctx, name, macro_, docs, bra, ket }
39 } 39 }
40 40
41 fn render( 41 fn render(&self, import_to_add: Option<ImportToAdd>) -> Option<CompletionItem> {
42 &self,
43 import_data: Option<(ModPath, ImportScope, Option<MergeBehaviour>)>,
44 ) -> Option<CompletionItem> {
45 // FIXME: Currently proc-macro do not have ast-node, 42 // FIXME: Currently proc-macro do not have ast-node,
46 // such that it does not have source 43 // such that it does not have source
47 if self.macro_.is_proc_macro() { 44 if self.macro_.is_proc_macro() {
@@ -53,7 +50,7 @@ impl<'a> MacroRender<'a> {
53 .kind(CompletionItemKind::Macro) 50 .kind(CompletionItemKind::Macro)
54 .set_documentation(self.docs.clone()) 51 .set_documentation(self.docs.clone())
55 .set_deprecated(self.ctx.is_deprecated(self.macro_)) 52 .set_deprecated(self.ctx.is_deprecated(self.macro_))
56 .import_data(import_data) 53 .add_import(import_to_add)
57 .detail(self.detail()); 54 .detail(self.detail());
58 55
59 let needs_bang = self.needs_bang(); 56 let needs_bang = self.needs_bang();