From 076945e47c22d984067e0ce4d2f2016821e869e2 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 10 Dec 2020 18:00:28 +0300 Subject: Minor, more orthogonal code It's better to accept things as arguments rather than store them. --- crates/completion/src/completions/unqualified_path.rs | 16 +++++++--------- crates/completion/src/item.rs | 5 ++--- crates/completion/src/lib.rs | 4 +--- crates/completion/src/test_utils.rs | 3 ++- 4 files changed, 12 insertions(+), 16 deletions(-) (limited to 'crates/completion') diff --git a/crates/completion/src/completions/unqualified_path.rs b/crates/completion/src/completions/unqualified_path.rs index 3372fb1a2..544886d29 100644 --- a/crates/completion/src/completions/unqualified_path.rs +++ b/crates/completion/src/completions/unqualified_path.rs @@ -145,15 +145,13 @@ fn fuzzy_completion(acc: &mut Completions, ctx: &CompletionContext) -> Option<() }) .filter(|(mod_path, _)| mod_path.len() > 1) .filter_map(|(import_path, definition)| { - render_resolution_with_import( - RenderContext::new(ctx), - ImportEdit { - import_path: import_path.clone(), - import_scope: import_scope.clone(), - merge_behavior: ctx.config.merge, - }, - &definition, - ) + let ie = + ImportEdit { import_path: import_path.clone(), import_scope: import_scope.clone() }; + { + let _p = profile::span("totextedit"); + ie.to_text_edit(ctx.config.merge); + } + render_resolution_with_import(RenderContext::new(ctx), ie, &definition) }); acc.add_all(possible_imports); diff --git a/crates/completion/src/item.rs b/crates/completion/src/item.rs index 83166df4e..65f8353e7 100644 --- a/crates/completion/src/item.rs +++ b/crates/completion/src/item.rs @@ -271,19 +271,18 @@ impl CompletionItem { pub struct ImportEdit { pub import_path: ModPath, pub import_scope: ImportScope, - pub merge_behavior: Option, } impl ImportEdit { /// Attempts to insert the import to the given scope, producing a text edit. /// May return no edit in edge cases, such as scope already containing the import. - pub fn to_text_edit(&self) -> Option { + pub fn to_text_edit(&self, merge_behavior: Option) -> Option { let _p = profile::span("ImportEdit::to_text_edit"); let rewriter = insert_use::insert_use( &self.import_scope, mod_path_to_ast(&self.import_path), - self.merge_behavior, + merge_behavior, ); let old_ast = rewriter.rewrite_root()?; let mut import_insert = TextEdit::builder(); diff --git a/crates/completion/src/lib.rs b/crates/completion/src/lib.rs index 6a4b3c167..8e27bb153 100644 --- a/crates/completion/src/lib.rs +++ b/crates/completion/src/lib.rs @@ -153,9 +153,7 @@ pub fn resolve_completion_edits( }) .find(|mod_path| mod_path.to_string() == full_import_path)?; - ImportEdit { import_path, import_scope, merge_behavior: config.merge } - .to_text_edit() - .map(|edit| vec![edit]) + ImportEdit { import_path, import_scope }.to_text_edit(config.merge).map(|edit| vec![edit]) } #[cfg(test)] diff --git a/crates/completion/src/test_utils.rs b/crates/completion/src/test_utils.rs index 25f5f4924..db896b2df 100644 --- a/crates/completion/src/test_utils.rs +++ b/crates/completion/src/test_utils.rs @@ -98,7 +98,8 @@ pub(crate) fn check_edit_with_config( let mut actual = db.file_text(position.file_id).to_string(); let mut combined_edit = completion.text_edit().to_owned(); - if let Some(import_text_edit) = completion.import_to_add().and_then(|edit| edit.to_text_edit()) + if let Some(import_text_edit) = + completion.import_to_add().and_then(|edit| edit.to_text_edit(config.merge)) { combined_edit.union(import_text_edit).expect( "Failed to apply completion resolve changes: change ranges overlap, but should not", -- cgit v1.2.3