aboutsummaryrefslogtreecommitdiff
path: root/crates/assists
diff options
context:
space:
mode:
authorLukas Wirth <[email protected]>2021-01-14 21:43:36 +0000
committerLukas Wirth <[email protected]>2021-01-14 21:43:36 +0000
commitd5095329a1c12e93653d8de4a93f0b4f5cad4c6e (patch)
tree1de73ddefe48cc9f82cb4f063eaddc069adf83bc /crates/assists
parente23bfafb32a235fdb60ba279ea68b5aa381c2110 (diff)
Phase out SourceFileEdits in favour of a plain HashMap
Diffstat (limited to 'crates/assists')
-rw-r--r--crates/assists/src/assist_context.rs28
-rw-r--r--crates/assists/src/tests.rs25
2 files changed, 19 insertions, 34 deletions
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::{
10}; 10};
11use ide_db::{ 11use ide_db::{
12 label::Label, 12 label::Label,
13 source_change::{FileSystemEdit, SourceChange, SourceFileEdits}, 13 source_change::{FileSystemEdit, SourceChange},
14 RootDatabase, 14 RootDatabase,
15}; 15};
16use syntax::{ 16use syntax::{
@@ -180,20 +180,12 @@ impl Assists {
180pub(crate) struct AssistBuilder { 180pub(crate) struct AssistBuilder {
181 edit: TextEditBuilder, 181 edit: TextEditBuilder,
182 file_id: FileId, 182 file_id: FileId,
183 is_snippet: bool, 183 source_change: SourceChange,
184 source_file_edits: SourceFileEdits,
185 file_system_edits: Vec<FileSystemEdit>,
186} 184}
187 185
188impl AssistBuilder { 186impl AssistBuilder {
189 pub(crate) fn new(file_id: FileId) -> AssistBuilder { 187 pub(crate) fn new(file_id: FileId) -> AssistBuilder {
190 AssistBuilder { 188 AssistBuilder { edit: TextEdit::builder(), file_id, source_change: SourceChange::default() }
191 edit: TextEdit::builder(),
192 file_id,
193 is_snippet: false,
194 source_file_edits: SourceFileEdits::default(),
195 file_system_edits: Vec::default(),
196 }
197 } 189 }
198 190
199 pub(crate) fn edit_file(&mut self, file_id: FileId) { 191 pub(crate) fn edit_file(&mut self, file_id: FileId) {
@@ -204,7 +196,7 @@ impl AssistBuilder {
204 fn commit(&mut self) { 196 fn commit(&mut self) {
205 let edit = mem::take(&mut self.edit).finish(); 197 let edit = mem::take(&mut self.edit).finish();
206 if !edit.is_empty() { 198 if !edit.is_empty() {
207 self.source_file_edits.insert(self.file_id, edit); 199 self.source_change.insert_source_edit(self.file_id, edit);
208 } 200 }
209 } 201 }
210 202
@@ -223,7 +215,7 @@ impl AssistBuilder {
223 offset: TextSize, 215 offset: TextSize,
224 snippet: impl Into<String>, 216 snippet: impl Into<String>,
225 ) { 217 ) {
226 self.is_snippet = true; 218 self.source_change.is_snippet = true;
227 self.insert(offset, snippet); 219 self.insert(offset, snippet);
228 } 220 }
229 /// Replaces specified `range` of text with a given string. 221 /// Replaces specified `range` of text with a given string.
@@ -237,7 +229,7 @@ impl AssistBuilder {
237 range: TextRange, 229 range: TextRange,
238 snippet: impl Into<String>, 230 snippet: impl Into<String>,
239 ) { 231 ) {
240 self.is_snippet = true; 232 self.source_change.is_snippet = true;
241 self.replace(range, snippet); 233 self.replace(range, snippet);
242 } 234 }
243 pub(crate) fn replace_ast<N: AstNode>(&mut self, old: N, new: N) { 235 pub(crate) fn replace_ast<N: AstNode>(&mut self, old: N, new: N) {
@@ -252,15 +244,11 @@ impl AssistBuilder {
252 pub(crate) fn create_file(&mut self, dst: AnchoredPathBuf, content: impl Into<String>) { 244 pub(crate) fn create_file(&mut self, dst: AnchoredPathBuf, content: impl Into<String>) {
253 let file_system_edit = 245 let file_system_edit =
254 FileSystemEdit::CreateFile { dst: dst.clone(), initial_contents: content.into() }; 246 FileSystemEdit::CreateFile { dst: dst.clone(), initial_contents: content.into() };
255 self.file_system_edits.push(file_system_edit); 247 self.source_change.push_file_system_edit(file_system_edit);
256 } 248 }
257 249
258 fn finish(mut self) -> SourceChange { 250 fn finish(mut self) -> SourceChange {
259 self.commit(); 251 self.commit();
260 SourceChange { 252 mem::take(&mut self.source_change)
261 source_file_edits: mem::take(&mut self.source_file_edits),
262 file_system_edits: mem::take(&mut self.file_system_edits),
263 is_snippet: self.is_snippet,
264 }
265 } 253 }
266} 254}
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) {
80 let actual = { 80 let actual = {
81 let source_change = assist.source_change.unwrap(); 81 let source_change = assist.source_change.unwrap();
82 let mut actual = before; 82 let mut actual = before;
83 if let Some(source_file_edit) = source_change.source_file_edits.edits.get(&file_id) { 83 if let Some(source_file_edit) = source_change.get_source_edit(file_id) {
84 source_file_edit.apply(&mut actual); 84 source_file_edit.apply(&mut actual);
85 } 85 }
86 actual 86 actual
@@ -120,7 +120,7 @@ fn check(handler: Handler, before: &str, expected: ExpectedResult, assist_label:
120 && source_change.file_system_edits.len() == 0; 120 && source_change.file_system_edits.len() == 0;
121 121
122 let mut buf = String::new(); 122 let mut buf = String::new();
123 for (file_id, edit) in source_change.source_file_edits.edits { 123 for (file_id, edit) in source_change.source_file_edits {
124 let mut text = db.file_text(file_id).as_ref().to_owned(); 124 let mut text = db.file_text(file_id).as_ref().to_owned();
125 edit.apply(&mut text); 125 edit.apply(&mut text);
126 if !skip_header { 126 if !skip_header {
@@ -132,18 +132,15 @@ fn check(handler: Handler, before: &str, expected: ExpectedResult, assist_label:
132 buf.push_str(&text); 132 buf.push_str(&text);
133 } 133 }
134 134
135 for file_system_edit in source_change.file_system_edits.clone() { 135 for file_system_edit in source_change.file_system_edits {
136 match file_system_edit { 136 if let FileSystemEdit::CreateFile { dst, initial_contents } = file_system_edit {
137 FileSystemEdit::CreateFile { dst, initial_contents } => { 137 let sr = db.file_source_root(dst.anchor);
138 let sr = db.file_source_root(dst.anchor); 138 let sr = db.source_root(sr);
139 let sr = db.source_root(sr); 139 let mut base = sr.path_for_file(&dst.anchor).unwrap().clone();
140 let mut base = sr.path_for_file(&dst.anchor).unwrap().clone(); 140 base.pop();
141 base.pop(); 141 let created_file_path = format!("{}{}", base.to_string(), &dst.path[1..]);
142 let created_file_path = format!("{}{}", base.to_string(), &dst.path[1..]); 142 format_to!(buf, "//- {}\n", created_file_path);
143 format_to!(buf, "//- {}\n", created_file_path); 143 buf.push_str(&initial_contents);
144 buf.push_str(&initial_contents);
145 }
146 _ => (),
147 } 144 }
148 } 145 }
149 146