diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-05-05 22:50:47 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2020-05-05 22:50:47 +0100 |
commit | 30eb458b4fa8adcecd8cbf731bd1cfa9a7a8b88b (patch) | |
tree | 1ee318a9568c8bf1d0b314ef996581bb4e518b9e /crates/ra_ide | |
parent | 78c82eff95cae43b389dbd6e590d7f09bac8f3f1 (diff) | |
parent | ca9e0f5fe9ad29ab0c5a0771a0d0eaec97e4104b (diff) |
Merge #4332
4332: Refactor TextEdit r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_ide')
-rw-r--r-- | crates/ra_ide/src/completion/completion_context.rs | 4 | ||||
-rw-r--r-- | crates/ra_ide/src/completion/completion_item.rs | 4 | ||||
-rw-r--r-- | crates/ra_ide/src/diagnostics.rs | 24 | ||||
-rw-r--r-- | crates/ra_ide/src/join_lines.rs | 6 | ||||
-rw-r--r-- | crates/ra_ide/src/references/rename.rs | 32 | ||||
-rw-r--r-- | crates/ra_ide/src/ssr.rs | 32 | ||||
-rw-r--r-- | crates/ra_ide/src/test_utils.rs | 6 | ||||
-rw-r--r-- | crates/ra_ide/src/typing.rs | 9 | ||||
-rw-r--r-- | crates/ra_ide/src/typing/on_enter.rs | 3 |
9 files changed, 77 insertions, 43 deletions
diff --git a/crates/ra_ide/src/completion/completion_context.rs b/crates/ra_ide/src/completion/completion_context.rs index dd87bd119..b6b9627de 100644 --- a/crates/ra_ide/src/completion/completion_context.rs +++ b/crates/ra_ide/src/completion/completion_context.rs | |||
@@ -9,7 +9,7 @@ use ra_syntax::{ | |||
9 | SyntaxKind::*, | 9 | SyntaxKind::*, |
10 | SyntaxNode, SyntaxToken, TextRange, TextSize, | 10 | SyntaxNode, SyntaxToken, TextRange, TextSize, |
11 | }; | 11 | }; |
12 | use ra_text_edit::AtomTextEdit; | 12 | use ra_text_edit::Indel; |
13 | 13 | ||
14 | use crate::{call_info::ActiveParameter, completion::CompletionConfig, FilePosition}; | 14 | use crate::{call_info::ActiveParameter, completion::CompletionConfig, FilePosition}; |
15 | 15 | ||
@@ -76,7 +76,7 @@ impl<'a> CompletionContext<'a> { | |||
76 | // actual completion. | 76 | // actual completion. |
77 | let file_with_fake_ident = { | 77 | let file_with_fake_ident = { |
78 | let parse = db.parse(position.file_id); | 78 | let parse = db.parse(position.file_id); |
79 | let edit = AtomTextEdit::insert(position.offset, "intellijRulezz".to_string()); | 79 | let edit = Indel::insert(position.offset, "intellijRulezz".to_string()); |
80 | parse.reparse(&edit).tree() | 80 | parse.reparse(&edit).tree() |
81 | }; | 81 | }; |
82 | let fake_ident_token = | 82 | let fake_ident_token = |
diff --git a/crates/ra_ide/src/completion/completion_item.rs b/crates/ra_ide/src/completion/completion_item.rs index 5936fb8f7..383b23ac4 100644 --- a/crates/ra_ide/src/completion/completion_item.rs +++ b/crates/ra_ide/src/completion/completion_item.rs | |||
@@ -62,8 +62,8 @@ impl fmt::Debug for CompletionItem { | |||
62 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | 62 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
63 | let mut s = f.debug_struct("CompletionItem"); | 63 | let mut s = f.debug_struct("CompletionItem"); |
64 | s.field("label", &self.label()).field("source_range", &self.source_range()); | 64 | s.field("label", &self.label()).field("source_range", &self.source_range()); |
65 | if self.text_edit().as_atoms().len() == 1 { | 65 | if self.text_edit().as_indels().len() == 1 { |
66 | let atom = &self.text_edit().as_atoms()[0]; | 66 | let atom = &self.text_edit().as_indels()[0]; |
67 | s.field("delete", &atom.delete); | 67 | s.field("delete", &atom.delete); |
68 | s.field("insert", &atom.insert); | 68 | s.field("insert", &atom.insert); |
69 | } else { | 69 | } else { |
diff --git a/crates/ra_ide/src/diagnostics.rs b/crates/ra_ide/src/diagnostics.rs index 4c04cee07..87a0b80f1 100644 --- a/crates/ra_ide/src/diagnostics.rs +++ b/crates/ra_ide/src/diagnostics.rs | |||
@@ -241,7 +241,11 @@ mod tests { | |||
241 | diagnostics.pop().unwrap_or_else(|| panic!("no diagnostics for:\n{}\n", before)); | 241 | diagnostics.pop().unwrap_or_else(|| panic!("no diagnostics for:\n{}\n", before)); |
242 | let mut fix = diagnostic.fix.unwrap(); | 242 | let mut fix = diagnostic.fix.unwrap(); |
243 | let edit = fix.source_file_edits.pop().unwrap().edit; | 243 | let edit = fix.source_file_edits.pop().unwrap().edit; |
244 | let actual = edit.apply(&before); | 244 | let actual = { |
245 | let mut actual = before.to_string(); | ||
246 | edit.apply(&mut actual); | ||
247 | actual | ||
248 | }; | ||
245 | assert_eq_text!(after, &actual); | 249 | assert_eq_text!(after, &actual); |
246 | } | 250 | } |
247 | 251 | ||
@@ -256,7 +260,11 @@ mod tests { | |||
256 | let mut fix = diagnostic.fix.unwrap(); | 260 | let mut fix = diagnostic.fix.unwrap(); |
257 | let edit = fix.source_file_edits.pop().unwrap().edit; | 261 | let edit = fix.source_file_edits.pop().unwrap().edit; |
258 | let target_file_contents = analysis.file_text(file_position.file_id).unwrap(); | 262 | let target_file_contents = analysis.file_text(file_position.file_id).unwrap(); |
259 | let actual = edit.apply(&target_file_contents); | 263 | let actual = { |
264 | let mut actual = target_file_contents.to_string(); | ||
265 | edit.apply(&mut actual); | ||
266 | actual | ||
267 | }; | ||
260 | 268 | ||
261 | // Strip indent and empty lines from `after`, to match the behaviour of | 269 | // Strip indent and empty lines from `after`, to match the behaviour of |
262 | // `parse_fixture` called from `analysis_and_position`. | 270 | // `parse_fixture` called from `analysis_and_position`. |
@@ -288,7 +296,11 @@ mod tests { | |||
288 | let diagnostic = analysis.diagnostics(file_id).unwrap().pop().unwrap(); | 296 | let diagnostic = analysis.diagnostics(file_id).unwrap().pop().unwrap(); |
289 | let mut fix = diagnostic.fix.unwrap(); | 297 | let mut fix = diagnostic.fix.unwrap(); |
290 | let edit = fix.source_file_edits.pop().unwrap().edit; | 298 | let edit = fix.source_file_edits.pop().unwrap().edit; |
291 | let actual = edit.apply(&before); | 299 | let actual = { |
300 | let mut actual = before.to_string(); | ||
301 | edit.apply(&mut actual); | ||
302 | actual | ||
303 | }; | ||
292 | assert_eq_text!(after, &actual); | 304 | assert_eq_text!(after, &actual); |
293 | } | 305 | } |
294 | 306 | ||
@@ -662,10 +674,10 @@ mod tests { | |||
662 | 1, | 674 | 1, |
663 | ), | 675 | ), |
664 | edit: TextEdit { | 676 | edit: TextEdit { |
665 | atoms: [ | 677 | indels: [ |
666 | AtomTextEdit { | 678 | Indel { |
667 | delete: 3..9, | ||
668 | insert: "{a:42, b: ()}", | 679 | insert: "{a:42, b: ()}", |
680 | delete: 3..9, | ||
669 | }, | 681 | }, |
670 | ], | 682 | ], |
671 | }, | 683 | }, |
diff --git a/crates/ra_ide/src/join_lines.rs b/crates/ra_ide/src/join_lines.rs index 63fd6b3e4..d3af780c4 100644 --- a/crates/ra_ide/src/join_lines.rs +++ b/crates/ra_ide/src/join_lines.rs | |||
@@ -569,7 +569,11 @@ fn foo() { | |||
569 | let (sel, before) = extract_range(before); | 569 | let (sel, before) = extract_range(before); |
570 | let parse = SourceFile::parse(&before); | 570 | let parse = SourceFile::parse(&before); |
571 | let result = join_lines(&parse.tree(), sel); | 571 | let result = join_lines(&parse.tree(), sel); |
572 | let actual = result.apply(&before); | 572 | let actual = { |
573 | let mut actual = before.to_string(); | ||
574 | result.apply(&mut actual); | ||
575 | actual | ||
576 | }; | ||
573 | assert_eq_text!(after, &actual); | 577 | assert_eq_text!(after, &actual); |
574 | } | 578 | } |
575 | 579 | ||
diff --git a/crates/ra_ide/src/references/rename.rs b/crates/ra_ide/src/references/rename.rs index 52e55b0a0..0398d53bc 100644 --- a/crates/ra_ide/src/references/rename.rs +++ b/crates/ra_ide/src/references/rename.rs | |||
@@ -537,10 +537,10 @@ mod tests { | |||
537 | 2, | 537 | 2, |
538 | ), | 538 | ), |
539 | edit: TextEdit { | 539 | edit: TextEdit { |
540 | atoms: [ | 540 | indels: [ |
541 | AtomTextEdit { | 541 | Indel { |
542 | delete: 4..7, | ||
543 | insert: "foo2", | 542 | insert: "foo2", |
543 | delete: 4..7, | ||
544 | }, | 544 | }, |
545 | ], | 545 | ], |
546 | }, | 546 | }, |
@@ -589,10 +589,10 @@ mod tests { | |||
589 | 1, | 589 | 1, |
590 | ), | 590 | ), |
591 | edit: TextEdit { | 591 | edit: TextEdit { |
592 | atoms: [ | 592 | indels: [ |
593 | AtomTextEdit { | 593 | Indel { |
594 | delete: 4..7, | ||
595 | insert: "foo2", | 594 | insert: "foo2", |
595 | delete: 4..7, | ||
596 | }, | 596 | }, |
597 | ], | 597 | ], |
598 | }, | 598 | }, |
@@ -672,10 +672,10 @@ mod tests { | |||
672 | 2, | 672 | 2, |
673 | ), | 673 | ), |
674 | edit: TextEdit { | 674 | edit: TextEdit { |
675 | atoms: [ | 675 | indels: [ |
676 | AtomTextEdit { | 676 | Indel { |
677 | delete: 8..11, | ||
678 | insert: "foo2", | 677 | insert: "foo2", |
678 | delete: 8..11, | ||
679 | }, | 679 | }, |
680 | ], | 680 | ], |
681 | }, | 681 | }, |
@@ -685,10 +685,10 @@ mod tests { | |||
685 | 1, | 685 | 1, |
686 | ), | 686 | ), |
687 | edit: TextEdit { | 687 | edit: TextEdit { |
688 | atoms: [ | 688 | indels: [ |
689 | AtomTextEdit { | 689 | Indel { |
690 | delete: 27..30, | ||
691 | insert: "foo2", | 690 | insert: "foo2", |
691 | delete: 27..30, | ||
692 | }, | 692 | }, |
693 | ], | 693 | ], |
694 | }, | 694 | }, |
@@ -720,13 +720,13 @@ mod tests { | |||
720 | if let Some(change) = source_change { | 720 | if let Some(change) = source_change { |
721 | for edit in change.info.source_file_edits { | 721 | for edit in change.info.source_file_edits { |
722 | file_id = Some(edit.file_id); | 722 | file_id = Some(edit.file_id); |
723 | for atom in edit.edit.as_atoms() { | 723 | for indel in edit.edit.as_indels() { |
724 | text_edit_builder.replace(atom.delete, atom.insert.clone()); | 724 | text_edit_builder.replace(indel.delete, indel.insert.clone()); |
725 | } | 725 | } |
726 | } | 726 | } |
727 | } | 727 | } |
728 | let result = | 728 | let mut result = analysis.file_text(file_id.unwrap()).unwrap().to_string(); |
729 | text_edit_builder.finish().apply(&*analysis.file_text(file_id.unwrap()).unwrap()); | 729 | text_edit_builder.finish().apply(&mut result); |
730 | assert_eq_text!(expected, &*result); | 730 | assert_eq_text!(expected, &*result); |
731 | } | 731 | } |
732 | } | 732 | } |
diff --git a/crates/ra_ide/src/ssr.rs b/crates/ra_ide/src/ssr.rs index 7b93ff2d2..8bf52d0fa 100644 --- a/crates/ra_ide/src/ssr.rs +++ b/crates/ra_ide/src/ssr.rs | |||
@@ -401,16 +401,22 @@ fn render_replace( | |||
401 | ignored_comments: &Vec<Comment>, | 401 | ignored_comments: &Vec<Comment>, |
402 | template: &SsrTemplate, | 402 | template: &SsrTemplate, |
403 | ) -> String { | 403 | ) -> String { |
404 | let mut builder = TextEditBuilder::default(); | 404 | let edit = { |
405 | for element in template.template.descendants() { | 405 | let mut builder = TextEditBuilder::default(); |
406 | if let Some(var) = template.placeholders.get(&element) { | 406 | for element in template.template.descendants() { |
407 | builder.replace(element.text_range(), binding[var].to_string()) | 407 | if let Some(var) = template.placeholders.get(&element) { |
408 | builder.replace(element.text_range(), binding[var].to_string()) | ||
409 | } | ||
408 | } | 410 | } |
409 | } | 411 | for comment in ignored_comments { |
410 | for comment in ignored_comments { | 412 | builder.insert(template.template.text_range().end(), comment.syntax().to_string()) |
411 | builder.insert(template.template.text_range().end(), comment.syntax().to_string()) | 413 | } |
412 | } | 414 | builder.finish() |
413 | builder.finish().apply(&template.template.text().to_string()) | 415 | }; |
416 | |||
417 | let mut text = template.template.text().to_string(); | ||
418 | edit.apply(&mut text); | ||
419 | text | ||
414 | } | 420 | } |
415 | 421 | ||
416 | #[cfg(test)] | 422 | #[cfg(test)] |
@@ -505,7 +511,9 @@ mod tests { | |||
505 | ); | 511 | ); |
506 | 512 | ||
507 | let edit = replace(&matches, &query.template); | 513 | let edit = replace(&matches, &query.template); |
508 | assert_eq!(edit.apply(input), "fn main() { bar(1+2); }"); | 514 | let mut after = input.to_string(); |
515 | edit.apply(&mut after); | ||
516 | assert_eq!(after, "fn main() { bar(1+2); }"); | ||
509 | } | 517 | } |
510 | 518 | ||
511 | fn assert_ssr_transform(query: &str, input: &str, result: &str) { | 519 | fn assert_ssr_transform(query: &str, input: &str, result: &str) { |
@@ -513,7 +521,9 @@ mod tests { | |||
513 | let code = SourceFile::parse(input).tree(); | 521 | let code = SourceFile::parse(input).tree(); |
514 | let matches = find(&query.pattern, code.syntax()); | 522 | let matches = find(&query.pattern, code.syntax()); |
515 | let edit = replace(&matches, &query.template); | 523 | let edit = replace(&matches, &query.template); |
516 | assert_eq!(edit.apply(input), result); | 524 | let mut after = input.to_string(); |
525 | edit.apply(&mut after); | ||
526 | assert_eq!(after, result); | ||
517 | } | 527 | } |
518 | 528 | ||
519 | #[test] | 529 | #[test] |
diff --git a/crates/ra_ide/src/test_utils.rs b/crates/ra_ide/src/test_utils.rs index f14533e14..48c8fd1f4 100644 --- a/crates/ra_ide/src/test_utils.rs +++ b/crates/ra_ide/src/test_utils.rs | |||
@@ -13,7 +13,11 @@ pub fn check_action<F: Fn(&SourceFile, TextSize) -> Option<TextEdit>>( | |||
13 | let (before_cursor_pos, before) = extract_offset(before); | 13 | let (before_cursor_pos, before) = extract_offset(before); |
14 | let file = SourceFile::parse(&before).ok().unwrap(); | 14 | let file = SourceFile::parse(&before).ok().unwrap(); |
15 | let result = f(&file, before_cursor_pos).expect("code action is not applicable"); | 15 | let result = f(&file, before_cursor_pos).expect("code action is not applicable"); |
16 | let actual = result.apply(&before); | 16 | let actual = { |
17 | let mut actual = before.to_string(); | ||
18 | result.apply(&mut actual); | ||
19 | actual | ||
20 | }; | ||
17 | let actual_cursor_pos = | 21 | let actual_cursor_pos = |
18 | result.apply_to_offset(before_cursor_pos).expect("cursor position is affected by the edit"); | 22 | result.apply_to_offset(before_cursor_pos).expect("cursor position is affected by the edit"); |
19 | let actual = add_cursor(&actual, actual_cursor_pos); | 23 | let actual = add_cursor(&actual, actual_cursor_pos); |
diff --git a/crates/ra_ide/src/typing.rs b/crates/ra_ide/src/typing.rs index 2a8b4327f..a03da4693 100644 --- a/crates/ra_ide/src/typing.rs +++ b/crates/ra_ide/src/typing.rs | |||
@@ -142,10 +142,13 @@ mod tests { | |||
142 | fn do_type_char(char_typed: char, before: &str) -> Option<(String, SingleFileChange)> { | 142 | fn do_type_char(char_typed: char, before: &str) -> Option<(String, SingleFileChange)> { |
143 | let (offset, before) = extract_offset(before); | 143 | let (offset, before) = extract_offset(before); |
144 | let edit = TextEdit::insert(offset, char_typed.to_string()); | 144 | let edit = TextEdit::insert(offset, char_typed.to_string()); |
145 | let before = edit.apply(&before); | 145 | let mut before = before.to_string(); |
146 | edit.apply(&mut before); | ||
146 | let parse = SourceFile::parse(&before); | 147 | let parse = SourceFile::parse(&before); |
147 | on_char_typed_inner(&parse.tree(), offset, char_typed) | 148 | on_char_typed_inner(&parse.tree(), offset, char_typed).map(|it| { |
148 | .map(|it| (it.edit.apply(&before), it)) | 149 | it.edit.apply(&mut before); |
150 | (before.to_string(), it) | ||
151 | }) | ||
149 | } | 152 | } |
150 | 153 | ||
151 | fn type_char(char_typed: char, before: &str, after: &str) { | 154 | fn type_char(char_typed: char, before: &str, after: &str) { |
diff --git a/crates/ra_ide/src/typing/on_enter.rs b/crates/ra_ide/src/typing/on_enter.rs index 725237464..78a40cc94 100644 --- a/crates/ra_ide/src/typing/on_enter.rs +++ b/crates/ra_ide/src/typing/on_enter.rs | |||
@@ -96,7 +96,8 @@ mod tests { | |||
96 | let result = analysis.on_enter(FilePosition { offset, file_id }).unwrap()?; | 96 | let result = analysis.on_enter(FilePosition { offset, file_id }).unwrap()?; |
97 | 97 | ||
98 | assert_eq!(result.source_file_edits.len(), 1); | 98 | assert_eq!(result.source_file_edits.len(), 1); |
99 | let actual = result.source_file_edits[0].edit.apply(&before); | 99 | let mut actual = before.to_string(); |
100 | result.source_file_edits[0].edit.apply(&mut actual); | ||
100 | let actual = add_cursor(&actual, result.cursor_position.unwrap().offset); | 101 | let actual = add_cursor(&actual, result.cursor_position.unwrap().offset); |
101 | Some(actual) | 102 | Some(actual) |
102 | } | 103 | } |