From f51457a643b768794092f73add6dda4aecd400a1 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Thu, 14 Jan 2021 18:35:22 +0100 Subject: Group file source edits by FileId --- crates/assists/src/assist_context.rs | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) (limited to 'crates/assists/src/assist_context.rs') diff --git a/crates/assists/src/assist_context.rs b/crates/assists/src/assist_context.rs index 91cc63427..de4b32573 100644 --- a/crates/assists/src/assist_context.rs +++ b/crates/assists/src/assist_context.rs @@ -10,7 +10,7 @@ use ide_db::{ }; use ide_db::{ label::Label, - source_change::{FileSystemEdit, SourceChange, SourceFileEdit}, + source_change::{FileSystemEdit, SourceChange, SourceFileEdits}, RootDatabase, }; use syntax::{ @@ -181,7 +181,7 @@ pub(crate) struct AssistBuilder { edit: TextEditBuilder, file_id: FileId, is_snippet: bool, - source_file_edits: Vec, + source_file_edits: SourceFileEdits, file_system_edits: Vec, } @@ -191,7 +191,7 @@ impl AssistBuilder { edit: TextEdit::builder(), file_id, is_snippet: false, - source_file_edits: Vec::default(), + source_file_edits: SourceFileEdits::default(), file_system_edits: Vec::default(), } } @@ -204,15 +204,7 @@ impl AssistBuilder { fn commit(&mut self) { let edit = mem::take(&mut self.edit).finish(); if !edit.is_empty() { - match self.source_file_edits.binary_search_by_key(&self.file_id, |edit| edit.file_id) { - Ok(idx) => self.source_file_edits[idx] - .edit - .union(edit) - .expect("overlapping edits for same file"), - Err(idx) => self - .source_file_edits - .insert(idx, SourceFileEdit { file_id: self.file_id, edit }), - } + self.source_file_edits.insert(self.file_id, edit); } } -- cgit v1.2.3 From d5095329a1c12e93653d8de4a93f0b4f5cad4c6e Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Thu, 14 Jan 2021 22:43:36 +0100 Subject: Phase out SourceFileEdits in favour of a plain HashMap --- crates/assists/src/assist_context.rs | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-) (limited to 'crates/assists/src/assist_context.rs') diff --git a/crates/assists/src/assist_context.rs b/crates/assists/src/assist_context.rs index de4b32573..321fe77f3 100644 --- a/crates/assists/src/assist_context.rs +++ b/crates/assists/src/assist_context.rs @@ -10,7 +10,7 @@ use ide_db::{ }; use ide_db::{ label::Label, - source_change::{FileSystemEdit, SourceChange, SourceFileEdits}, + source_change::{FileSystemEdit, SourceChange}, RootDatabase, }; use syntax::{ @@ -180,20 +180,12 @@ impl Assists { pub(crate) struct AssistBuilder { edit: TextEditBuilder, file_id: FileId, - is_snippet: bool, - source_file_edits: SourceFileEdits, - file_system_edits: Vec, + source_change: SourceChange, } impl AssistBuilder { pub(crate) fn new(file_id: FileId) -> AssistBuilder { - AssistBuilder { - edit: TextEdit::builder(), - file_id, - is_snippet: false, - source_file_edits: SourceFileEdits::default(), - file_system_edits: Vec::default(), - } + AssistBuilder { edit: TextEdit::builder(), file_id, source_change: SourceChange::default() } } pub(crate) fn edit_file(&mut self, file_id: FileId) { @@ -204,7 +196,7 @@ impl AssistBuilder { fn commit(&mut self) { let edit = mem::take(&mut self.edit).finish(); if !edit.is_empty() { - self.source_file_edits.insert(self.file_id, edit); + self.source_change.insert_source_edit(self.file_id, edit); } } @@ -223,7 +215,7 @@ impl AssistBuilder { offset: TextSize, snippet: impl Into, ) { - self.is_snippet = true; + self.source_change.is_snippet = true; self.insert(offset, snippet); } /// Replaces specified `range` of text with a given string. @@ -237,7 +229,7 @@ impl AssistBuilder { range: TextRange, snippet: impl Into, ) { - self.is_snippet = true; + self.source_change.is_snippet = true; self.replace(range, snippet); } pub(crate) fn replace_ast(&mut self, old: N, new: N) { @@ -252,15 +244,11 @@ impl AssistBuilder { pub(crate) fn create_file(&mut self, dst: AnchoredPathBuf, content: impl Into) { let file_system_edit = FileSystemEdit::CreateFile { dst: dst.clone(), initial_contents: content.into() }; - self.file_system_edits.push(file_system_edit); + self.source_change.push_file_system_edit(file_system_edit); } fn finish(mut self) -> SourceChange { self.commit(); - SourceChange { - source_file_edits: mem::take(&mut self.source_file_edits), - file_system_edits: mem::take(&mut self.file_system_edits), - is_snippet: self.is_snippet, - } + mem::take(&mut self.source_change) } } -- cgit v1.2.3