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 ++++------------ crates/assists/src/tests.rs | 19 ++++++++----------- 2 files changed, 12 insertions(+), 23 deletions(-) (limited to 'crates/assists/src') 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); } } diff --git a/crates/assists/src/tests.rs b/crates/assists/src/tests.rs index fef29a0b8..d13d6ad31 100644 --- a/crates/assists/src/tests.rs +++ b/crates/assists/src/tests.rs @@ -80,10 +80,8 @@ fn check_doc_test(assist_id: &str, before: &str, after: &str) { let actual = { let source_change = assist.source_change.unwrap(); let mut actual = before; - for source_file_edit in source_change.source_file_edits { - if source_file_edit.file_id == file_id { - source_file_edit.edit.apply(&mut actual) - } + if let Some(source_file_edit) = source_change.source_file_edits.edits.get(&file_id) { + source_file_edit.apply(&mut actual); } actual }; @@ -116,20 +114,19 @@ fn check(handler: Handler, before: &str, expected: ExpectedResult, assist_label: match (assist, expected) { (Some(assist), ExpectedResult::After(after)) => { - let mut source_change = assist.source_change.unwrap(); + let source_change = assist.source_change.unwrap(); assert!(!source_change.source_file_edits.is_empty()); let skip_header = source_change.source_file_edits.len() == 1 && source_change.file_system_edits.len() == 0; - source_change.source_file_edits.sort_by_key(|it| it.file_id); let mut buf = String::new(); - for source_file_edit in source_change.source_file_edits { - let mut text = db.file_text(source_file_edit.file_id).as_ref().to_owned(); - source_file_edit.edit.apply(&mut text); + for (file_id, edit) in source_change.source_file_edits.edits { + let mut text = db.file_text(file_id).as_ref().to_owned(); + edit.apply(&mut text); if !skip_header { - let sr = db.file_source_root(source_file_edit.file_id); + let sr = db.file_source_root(file_id); let sr = db.source_root(sr); - let path = sr.path_for_file(&source_file_edit.file_id).unwrap(); + let path = sr.path_for_file(&file_id).unwrap(); format_to!(buf, "//- {}\n", path) } buf.push_str(&text); -- 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 ++++++++-------------------- crates/assists/src/tests.rs | 25 +++++++++++-------------- 2 files changed, 19 insertions(+), 34 deletions(-) (limited to 'crates/assists/src') 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) } } diff --git a/crates/assists/src/tests.rs b/crates/assists/src/tests.rs index d13d6ad31..71431b406 100644 --- a/crates/assists/src/tests.rs +++ b/crates/assists/src/tests.rs @@ -80,7 +80,7 @@ fn check_doc_test(assist_id: &str, before: &str, after: &str) { let actual = { let source_change = assist.source_change.unwrap(); let mut actual = before; - if let Some(source_file_edit) = source_change.source_file_edits.edits.get(&file_id) { + if let Some(source_file_edit) = source_change.get_source_edit(file_id) { source_file_edit.apply(&mut actual); } actual @@ -120,7 +120,7 @@ fn check(handler: Handler, before: &str, expected: ExpectedResult, assist_label: && source_change.file_system_edits.len() == 0; let mut buf = String::new(); - for (file_id, edit) in source_change.source_file_edits.edits { + for (file_id, edit) in source_change.source_file_edits { let mut text = db.file_text(file_id).as_ref().to_owned(); edit.apply(&mut text); if !skip_header { @@ -132,18 +132,15 @@ fn check(handler: Handler, before: &str, expected: ExpectedResult, assist_label: buf.push_str(&text); } - for file_system_edit in source_change.file_system_edits.clone() { - match file_system_edit { - FileSystemEdit::CreateFile { dst, initial_contents } => { - let sr = db.file_source_root(dst.anchor); - let sr = db.source_root(sr); - let mut base = sr.path_for_file(&dst.anchor).unwrap().clone(); - base.pop(); - let created_file_path = format!("{}{}", base.to_string(), &dst.path[1..]); - format_to!(buf, "//- {}\n", created_file_path); - buf.push_str(&initial_contents); - } - _ => (), + for file_system_edit in source_change.file_system_edits { + if let FileSystemEdit::CreateFile { dst, initial_contents } = file_system_edit { + let sr = db.file_source_root(dst.anchor); + let sr = db.source_root(sr); + let mut base = sr.path_for_file(&dst.anchor).unwrap().clone(); + base.pop(); + let created_file_path = format!("{}{}", base.to_string(), &dst.path[1..]); + format_to!(buf, "//- {}\n", created_file_path); + buf.push_str(&initial_contents); } } -- cgit v1.2.3