diff options
Diffstat (limited to 'crates/ide_completion')
-rw-r--r-- | crates/ide_completion/src/completions/flyimport.rs | 75 | ||||
-rw-r--r-- | crates/ide_completion/src/completions/postfix/format_like.rs | 2 | ||||
-rw-r--r-- | crates/ide_completion/src/context.rs | 4 | ||||
-rw-r--r-- | crates/ide_completion/src/item.rs | 10 | ||||
-rw-r--r-- | crates/ide_completion/src/lib.rs | 2 | ||||
-rw-r--r-- | crates/ide_completion/src/render/enum_variant.rs | 2 | ||||
-rw-r--r-- | crates/ide_completion/src/test_utils.rs | 5 |
7 files changed, 85 insertions, 15 deletions
diff --git a/crates/ide_completion/src/completions/flyimport.rs b/crates/ide_completion/src/completions/flyimport.rs index 9ace13e41..9d5b61562 100644 --- a/crates/ide_completion/src/completions/flyimport.rs +++ b/crates/ide_completion/src/completions/flyimport.rs | |||
@@ -114,6 +114,8 @@ pub(crate) fn import_on_the_fly(acc: &mut Completions, ctx: &CompletionContext) | |||
114 | || ctx.attribute_under_caret.is_some() | 114 | || ctx.attribute_under_caret.is_some() |
115 | || ctx.mod_declaration_under_caret.is_some() | 115 | || ctx.mod_declaration_under_caret.is_some() |
116 | || ctx.record_lit_syntax.is_some() | 116 | || ctx.record_lit_syntax.is_some() |
117 | || ctx.has_trait_parent | ||
118 | || ctx.has_impl_parent | ||
117 | { | 119 | { |
118 | return None; | 120 | return None; |
119 | } | 121 | } |
@@ -130,7 +132,7 @@ pub(crate) fn import_on_the_fly(acc: &mut Completions, ctx: &CompletionContext) | |||
130 | 132 | ||
131 | let user_input_lowercased = potential_import_name.to_lowercase(); | 133 | let user_input_lowercased = potential_import_name.to_lowercase(); |
132 | let import_assets = import_assets(ctx, potential_import_name)?; | 134 | let import_assets = import_assets(ctx, potential_import_name)?; |
133 | let import_scope = ImportScope::find_insert_use_container( | 135 | let import_scope = ImportScope::find_insert_use_container_with_macros( |
134 | position_for_import(ctx, Some(import_assets.import_candidate()))?, | 136 | position_for_import(ctx, Some(import_assets.import_candidate()))?, |
135 | &ctx.sema, | 137 | &ctx.sema, |
136 | )?; | 138 | )?; |
@@ -1077,4 +1079,75 @@ fn main() { | |||
1077 | "#]], | 1079 | "#]], |
1078 | ); | 1080 | ); |
1079 | } | 1081 | } |
1082 | |||
1083 | #[test] | ||
1084 | fn no_flyimports_in_traits_and_impl_declarations() { | ||
1085 | check( | ||
1086 | r#" | ||
1087 | mod m { | ||
1088 | pub fn some_fn() -> i32 { | ||
1089 | 42 | ||
1090 | } | ||
1091 | } | ||
1092 | trait Foo { | ||
1093 | som$0 | ||
1094 | } | ||
1095 | "#, | ||
1096 | expect![[r#""#]], | ||
1097 | ); | ||
1098 | |||
1099 | check( | ||
1100 | r#" | ||
1101 | mod m { | ||
1102 | pub fn some_fn() -> i32 { | ||
1103 | 42 | ||
1104 | } | ||
1105 | } | ||
1106 | struct Foo; | ||
1107 | impl Foo { | ||
1108 | som$0 | ||
1109 | } | ||
1110 | "#, | ||
1111 | expect![[r#""#]], | ||
1112 | ); | ||
1113 | |||
1114 | check( | ||
1115 | r#" | ||
1116 | mod m { | ||
1117 | pub fn some_fn() -> i32 { | ||
1118 | 42 | ||
1119 | } | ||
1120 | } | ||
1121 | struct Foo; | ||
1122 | trait Bar {} | ||
1123 | impl Bar for Foo { | ||
1124 | som$0 | ||
1125 | } | ||
1126 | "#, | ||
1127 | expect![[r#""#]], | ||
1128 | ); | ||
1129 | } | ||
1130 | |||
1131 | #[test] | ||
1132 | fn no_inherent_candidates_proposed() { | ||
1133 | check( | ||
1134 | r#" | ||
1135 | mod baz { | ||
1136 | pub trait DefDatabase { | ||
1137 | fn method1(&self); | ||
1138 | } | ||
1139 | pub trait HirDatabase: DefDatabase { | ||
1140 | fn method2(&self); | ||
1141 | } | ||
1142 | } | ||
1143 | |||
1144 | mod bar { | ||
1145 | fn test(db: &dyn crate::baz::HirDatabase) { | ||
1146 | db.metho$0 | ||
1147 | } | ||
1148 | } | ||
1149 | "#, | ||
1150 | expect![[r#""#]], | ||
1151 | ); | ||
1152 | } | ||
1080 | } | 1153 | } |
diff --git a/crates/ide_completion/src/completions/postfix/format_like.rs b/crates/ide_completion/src/completions/postfix/format_like.rs index e86ffa8f8..0dcb3e898 100644 --- a/crates/ide_completion/src/completions/postfix/format_like.rs +++ b/crates/ide_completion/src/completions/postfix/format_like.rs | |||
@@ -1,4 +1,4 @@ | |||
1 | // Feature: Format String Completion. | 1 | // Feature: Format String Completion |
2 | // | 2 | // |
3 | // `"Result {result} is {2 + 2}"` is expanded to the `"Result {} is {}", result, 2 + 2`. | 3 | // `"Result {result} is {2 + 2}"` is expanded to the `"Result {} is {}", result, 2 + 2`. |
4 | // | 4 | // |
diff --git a/crates/ide_completion/src/context.rs b/crates/ide_completion/src/context.rs index 32f81aec1..b005bd773 100644 --- a/crates/ide_completion/src/context.rs +++ b/crates/ide_completion/src/context.rs | |||
@@ -347,7 +347,7 @@ impl<'a> CompletionContext<'a> { | |||
347 | .and_then(|node| ast::RecordExprField::cast(node)) | 347 | .and_then(|node| ast::RecordExprField::cast(node)) |
348 | .and_then(|rf| self.sema.resolve_record_field(&rf).zip(Some(rf))) | 348 | .and_then(|rf| self.sema.resolve_record_field(&rf).zip(Some(rf))) |
349 | .map(|(f, rf)|( | 349 | .map(|(f, rf)|( |
350 | Some(f.0.signature_ty(self.db)), | 350 | Some(f.0.ty(self.db)), |
351 | rf.field_name().map(NameOrNameRef::NameRef), | 351 | rf.field_name().map(NameOrNameRef::NameRef), |
352 | )) | 352 | )) |
353 | .unwrap_or((None, None)) | 353 | .unwrap_or((None, None)) |
@@ -357,7 +357,7 @@ impl<'a> CompletionContext<'a> { | |||
357 | self.sema | 357 | self.sema |
358 | .resolve_record_field(&it) | 358 | .resolve_record_field(&it) |
359 | .map(|f|( | 359 | .map(|f|( |
360 | Some(f.0.signature_ty(self.db)), | 360 | Some(f.0.ty(self.db)), |
361 | it.field_name().map(NameOrNameRef::NameRef), | 361 | it.field_name().map(NameOrNameRef::NameRef), |
362 | )) | 362 | )) |
363 | .unwrap_or((None, None)) | 363 | .unwrap_or((None, None)) |
diff --git a/crates/ide_completion/src/item.rs b/crates/ide_completion/src/item.rs index cc4ac9ea2..99edb9499 100644 --- a/crates/ide_completion/src/item.rs +++ b/crates/ide_completion/src/item.rs | |||
@@ -29,7 +29,7 @@ pub struct CompletionItem { | |||
29 | /// Range of identifier that is being completed. | 29 | /// Range of identifier that is being completed. |
30 | /// | 30 | /// |
31 | /// It should be used primarily for UI, but we also use this to convert | 31 | /// It should be used primarily for UI, but we also use this to convert |
32 | /// genetic TextEdit into LSP's completion edit (see conv.rs). | 32 | /// generic TextEdit into LSP's completion edit (see conv.rs). |
33 | /// | 33 | /// |
34 | /// `source_range` must contain the completion offset. `insert_text` should | 34 | /// `source_range` must contain the completion offset. `insert_text` should |
35 | /// start with what `source_range` points to, or VSCode will filter out the | 35 | /// start with what `source_range` points to, or VSCode will filter out the |
@@ -377,11 +377,11 @@ impl ImportEdit { | |||
377 | pub fn to_text_edit(&self, cfg: InsertUseConfig) -> Option<TextEdit> { | 377 | pub fn to_text_edit(&self, cfg: InsertUseConfig) -> Option<TextEdit> { |
378 | let _p = profile::span("ImportEdit::to_text_edit"); | 378 | let _p = profile::span("ImportEdit::to_text_edit"); |
379 | 379 | ||
380 | let rewriter = | 380 | let new_ast = self.scope.clone_for_update(); |
381 | insert_use::insert_use(&self.scope, mod_path_to_ast(&self.import.import_path), cfg); | 381 | insert_use::insert_use(&new_ast, mod_path_to_ast(&self.import.import_path), cfg); |
382 | let old_ast = rewriter.rewrite_root()?; | ||
383 | let mut import_insert = TextEdit::builder(); | 382 | let mut import_insert = TextEdit::builder(); |
384 | algo::diff(&old_ast, &rewriter.rewrite(&old_ast)).into_text_edit(&mut import_insert); | 383 | algo::diff(self.scope.as_syntax_node(), new_ast.as_syntax_node()) |
384 | .into_text_edit(&mut import_insert); | ||
385 | 385 | ||
386 | Some(import_insert.finish()) | 386 | Some(import_insert.finish()) |
387 | } | 387 | } |
diff --git a/crates/ide_completion/src/lib.rs b/crates/ide_completion/src/lib.rs index 6f3d5c5c5..e32633565 100644 --- a/crates/ide_completion/src/lib.rs +++ b/crates/ide_completion/src/lib.rs | |||
@@ -179,7 +179,7 @@ pub fn resolve_completion_edits( | |||
179 | ) -> Option<Vec<TextEdit>> { | 179 | ) -> Option<Vec<TextEdit>> { |
180 | let ctx = CompletionContext::new(db, position, config)?; | 180 | let ctx = CompletionContext::new(db, position, config)?; |
181 | let position_for_import = position_for_import(&ctx, None)?; | 181 | let position_for_import = position_for_import(&ctx, None)?; |
182 | let scope = ImportScope::find_insert_use_container(position_for_import, &ctx.sema)?; | 182 | let scope = ImportScope::find_insert_use_container_with_macros(position_for_import, &ctx.sema)?; |
183 | 183 | ||
184 | let current_module = ctx.sema.scope(position_for_import).module()?; | 184 | let current_module = ctx.sema.scope(position_for_import).module()?; |
185 | let current_crate = current_module.krate(); | 185 | let current_crate = current_module.krate(); |
diff --git a/crates/ide_completion/src/render/enum_variant.rs b/crates/ide_completion/src/render/enum_variant.rs index 832f5ced1..0c0c71134 100644 --- a/crates/ide_completion/src/render/enum_variant.rs +++ b/crates/ide_completion/src/render/enum_variant.rs | |||
@@ -93,7 +93,7 @@ impl<'a> EnumRender<'a> { | |||
93 | .variant | 93 | .variant |
94 | .fields(self.ctx.db()) | 94 | .fields(self.ctx.db()) |
95 | .into_iter() | 95 | .into_iter() |
96 | .map(|field| (field.name(self.ctx.db()), field.signature_ty(self.ctx.db()))); | 96 | .map(|field| (field.name(self.ctx.db()), field.ty(self.ctx.db()))); |
97 | 97 | ||
98 | match self.variant_kind { | 98 | match self.variant_kind { |
99 | StructKind::Tuple | StructKind::Unit => format!( | 99 | StructKind::Tuple | StructKind::Unit => format!( |
diff --git a/crates/ide_completion/src/test_utils.rs b/crates/ide_completion/src/test_utils.rs index 9da844031..c9857ec5f 100644 --- a/crates/ide_completion/src/test_utils.rs +++ b/crates/ide_completion/src/test_utils.rs | |||
@@ -3,10 +3,7 @@ | |||
3 | use hir::{PrefixKind, Semantics}; | 3 | use hir::{PrefixKind, Semantics}; |
4 | use ide_db::{ | 4 | use ide_db::{ |
5 | base_db::{fixture::ChangeFixture, FileLoader, FilePosition}, | 5 | base_db::{fixture::ChangeFixture, FileLoader, FilePosition}, |
6 | helpers::{ | 6 | helpers::{insert_use::InsertUseConfig, merge_imports::MergeBehavior, SnippetCap}, |
7 | insert_use::{InsertUseConfig, MergeBehavior}, | ||
8 | SnippetCap, | ||
9 | }, | ||
10 | RootDatabase, | 7 | RootDatabase, |
11 | }; | 8 | }; |
12 | use itertools::Itertools; | 9 | use itertools::Itertools; |