aboutsummaryrefslogtreecommitdiff
path: root/crates/assists/src/assist_context.rs
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-01-15 09:43:08 +0000
committerGitHub <[email protected]>2021-01-15 09:43:08 +0000
commit054e2061521292a72748510f3f6cb7c8b1e8611b (patch)
treec1660a95eae4aaa57ea5546de0d81678d49f68f1 /crates/assists/src/assist_context.rs
parentdc48de28d8460903dbfc9454c8cae0e17d62e9c1 (diff)
parentd5095329a1c12e93653d8de4a93f0b4f5cad4c6e (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/assist_context.rs')
-rw-r--r--crates/assists/src/assist_context.rs36
1 files changed, 8 insertions, 28 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};
11use ide_db::{ 11use 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};
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: Vec<SourceFileEdit>,
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: 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}