aboutsummaryrefslogtreecommitdiff
path: root/crates/completion/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/completion/src')
-rw-r--r--crates/completion/src/item.rs34
1 files changed, 11 insertions, 23 deletions
diff --git a/crates/completion/src/item.rs b/crates/completion/src/item.rs
index e85549fef..ce6a44e57 100644
--- a/crates/completion/src/item.rs
+++ b/crates/completion/src/item.rs
@@ -3,11 +3,8 @@
3use std::fmt; 3use std::fmt;
4 4
5use hir::{Documentation, ModPath, Mutability}; 5use hir::{Documentation, ModPath, Mutability};
6use ide_db::helpers::{ 6use ide_db::helpers::insert_use::{ImportScope, MergeBehaviour};
7 insert_use::{self, ImportScope, MergeBehaviour}, 7use syntax::TextRange;
8 mod_path_to_ast,
9};
10use syntax::{algo, TextRange};
11use text_edit::TextEdit; 8use text_edit::TextEdit;
12 9
13use crate::config::SnippetCap; 10use crate::config::SnippetCap;
@@ -65,6 +62,10 @@ pub struct CompletionItem {
65 /// Indicates that a reference or mutable reference to this variable is a 62 /// Indicates that a reference or mutable reference to this variable is a
66 /// possible match. 63 /// possible match.
67 ref_match: Option<(Mutability, CompletionScore)>, 64 ref_match: Option<(Mutability, CompletionScore)>,
65
66 /// The data later to be used in the `completionItem/resolve` response
67 /// to add the insert import edit.
68 import_to_add: Option<ImportToAdd>,
68} 69}
69 70
70// We use custom debug for CompletionItem to make snapshot tests more readable. 71// We use custom debug for CompletionItem to make snapshot tests more readable.
@@ -294,11 +295,9 @@ impl Builder {
294 let mut label = self.label; 295 let mut label = self.label;
295 let mut lookup = self.lookup; 296 let mut lookup = self.lookup;
296 let mut insert_text = self.insert_text; 297 let mut insert_text = self.insert_text;
297 let mut text_edits = TextEdit::builder();
298 298
299 if let Some(import_data) = self.import_to_add { 299 if let Some(import_to_add) = self.import_to_add.as_ref() {
300 let import = mod_path_to_ast(&import_data.import_path); 300 let mut import_path_without_last_segment = import_to_add.import_path.to_owned();
301 let mut import_path_without_last_segment = import_data.import_path;
302 let _ = import_path_without_last_segment.segments.pop(); 301 let _ = import_path_without_last_segment.segments.pop();
303 302
304 if !import_path_without_last_segment.segments.is_empty() { 303 if !import_path_without_last_segment.segments.is_empty() {
@@ -310,32 +309,20 @@ impl Builder {
310 } 309 }
311 label = format!("{}::{}", import_path_without_last_segment, label); 310 label = format!("{}::{}", import_path_without_last_segment, label);
312 } 311 }
313
314 let rewriter = insert_use::insert_use(
315 &import_data.import_scope,
316 import,
317 import_data.merge_behaviour,
318 );
319 if let Some(old_ast) = rewriter.rewrite_root() {
320 algo::diff(&old_ast, &rewriter.rewrite(&old_ast)).into_text_edit(&mut text_edits);
321 }
322 } 312 }
323 313
324 let original_edit = match self.text_edit { 314 let text_edit = match self.text_edit {
325 Some(it) => it, 315 Some(it) => it,
326 None => { 316 None => {
327 TextEdit::replace(self.source_range, insert_text.unwrap_or_else(|| label.clone())) 317 TextEdit::replace(self.source_range, insert_text.unwrap_or_else(|| label.clone()))
328 } 318 }
329 }; 319 };
330 320
331 let mut resulting_edit = text_edits.finish();
332 resulting_edit.union(original_edit).expect("Failed to unite text edits");
333
334 CompletionItem { 321 CompletionItem {
335 source_range: self.source_range, 322 source_range: self.source_range,
336 label, 323 label,
337 insert_text_format: self.insert_text_format, 324 insert_text_format: self.insert_text_format,
338 text_edit: resulting_edit, 325 text_edit,
339 detail: self.detail, 326 detail: self.detail,
340 documentation: self.documentation, 327 documentation: self.documentation,
341 lookup, 328 lookup,
@@ -345,6 +332,7 @@ impl Builder {
345 trigger_call_info: self.trigger_call_info.unwrap_or(false), 332 trigger_call_info: self.trigger_call_info.unwrap_or(false),
346 score: self.score, 333 score: self.score,
347 ref_match: self.ref_match, 334 ref_match: self.ref_match,
335 import_to_add: self.import_to_add,
348 } 336 }
349 } 337 }
350 pub(crate) fn lookup_by(mut self, lookup: impl Into<String>) -> Builder { 338 pub(crate) fn lookup_by(mut self, lookup: impl Into<String>) -> Builder {