Skip to content

Commit 356f3f4

Browse files
authored
Merge pull request rust-lang#19332 from Veykril/push-trvznlqsvtyq
Make change annotations per text-edit
2 parents 82cbddf + 1ba4391 commit 356f3f4

File tree

7 files changed

+53
-84
lines changed

7 files changed

+53
-84
lines changed

src/tools/rust-analyzer/crates/ide-assists/src/tests.rs

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -710,24 +710,21 @@ pub fn test_some_range(a: int) -> bool {
710710
Indel {
711711
insert: "let",
712712
delete: 45..47,
713-
annotation: None,
714713
},
715714
Indel {
716715
insert: "var_name",
717716
delete: 48..60,
718-
annotation: None,
719717
},
720718
Indel {
721719
insert: "=",
722720
delete: 61..81,
723-
annotation: None,
724721
},
725722
Indel {
726723
insert: "5;\n if let 2..6 = var_name {\n true\n } else {\n false\n }",
727724
delete: 82..108,
728-
annotation: None,
729725
},
730726
],
727+
annotation: None,
731728
},
732729
Some(
733730
SnippetEdit(
@@ -845,24 +842,21 @@ pub fn test_some_range(a: int) -> bool {
845842
Indel {
846843
insert: "let",
847844
delete: 45..47,
848-
annotation: None,
849845
},
850846
Indel {
851847
insert: "var_name",
852848
delete: 48..60,
853-
annotation: None,
854849
},
855850
Indel {
856851
insert: "=",
857852
delete: 61..81,
858-
annotation: None,
859853
},
860854
Indel {
861855
insert: "5;\n if let 2..6 = var_name {\n true\n } else {\n false\n }",
862856
delete: 82..108,
863-
annotation: None,
864857
},
865858
],
859+
annotation: None,
866860
},
867861
Some(
868862
SnippetEdit(
@@ -914,29 +908,25 @@ pub fn test_some_range(a: int) -> bool {
914908
Indel {
915909
insert: "const",
916910
delete: 45..47,
917-
annotation: None,
918911
},
919912
Indel {
920913
insert: "VAR_NAME:",
921914
delete: 48..60,
922-
annotation: None,
923915
},
924916
Indel {
925917
insert: "i32",
926918
delete: 61..81,
927-
annotation: None,
928919
},
929920
Indel {
930921
insert: "=",
931922
delete: 82..86,
932-
annotation: None,
933923
},
934924
Indel {
935925
insert: "5;\n if let 2..6 = VAR_NAME {\n true\n } else {\n false\n }",
936926
delete: 87..108,
937-
annotation: None,
938927
},
939928
],
929+
annotation: None,
940930
},
941931
Some(
942932
SnippetEdit(
@@ -988,29 +978,25 @@ pub fn test_some_range(a: int) -> bool {
988978
Indel {
989979
insert: "static",
990980
delete: 45..47,
991-
annotation: None,
992981
},
993982
Indel {
994983
insert: "VAR_NAME:",
995984
delete: 48..60,
996-
annotation: None,
997985
},
998986
Indel {
999987
insert: "i32",
1000988
delete: 61..81,
1001-
annotation: None,
1002989
},
1003990
Indel {
1004991
insert: "=",
1005992
delete: 82..86,
1006-
annotation: None,
1007993
},
1008994
Indel {
1009995
insert: "5;\n if let 2..6 = VAR_NAME {\n true\n } else {\n false\n }",
1010996
delete: 87..108,
1011-
annotation: None,
1012997
},
1013998
],
999+
annotation: None,
10141000
},
10151001
Some(
10161002
SnippetEdit(
@@ -1062,14 +1048,13 @@ pub fn test_some_range(a: int) -> bool {
10621048
Indel {
10631049
insert: "fun_name()",
10641050
delete: 59..60,
1065-
annotation: None,
10661051
},
10671052
Indel {
10681053
insert: "\n\nfn fun_name() -> i32 {\n 5\n}",
10691054
delete: 110..110,
1070-
annotation: None,
10711055
},
10721056
],
1057+
annotation: None,
10731058
},
10741059
Some(
10751060
SnippetEdit(

src/tools/rust-analyzer/crates/ide-completion/src/render.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2773,14 +2773,13 @@ fn foo(f: Foo) { let _: &u32 = f.b$0 }
27732773
Indel {
27742774
insert: "(",
27752775
delete: 107..107,
2776-
annotation: None,
27772776
},
27782777
Indel {
27792778
insert: "qux)()",
27802779
delete: 109..110,
2781-
annotation: None,
27822780
},
27832781
],
2782+
annotation: None,
27842783
},
27852784
kind: SymbolKind(
27862785
Field,

src/tools/rust-analyzer/crates/ide-db/src/rename.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -367,14 +367,10 @@ fn rename_reference(
367367
)
368368
}));
369369

370-
let mut insert_def_edit = |def| {
371-
let (file_id, edit) = source_edit_from_def(sema, def, new_name, &mut source_change)?;
372-
source_change.insert_source_edit(file_id, edit);
373-
Ok(())
374-
};
375370
// This needs to come after the references edits, because we change the annotation of existing edits
376371
// if a conflict is detected.
377-
insert_def_edit(def)?;
372+
let (file_id, edit) = source_edit_from_def(sema, def, new_name, &mut source_change)?;
373+
source_change.insert_source_edit(file_id, edit);
378374
Ok(source_change)
379375
}
380376

src/tools/rust-analyzer/crates/ide-db/src/text_edit.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,19 @@ pub struct Indel {
1818
pub insert: String,
1919
/// Refers to offsets in the original text
2020
pub delete: TextRange,
21-
pub annotation: Option<ChangeAnnotationId>,
2221
}
2322

2423
#[derive(Default, Debug, Clone)]
2524
pub struct TextEdit {
2625
/// Invariant: disjoint and sorted by `delete`.
2726
indels: Vec<Indel>,
27+
annotation: Option<ChangeAnnotationId>,
2828
}
2929

3030
#[derive(Debug, Default, Clone)]
3131
pub struct TextEditBuilder {
3232
indels: Vec<Indel>,
33+
annotation: Option<ChangeAnnotationId>,
3334
}
3435

3536
impl Indel {
@@ -40,7 +41,7 @@ impl Indel {
4041
Indel::replace(range, String::new())
4142
}
4243
pub fn replace(range: TextRange, replace_with: String) -> Indel {
43-
Indel { delete: range, insert: replace_with, annotation: None }
44+
Indel { delete: range, insert: replace_with }
4445
}
4546

4647
pub fn apply(&self, text: &mut String) {
@@ -142,12 +143,12 @@ impl TextEdit {
142143
Some(res)
143144
}
144145

145-
pub fn set_annotation(&mut self, annotation: Option<ChangeAnnotationId>) {
146-
if annotation.is_some() {
147-
for indel in &mut self.indels {
148-
indel.annotation = annotation;
149-
}
150-
}
146+
pub(crate) fn set_annotation(&mut self, conflict_annotation: Option<ChangeAnnotationId>) {
147+
self.annotation = conflict_annotation;
148+
}
149+
150+
pub fn change_annotation(&self) -> Option<ChangeAnnotationId> {
151+
self.annotation
151152
}
152153
}
153154

@@ -183,10 +184,10 @@ impl TextEditBuilder {
183184
self.indel(Indel::insert(offset, text));
184185
}
185186
pub fn finish(self) -> TextEdit {
186-
let mut indels = self.indels;
187+
let TextEditBuilder { mut indels, annotation } = self;
187188
assert_disjoint_or_equal(&mut indels);
188189
indels = coalesce_indels(indels);
189-
TextEdit { indels }
190+
TextEdit { indels, annotation }
190191
}
191192
pub fn invalidates_offset(&self, offset: TextSize) -> bool {
192193
self.indels.iter().any(|indel| indel.delete.contains_inclusive(offset))

0 commit comments

Comments
 (0)