diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-01-15 09:43:08 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2021-01-15 09:43:08 +0000 |
commit | 054e2061521292a72748510f3f6cb7c8b1e8611b (patch) | |
tree | c1660a95eae4aaa57ea5546de0d81678d49f68f1 /crates/assists/src | |
parent | dc48de28d8460903dbfc9454c8cae0e17d62e9c1 (diff) | |
parent | d5095329a1c12e93653d8de4a93f0b4f5cad4c6e (diff) |
Merge #7272
7272: Group file source edits by FileId r=matklad a=Veykril
Co-authored-by: Lukas Wirth <[email protected]>
Diffstat (limited to 'crates/assists/src')
-rw-r--r-- | crates/assists/src/assist_context.rs | 36 | ||||
-rw-r--r-- | crates/assists/src/tests.rs | 40 |
2 files changed, 25 insertions, 51 deletions
diff --git a/crates/assists/src/assist_context.rs b/crates/assists/src/assist_context.rs index 91cc63427..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 | }; |
11 | use ide_db::{ | 11 | use ide_db::{ |
12 | label::Label, | 12 | label::Label, |
13 | source_change::{FileSystemEdit, SourceChange, SourceFileEdit}, | 13 | source_change::{FileSystemEdit, SourceChange}, |
14 | RootDatabase, | 14 | RootDatabase, |
15 | }; | 15 | }; |
16 | use syntax::{ | 16 | use syntax::{ |
@@ -180,20 +180,12 @@ impl Assists { | |||
180 | pub(crate) struct AssistBuilder { | 180 | pub(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: Vec<SourceFileEdit>, | ||
185 | file_system_edits: Vec<FileSystemEdit>, | ||
186 | } | 184 | } |
187 | 185 | ||
188 | impl AssistBuilder { | 186 | impl 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: Vec::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,15 +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 | match self.source_file_edits.binary_search_by_key(&self.file_id, |edit| edit.file_id) { | 199 | self.source_change.insert_source_edit(self.file_id, edit); |
208 | Ok(idx) => self.source_file_edits[idx] | ||
209 | .edit | ||
210 | .union(edit) | ||
211 | .expect("overlapping edits for same file"), | ||
212 | Err(idx) => self | ||
213 | .source_file_edits | ||
214 | .insert(idx, SourceFileEdit { file_id: self.file_id, edit }), | ||
215 | } | ||
216 | } | 200 | } |
217 | } | 201 | } |
218 | 202 | ||
@@ -231,7 +215,7 @@ impl AssistBuilder { | |||
231 | offset: TextSize, | 215 | offset: TextSize, |
232 | snippet: impl Into<String>, | 216 | snippet: impl Into<String>, |
233 | ) { | 217 | ) { |
234 | self.is_snippet = true; | 218 | self.source_change.is_snippet = true; |
235 | self.insert(offset, snippet); | 219 | self.insert(offset, snippet); |
236 | } | 220 | } |
237 | /// Replaces specified `range` of text with a given string. | 221 | /// Replaces specified `range` of text with a given string. |
@@ -245,7 +229,7 @@ impl AssistBuilder { | |||
245 | range: TextRange, | 229 | range: TextRange, |
246 | snippet: impl Into<String>, | 230 | snippet: impl Into<String>, |
247 | ) { | 231 | ) { |
248 | self.is_snippet = true; | 232 | self.source_change.is_snippet = true; |
249 | self.replace(range, snippet); | 233 | self.replace(range, snippet); |
250 | } | 234 | } |
251 | 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) { |
@@ -260,15 +244,11 @@ impl AssistBuilder { | |||
260 | 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>) { |
261 | let file_system_edit = | 245 | let file_system_edit = |
262 | FileSystemEdit::CreateFile { dst: dst.clone(), initial_contents: content.into() }; | 246 | FileSystemEdit::CreateFile { dst: dst.clone(), initial_contents: content.into() }; |
263 | self.file_system_edits.push(file_system_edit); | 247 | self.source_change.push_file_system_edit(file_system_edit); |
264 | } | 248 | } |
265 | 249 | ||
266 | fn finish(mut self) -> SourceChange { | 250 | fn finish(mut self) -> SourceChange { |
267 | self.commit(); | 251 | self.commit(); |
268 | SourceChange { | 252 | mem::take(&mut self.source_change) |
269 | source_file_edits: mem::take(&mut self.source_file_edits), | ||
270 | file_system_edits: mem::take(&mut self.file_system_edits), | ||
271 | is_snippet: self.is_snippet, | ||
272 | } | ||
273 | } | 253 | } |
274 | } | 254 | } |
diff --git a/crates/assists/src/tests.rs b/crates/assists/src/tests.rs index fef29a0b8..71431b406 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) { | |||
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 | for source_file_edit in source_change.source_file_edits { | 83 | if let Some(source_file_edit) = source_change.get_source_edit(file_id) { |
84 | if source_file_edit.file_id == file_id { | 84 | source_file_edit.apply(&mut actual); |
85 | source_file_edit.edit.apply(&mut actual) | ||
86 | } | ||
87 | } | 85 | } |
88 | actual | 86 | actual |
89 | }; | 87 | }; |
@@ -116,37 +114,33 @@ fn check(handler: Handler, before: &str, expected: ExpectedResult, assist_label: | |||
116 | 114 | ||
117 | match (assist, expected) { | 115 | match (assist, expected) { |
118 | (Some(assist), ExpectedResult::After(after)) => { | 116 | (Some(assist), ExpectedResult::After(after)) => { |
119 | let mut source_change = assist.source_change.unwrap(); | 117 | let source_change = assist.source_change.unwrap(); |
120 | assert!(!source_change.source_file_edits.is_empty()); | 118 | assert!(!source_change.source_file_edits.is_empty()); |
121 | let skip_header = source_change.source_file_edits.len() == 1 | 119 | let skip_header = source_change.source_file_edits.len() == 1 |
122 | && source_change.file_system_edits.len() == 0; | 120 | && source_change.file_system_edits.len() == 0; |
123 | source_change.source_file_edits.sort_by_key(|it| it.file_id); | ||
124 | 121 | ||
125 | let mut buf = String::new(); | 122 | let mut buf = String::new(); |
126 | for source_file_edit in source_change.source_file_edits { | 123 | for (file_id, edit) in source_change.source_file_edits { |
127 | let mut text = db.file_text(source_file_edit.file_id).as_ref().to_owned(); | 124 | let mut text = db.file_text(file_id).as_ref().to_owned(); |
128 | source_file_edit.edit.apply(&mut text); | 125 | edit.apply(&mut text); |
129 | if !skip_header { | 126 | if !skip_header { |
130 | let sr = db.file_source_root(source_file_edit.file_id); | 127 | let sr = db.file_source_root(file_id); |
131 | let sr = db.source_root(sr); | 128 | let sr = db.source_root(sr); |
132 | let path = sr.path_for_file(&source_file_edit.file_id).unwrap(); | 129 | let path = sr.path_for_file(&file_id).unwrap(); |
133 | format_to!(buf, "//- {}\n", path) | 130 | format_to!(buf, "//- {}\n", path) |
134 | } | 131 | } |
135 | buf.push_str(&text); | 132 | buf.push_str(&text); |
136 | } | 133 | } |
137 | 134 | ||
138 | for file_system_edit in source_change.file_system_edits.clone() { | 135 | for file_system_edit in source_change.file_system_edits { |
139 | match file_system_edit { | 136 | if let FileSystemEdit::CreateFile { dst, initial_contents } = file_system_edit { |
140 | FileSystemEdit::CreateFile { dst, initial_contents } => { | 137 | let sr = db.file_source_root(dst.anchor); |
141 | let sr = db.file_source_root(dst.anchor); | 138 | let sr = db.source_root(sr); |
142 | let sr = db.source_root(sr); | 139 | let mut base = sr.path_for_file(&dst.anchor).unwrap().clone(); |
143 | let mut base = sr.path_for_file(&dst.anchor).unwrap().clone(); | 140 | base.pop(); |
144 | base.pop(); | 141 | let created_file_path = format!("{}{}", base.to_string(), &dst.path[1..]); |
145 | let created_file_path = format!("{}{}", base.to_string(), &dst.path[1..]); | 142 | format_to!(buf, "//- {}\n", created_file_path); |
146 | format_to!(buf, "//- {}\n", created_file_path); | 143 | buf.push_str(&initial_contents); |
147 | buf.push_str(&initial_contents); | ||
148 | } | ||
149 | _ => (), | ||
150 | } | 144 | } |
151 | } | 145 | } |
152 | 146 | ||