Minor, more orthogonal code

It's better to accept things as arguments rather than store them.
This commit is contained in:
Aleksey Kladov 2020-12-10 18:00:28 +03:00
parent 17f236c2b0
commit 076945e47c
4 changed files with 12 additions and 16 deletions

View file

@ -145,15 +145,13 @@ fn fuzzy_completion(acc: &mut Completions, ctx: &CompletionContext) -> Option<()
}) })
.filter(|(mod_path, _)| mod_path.len() > 1) .filter(|(mod_path, _)| mod_path.len() > 1)
.filter_map(|(import_path, definition)| { .filter_map(|(import_path, definition)| {
render_resolution_with_import( let ie =
RenderContext::new(ctx), ImportEdit { import_path: import_path.clone(), import_scope: import_scope.clone() };
ImportEdit { {
import_path: import_path.clone(), let _p = profile::span("totextedit");
import_scope: import_scope.clone(), ie.to_text_edit(ctx.config.merge);
merge_behavior: ctx.config.merge, }
}, render_resolution_with_import(RenderContext::new(ctx), ie, &definition)
&definition,
)
}); });
acc.add_all(possible_imports); acc.add_all(possible_imports);

View file

@ -271,19 +271,18 @@ impl CompletionItem {
pub struct ImportEdit { pub struct ImportEdit {
pub import_path: ModPath, pub import_path: ModPath,
pub import_scope: ImportScope, pub import_scope: ImportScope,
pub merge_behavior: Option<MergeBehavior>,
} }
impl ImportEdit { impl ImportEdit {
/// Attempts to insert the import to the given scope, producing a text edit. /// 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. /// May return no edit in edge cases, such as scope already containing the import.
pub fn to_text_edit(&self) -> Option<TextEdit> { pub fn to_text_edit(&self, merge_behavior: Option<MergeBehavior>) -> Option<TextEdit> {
let _p = profile::span("ImportEdit::to_text_edit"); let _p = profile::span("ImportEdit::to_text_edit");
let rewriter = insert_use::insert_use( let rewriter = insert_use::insert_use(
&self.import_scope, &self.import_scope,
mod_path_to_ast(&self.import_path), mod_path_to_ast(&self.import_path),
self.merge_behavior, merge_behavior,
); );
let old_ast = rewriter.rewrite_root()?; let old_ast = rewriter.rewrite_root()?;
let mut import_insert = TextEdit::builder(); let mut import_insert = TextEdit::builder();

View file

@ -153,9 +153,7 @@ pub fn resolve_completion_edits(
}) })
.find(|mod_path| mod_path.to_string() == full_import_path)?; .find(|mod_path| mod_path.to_string() == full_import_path)?;
ImportEdit { import_path, import_scope, merge_behavior: config.merge } ImportEdit { import_path, import_scope }.to_text_edit(config.merge).map(|edit| vec![edit])
.to_text_edit()
.map(|edit| vec![edit])
} }
#[cfg(test)] #[cfg(test)]

View file

@ -98,7 +98,8 @@ pub(crate) fn check_edit_with_config(
let mut actual = db.file_text(position.file_id).to_string(); let mut actual = db.file_text(position.file_id).to_string();
let mut combined_edit = completion.text_edit().to_owned(); 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( combined_edit.union(import_text_edit).expect(
"Failed to apply completion resolve changes: change ranges overlap, but should not", "Failed to apply completion resolve changes: change ranges overlap, but should not",