diff options
author | Aleksey Kladov <[email protected]> | 2020-12-10 15:00:28 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-12-10 15:01:02 +0000 |
commit | 076945e47c22d984067e0ce4d2f2016821e869e2 (patch) | |
tree | e0dafa1ecf91c07b9946eed4fcf279ed3114cd0e | |
parent | 17f236c2b041de7abd8ec3be208b8eff75fd7ffb (diff) |
Minor, more orthogonal code
It's better to accept things as arguments rather than store them.
-rw-r--r-- | crates/completion/src/completions/unqualified_path.rs | 16 | ||||
-rw-r--r-- | crates/completion/src/item.rs | 5 | ||||
-rw-r--r-- | crates/completion/src/lib.rs | 4 | ||||
-rw-r--r-- | crates/completion/src/test_utils.rs | 3 |
4 files changed, 12 insertions, 16 deletions
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<() | |||
145 | }) | 145 | }) |
146 | .filter(|(mod_path, _)| mod_path.len() > 1) | 146 | .filter(|(mod_path, _)| mod_path.len() > 1) |
147 | .filter_map(|(import_path, definition)| { | 147 | .filter_map(|(import_path, definition)| { |
148 | render_resolution_with_import( | 148 | let ie = |
149 | RenderContext::new(ctx), | 149 | ImportEdit { import_path: import_path.clone(), import_scope: import_scope.clone() }; |
150 | ImportEdit { | 150 | { |
151 | import_path: import_path.clone(), | 151 | let _p = profile::span("totextedit"); |
152 | import_scope: import_scope.clone(), | 152 | ie.to_text_edit(ctx.config.merge); |
153 | merge_behavior: ctx.config.merge, | 153 | } |
154 | }, | 154 | render_resolution_with_import(RenderContext::new(ctx), ie, &definition) |
155 | &definition, | ||
156 | ) | ||
157 | }); | 155 | }); |
158 | 156 | ||
159 | acc.add_all(possible_imports); | 157 | 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 { | |||
271 | pub struct ImportEdit { | 271 | pub struct ImportEdit { |
272 | pub import_path: ModPath, | 272 | pub import_path: ModPath, |
273 | pub import_scope: ImportScope, | 273 | pub import_scope: ImportScope, |
274 | pub merge_behavior: Option<MergeBehavior>, | ||
275 | } | 274 | } |
276 | 275 | ||
277 | impl ImportEdit { | 276 | impl ImportEdit { |
278 | /// Attempts to insert the import to the given scope, producing a text edit. | 277 | /// Attempts to insert the import to the given scope, producing a text edit. |
279 | /// May return no edit in edge cases, such as scope already containing the import. | 278 | /// May return no edit in edge cases, such as scope already containing the import. |
280 | pub fn to_text_edit(&self) -> Option<TextEdit> { | 279 | pub fn to_text_edit(&self, merge_behavior: Option<MergeBehavior>) -> Option<TextEdit> { |
281 | let _p = profile::span("ImportEdit::to_text_edit"); | 280 | let _p = profile::span("ImportEdit::to_text_edit"); |
282 | 281 | ||
283 | let rewriter = insert_use::insert_use( | 282 | let rewriter = insert_use::insert_use( |
284 | &self.import_scope, | 283 | &self.import_scope, |
285 | mod_path_to_ast(&self.import_path), | 284 | mod_path_to_ast(&self.import_path), |
286 | self.merge_behavior, | 285 | merge_behavior, |
287 | ); | 286 | ); |
288 | let old_ast = rewriter.rewrite_root()?; | 287 | let old_ast = rewriter.rewrite_root()?; |
289 | let mut import_insert = TextEdit::builder(); | 288 | 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( | |||
153 | }) | 153 | }) |
154 | .find(|mod_path| mod_path.to_string() == full_import_path)?; | 154 | .find(|mod_path| mod_path.to_string() == full_import_path)?; |
155 | 155 | ||
156 | ImportEdit { import_path, import_scope, merge_behavior: config.merge } | 156 | ImportEdit { import_path, import_scope }.to_text_edit(config.merge).map(|edit| vec![edit]) |
157 | .to_text_edit() | ||
158 | .map(|edit| vec![edit]) | ||
159 | } | 157 | } |
160 | 158 | ||
161 | #[cfg(test)] | 159 | #[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( | |||
98 | let mut actual = db.file_text(position.file_id).to_string(); | 98 | let mut actual = db.file_text(position.file_id).to_string(); |
99 | 99 | ||
100 | let mut combined_edit = completion.text_edit().to_owned(); | 100 | let mut combined_edit = completion.text_edit().to_owned(); |
101 | if let Some(import_text_edit) = completion.import_to_add().and_then(|edit| edit.to_text_edit()) | 101 | if let Some(import_text_edit) = |
102 | completion.import_to_add().and_then(|edit| edit.to_text_edit(config.merge)) | ||
102 | { | 103 | { |
103 | combined_edit.union(import_text_edit).expect( | 104 | combined_edit.union(import_text_edit).expect( |
104 | "Failed to apply completion resolve changes: change ranges overlap, but should not", | 105 | "Failed to apply completion resolve changes: change ranges overlap, but should not", |