aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_completion/src
diff options
context:
space:
mode:
authorKirill Bulatov <[email protected]>2021-03-03 21:55:21 +0000
committerKirill Bulatov <[email protected]>2021-03-08 21:59:20 +0000
commit24a5d3b19dfa3e076df8b7413d0cc4a547aeb7d7 (patch)
treebb49da53786891e9bfd7d063f863416753265d11 /crates/ide_completion/src
parent33c83e72b9b48177a6171fd06a26676679963a4d (diff)
Fix the completion labels and tests
Diffstat (limited to 'crates/ide_completion/src')
-rw-r--r--crates/ide_completion/src/completions/flyimport.rs34
-rw-r--r--crates/ide_completion/src/item.rs41
-rw-r--r--crates/ide_completion/src/lib.rs7
3 files changed, 48 insertions, 34 deletions
diff --git a/crates/ide_completion/src/completions/flyimport.rs b/crates/ide_completion/src/completions/flyimport.rs
index c1e3f091f..c5b3c9e27 100644
--- a/crates/ide_completion/src/completions/flyimport.rs
+++ b/crates/ide_completion/src/completions/flyimport.rs
@@ -144,7 +144,7 @@ pub(crate) fn import_on_the_fly(acc: &mut Completions, ctx: &CompletionContext)
144 .filter_map(|import| { 144 .filter_map(|import| {
145 render_resolution_with_import( 145 render_resolution_with_import(
146 RenderContext::new(ctx), 146 RenderContext::new(ctx),
147 ImportEdit { import, import_scope: import_scope.clone() }, 147 ImportEdit { import, scope: import_scope.clone() },
148 ) 148 )
149 }), 149 }),
150 ); 150 );
@@ -690,8 +690,8 @@ fn main() {
690} 690}
691"#, 691"#,
692 expect![[r#" 692 expect![[r#"
693 fn weird_function() (dep::test_mod::TestTrait) -> () DEPRECATED
694 ct SPECIAL_CONST (dep::test_mod::TestTrait) DEPRECATED 693 ct SPECIAL_CONST (dep::test_mod::TestTrait) DEPRECATED
694 fn weird_function() (dep::test_mod::TestTrait) -> () DEPRECATED
695 "#]], 695 "#]],
696 ); 696 );
697 } 697 }
@@ -807,7 +807,12 @@ fn main() {
807 bar::baz::Ite$0 807 bar::baz::Ite$0
808}"#; 808}"#;
809 809
810 check(fixture, expect![["st Item (foo::bar::baz::Item)"]]); 810 check(
811 fixture,
812 expect![[r#"
813 st foo::bar::baz::Item
814 "#]],
815 );
811 816
812 check_edit( 817 check_edit(
813 "Item", 818 "Item",
@@ -825,8 +830,7 @@ fn main() {
825 830
826 fn main() { 831 fn main() {
827 bar::baz::Item 832 bar::baz::Item
828 } 833 }"#,
829 "#,
830 ); 834 );
831 } 835 }
832 836
@@ -845,7 +849,12 @@ fn main() {
845 Item::TEST_A$0 849 Item::TEST_A$0
846}"#; 850}"#;
847 851
848 check(fixture, expect![["ct TEST_ASSOC (foo::bar::baz::Item)"]]); 852 check(
853 fixture,
854 expect![[r#"
855 ct TEST_ASSOC (foo::Item)
856 "#]],
857 );
849 858
850 check_edit( 859 check_edit(
851 "TEST_ASSOC", 860 "TEST_ASSOC",
@@ -863,8 +872,7 @@ mod foo {
863 872
864fn main() { 873fn main() {
865 Item::TEST_ASSOC 874 Item::TEST_ASSOC
866} 875}"#,
867"#,
868 ); 876 );
869 } 877 }
870 878
@@ -885,7 +893,12 @@ fn main() {
885 bar::Item::TEST_A$0 893 bar::Item::TEST_A$0
886}"#; 894}"#;
887 895
888 check(fixture, expect![["ct TEST_ASSOC (foo::bar::baz::Item)"]]); 896 check(
897 fixture,
898 expect![[r#"
899 ct TEST_ASSOC (foo::bar::Item)
900 "#]],
901 );
889 902
890 check_edit( 903 check_edit(
891 "TEST_ASSOC", 904 "TEST_ASSOC",
@@ -905,8 +918,7 @@ mod foo {
905 918
906fn main() { 919fn main() {
907 bar::Item::TEST_ASSOC 920 bar::Item::TEST_ASSOC
908} 921}"#,
909"#,
910 ); 922 );
911 } 923 }
912} 924}
diff --git a/crates/ide_completion/src/item.rs b/crates/ide_completion/src/item.rs
index d01620500..44e4a6dfd 100644
--- a/crates/ide_completion/src/item.rs
+++ b/crates/ide_completion/src/item.rs
@@ -11,7 +11,7 @@ use ide_db::{
11 }, 11 },
12 SymbolKind, 12 SymbolKind,
13}; 13};
14use stdx::{impl_from, never}; 14use stdx::{format_to, impl_from, never};
15use syntax::{algo, TextRange}; 15use syntax::{algo, TextRange};
16use text_edit::TextEdit; 16use text_edit::TextEdit;
17 17
@@ -274,7 +274,7 @@ impl CompletionItem {
274#[derive(Debug, Clone)] 274#[derive(Debug, Clone)]
275pub struct ImportEdit { 275pub struct ImportEdit {
276 pub import: LocatedImport, 276 pub import: LocatedImport,
277 pub import_scope: ImportScope, 277 pub scope: ImportScope,
278} 278}
279 279
280impl ImportEdit { 280impl ImportEdit {
@@ -284,7 +284,7 @@ impl ImportEdit {
284 let _p = profile::span("ImportEdit::to_text_edit"); 284 let _p = profile::span("ImportEdit::to_text_edit");
285 285
286 let rewriter = insert_use::insert_use( 286 let rewriter = insert_use::insert_use(
287 &self.import_scope, 287 &self.scope,
288 mod_path_to_ast(&self.import.import_path), 288 mod_path_to_ast(&self.import.import_path),
289 cfg, 289 cfg,
290 ); 290 );
@@ -302,7 +302,6 @@ impl ImportEdit {
302pub(crate) struct Builder { 302pub(crate) struct Builder {
303 source_range: TextRange, 303 source_range: TextRange,
304 completion_kind: CompletionKind, 304 completion_kind: CompletionKind,
305 // TODO kb also add a db here, to resolve the completion label?
306 import_to_add: Option<ImportEdit>, 305 import_to_add: Option<ImportEdit>,
307 label: String, 306 label: String,
308 insert_text: Option<String>, 307 insert_text: Option<String>,
@@ -322,22 +321,24 @@ impl Builder {
322 pub(crate) fn build(self) -> CompletionItem { 321 pub(crate) fn build(self) -> CompletionItem {
323 let _p = profile::span("item::Builder::build"); 322 let _p = profile::span("item::Builder::build");
324 323
325 let label = self.label; 324 let mut label = self.label;
326 let lookup = self.lookup; 325 let mut lookup = self.lookup;
327 let insert_text = self.insert_text; 326 let mut insert_text = self.insert_text;
328 327
329 if let Some(_import_to_add) = self.import_to_add.as_ref() { 328 if let Some(original_path) = self
330 todo!("todo kb") 329 .import_to_add
331 // let import = &import_to_add.import; 330 .as_ref()
332 // let item_to_import = import.item_to_import(); 331 .and_then(|import_edit| import_edit.import.original_path.as_ref())
333 // lookup = lookup.or_else(|| Some(label.clone())); 332 {
334 // insert_text = insert_text.or_else(|| Some(label.clone())); 333 lookup = lookup.or_else(|| Some(label.clone()));
335 // let display_path = import_to_add.import.display_path(); 334 insert_text = insert_text.or_else(|| Some(label.clone()));
336 // if import_to_add.import { 335
337 // label = format!("{} ({})", label, display_path); 336 let original_path_label = original_path.to_string();
338 // } else { 337 if original_path_label.ends_with(&label) {
339 // label = display_path.to_string(); 338 label = original_path_label;
340 // } 339 } else {
340 format_to!(label, " ({})", original_path)
341 }
341 } 342 }
342 343
343 let text_edit = match self.text_edit { 344 let text_edit = match self.text_edit {
diff --git a/crates/ide_completion/src/lib.rs b/crates/ide_completion/src/lib.rs
index d19368de0..5470914fb 100644
--- a/crates/ide_completion/src/lib.rs
+++ b/crates/ide_completion/src/lib.rs
@@ -144,7 +144,7 @@ pub fn resolve_completion_edits(
144) -> Option<Vec<TextEdit>> { 144) -> Option<Vec<TextEdit>> {
145 let ctx = CompletionContext::new(db, position, config)?; 145 let ctx = CompletionContext::new(db, position, config)?;
146 let position_for_import = position_for_import(&ctx, None)?; 146 let position_for_import = position_for_import(&ctx, None)?;
147 let import_scope = ImportScope::find_insert_use_container(position_for_import, &ctx.sema)?; 147 let scope = ImportScope::find_insert_use_container(position_for_import, &ctx.sema)?;
148 148
149 let current_module = ctx.sema.scope(position_for_import).module()?; 149 let current_module = ctx.sema.scope(position_for_import).module()?;
150 let current_crate = current_module.krate(); 150 let current_crate = current_module.krate();
@@ -158,9 +158,10 @@ pub fn resolve_completion_edits(
158 .zip(Some(candidate)) 158 .zip(Some(candidate))
159 }) 159 })
160 .find(|(mod_path, _)| mod_path.to_string() == full_import_path)?; 160 .find(|(mod_path, _)| mod_path.to_string() == full_import_path)?;
161 let import = LocatedImport::new(import_path, item_to_import, item_to_import); 161 let import =
162 LocatedImport::new(import_path.clone(), item_to_import, item_to_import, Some(import_path));
162 163
163 ImportEdit { import, import_scope }.to_text_edit(config.insert_use).map(|edit| vec![edit]) 164 ImportEdit { import, scope }.to_text_edit(config.insert_use).map(|edit| vec![edit])
164} 165}
165 166
166#[cfg(test)] 167#[cfg(test)]