diff options
Diffstat (limited to 'crates/completion/src/item.rs')
-rw-r--r-- | crates/completion/src/item.rs | 36 |
1 files changed, 31 insertions, 5 deletions
diff --git a/crates/completion/src/item.rs b/crates/completion/src/item.rs index dc67df075..5906637a6 100644 --- a/crates/completion/src/item.rs +++ b/crates/completion/src/item.rs | |||
@@ -3,8 +3,11 @@ | |||
3 | use std::fmt; | 3 | use std::fmt; |
4 | 4 | ||
5 | use hir::{Documentation, ModPath, Mutability}; | 5 | use hir::{Documentation, ModPath, Mutability}; |
6 | use ide_db::helpers::insert_use::{ImportScope, MergeBehaviour}; | 6 | use ide_db::helpers::{ |
7 | use syntax::TextRange; | 7 | insert_use::{self, ImportScope, MergeBehaviour}, |
8 | mod_path_to_ast, | ||
9 | }; | ||
10 | use syntax::{algo, TextRange}; | ||
8 | use text_edit::TextEdit; | 11 | use text_edit::TextEdit; |
9 | 12 | ||
10 | use crate::config::SnippetCap; | 13 | use crate::config::SnippetCap; |
@@ -207,6 +210,7 @@ impl CompletionItem { | |||
207 | score: None, | 210 | score: None, |
208 | ref_match: None, | 211 | ref_match: None, |
209 | import_to_add: None, | 212 | import_to_add: None, |
213 | resolve_import_immediately: true, | ||
210 | } | 214 | } |
211 | } | 215 | } |
212 | 216 | ||
@@ -279,6 +283,7 @@ pub(crate) struct Builder { | |||
279 | source_range: TextRange, | 283 | source_range: TextRange, |
280 | completion_kind: CompletionKind, | 284 | completion_kind: CompletionKind, |
281 | import_to_add: Option<ImportToAdd>, | 285 | import_to_add: Option<ImportToAdd>, |
286 | resolve_import_immediately: bool, | ||
282 | label: String, | 287 | label: String, |
283 | insert_text: Option<String>, | 288 | insert_text: Option<String>, |
284 | insert_text_format: InsertTextFormat, | 289 | insert_text_format: InsertTextFormat, |
@@ -300,6 +305,7 @@ impl Builder { | |||
300 | let mut label = self.label; | 305 | let mut label = self.label; |
301 | let mut lookup = self.lookup; | 306 | let mut lookup = self.lookup; |
302 | let mut insert_text = self.insert_text; | 307 | let mut insert_text = self.insert_text; |
308 | let mut text_edits = TextEdit::builder(); | ||
303 | 309 | ||
304 | if let Some(import_to_add) = self.import_to_add.as_ref() { | 310 | if let Some(import_to_add) = self.import_to_add.as_ref() { |
305 | let mut import_path_without_last_segment = import_to_add.import_path.to_owned(); | 311 | let mut import_path_without_last_segment = import_to_add.import_path.to_owned(); |
@@ -314,20 +320,35 @@ impl Builder { | |||
314 | } | 320 | } |
315 | label = format!("{}::{}", import_path_without_last_segment, label); | 321 | label = format!("{}::{}", import_path_without_last_segment, label); |
316 | } | 322 | } |
323 | |||
324 | if self.resolve_import_immediately { | ||
325 | let rewriter = insert_use::insert_use( | ||
326 | &import_to_add.import_scope, | ||
327 | mod_path_to_ast(&import_to_add.import_path), | ||
328 | import_to_add.merge_behaviour, | ||
329 | ); | ||
330 | if let Some(old_ast) = rewriter.rewrite_root() { | ||
331 | algo::diff(&old_ast, &rewriter.rewrite(&old_ast)) | ||
332 | .into_text_edit(&mut text_edits); | ||
333 | } | ||
334 | } | ||
317 | } | 335 | } |
318 | 336 | ||
319 | let text_edit = match self.text_edit { | 337 | let original_edit = match self.text_edit { |
320 | Some(it) => it, | 338 | Some(it) => it, |
321 | None => { | 339 | None => { |
322 | TextEdit::replace(self.source_range, insert_text.unwrap_or_else(|| label.clone())) | 340 | TextEdit::replace(self.source_range, insert_text.unwrap_or_else(|| label.clone())) |
323 | } | 341 | } |
324 | }; | 342 | }; |
325 | 343 | ||
344 | let mut resulting_edit = text_edits.finish(); | ||
345 | resulting_edit.union(original_edit).expect("Failed to unite text edits"); | ||
346 | |||
326 | CompletionItem { | 347 | CompletionItem { |
327 | source_range: self.source_range, | 348 | source_range: self.source_range, |
328 | label, | 349 | label, |
329 | insert_text_format: self.insert_text_format, | 350 | insert_text_format: self.insert_text_format, |
330 | text_edit, | 351 | text_edit: resulting_edit, |
331 | detail: self.detail, | 352 | detail: self.detail, |
332 | documentation: self.documentation, | 353 | documentation: self.documentation, |
333 | lookup, | 354 | lookup, |
@@ -400,8 +421,13 @@ impl Builder { | |||
400 | self.trigger_call_info = Some(true); | 421 | self.trigger_call_info = Some(true); |
401 | self | 422 | self |
402 | } | 423 | } |
403 | pub(crate) fn add_import(mut self, import_to_add: Option<ImportToAdd>) -> Builder { | 424 | pub(crate) fn add_import( |
425 | mut self, | ||
426 | import_to_add: Option<ImportToAdd>, | ||
427 | resolve_import_immediately: bool, | ||
428 | ) -> Builder { | ||
404 | self.import_to_add = import_to_add; | 429 | self.import_to_add = import_to_add; |
430 | self.resolve_import_immediately = resolve_import_immediately; | ||
405 | self | 431 | self |
406 | } | 432 | } |
407 | pub(crate) fn set_ref_match( | 433 | pub(crate) fn set_ref_match( |