diff options
Diffstat (limited to 'crates/completion/src/item.rs')
-rw-r--r-- | crates/completion/src/item.rs | 38 |
1 files changed, 26 insertions, 12 deletions
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 | ||
3 | use std::fmt; | 3 | use std::fmt; |
4 | 4 | ||
5 | use assists::utils::{insert_use, mod_path_to_ast, ImportScope, MergeBehaviour}; | ||
6 | use hir::{Documentation, ModPath, Mutability}; | 5 | use hir::{Documentation, ModPath, Mutability}; |
6 | use ide_db::helpers::{ | ||
7 | insert_use::{self, ImportScope, MergeBehaviour}, | ||
8 | mod_path_to_ast, | ||
9 | }; | ||
7 | use syntax::{algo, TextRange}; | 10 | use syntax::{algo, TextRange}; |
8 | use text_edit::TextEdit; | 11 | use 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)] | ||
263 | pub(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)] |
261 | pub(crate) struct Builder { | 272 | pub(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 | ||
279 | impl Builder { | 290 | impl 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( |