Skip to content

Commit

Permalink
Merge pull request #19332 from Veykril/push-trvznlqsvtyq
Browse files Browse the repository at this point in the history
Make change annotations per text-edit
  • Loading branch information
Veykril authored Mar 10, 2025
2 parents d76b785 + 7006639 commit f81fcab
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 84 deletions.
25 changes: 5 additions & 20 deletions crates/ide-assists/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -710,24 +710,21 @@ pub fn test_some_range(a: int) -> bool {
Indel {
insert: "let",
delete: 45..47,
annotation: None,
},
Indel {
insert: "var_name",
delete: 48..60,
annotation: None,
},
Indel {
insert: "=",
delete: 61..81,
annotation: None,
},
Indel {
insert: "5;\n if let 2..6 = var_name {\n true\n } else {\n false\n }",
delete: 82..108,
annotation: None,
},
],
annotation: None,
},
Some(
SnippetEdit(
Expand Down Expand Up @@ -845,24 +842,21 @@ pub fn test_some_range(a: int) -> bool {
Indel {
insert: "let",
delete: 45..47,
annotation: None,
},
Indel {
insert: "var_name",
delete: 48..60,
annotation: None,
},
Indel {
insert: "=",
delete: 61..81,
annotation: None,
},
Indel {
insert: "5;\n if let 2..6 = var_name {\n true\n } else {\n false\n }",
delete: 82..108,
annotation: None,
},
],
annotation: None,
},
Some(
SnippetEdit(
Expand Down Expand Up @@ -914,29 +908,25 @@ pub fn test_some_range(a: int) -> bool {
Indel {
insert: "const",
delete: 45..47,
annotation: None,
},
Indel {
insert: "VAR_NAME:",
delete: 48..60,
annotation: None,
},
Indel {
insert: "i32",
delete: 61..81,
annotation: None,
},
Indel {
insert: "=",
delete: 82..86,
annotation: None,
},
Indel {
insert: "5;\n if let 2..6 = VAR_NAME {\n true\n } else {\n false\n }",
delete: 87..108,
annotation: None,
},
],
annotation: None,
},
Some(
SnippetEdit(
Expand Down Expand Up @@ -988,29 +978,25 @@ pub fn test_some_range(a: int) -> bool {
Indel {
insert: "static",
delete: 45..47,
annotation: None,
},
Indel {
insert: "VAR_NAME:",
delete: 48..60,
annotation: None,
},
Indel {
insert: "i32",
delete: 61..81,
annotation: None,
},
Indel {
insert: "=",
delete: 82..86,
annotation: None,
},
Indel {
insert: "5;\n if let 2..6 = VAR_NAME {\n true\n } else {\n false\n }",
delete: 87..108,
annotation: None,
},
],
annotation: None,
},
Some(
SnippetEdit(
Expand Down Expand Up @@ -1062,14 +1048,13 @@ pub fn test_some_range(a: int) -> bool {
Indel {
insert: "fun_name()",
delete: 59..60,
annotation: None,
},
Indel {
insert: "\n\nfn fun_name() -> i32 {\n 5\n}",
delete: 110..110,
annotation: None,
},
],
annotation: None,
},
Some(
SnippetEdit(
Expand Down
3 changes: 1 addition & 2 deletions crates/ide-completion/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2773,14 +2773,13 @@ fn foo(f: Foo) { let _: &u32 = f.b$0 }
Indel {
insert: "(",
delete: 107..107,
annotation: None,
},
Indel {
insert: "qux)()",
delete: 109..110,
annotation: None,
},
],
annotation: None,
},
kind: SymbolKind(
Field,
Expand Down
8 changes: 2 additions & 6 deletions crates/ide-db/src/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,14 +367,10 @@ fn rename_reference(
)
}));

let mut insert_def_edit = |def| {
let (file_id, edit) = source_edit_from_def(sema, def, new_name, &mut source_change)?;
source_change.insert_source_edit(file_id, edit);
Ok(())
};
// This needs to come after the references edits, because we change the annotation of existing edits
// if a conflict is detected.
insert_def_edit(def)?;
let (file_id, edit) = source_edit_from_def(sema, def, new_name, &mut source_change)?;
source_change.insert_source_edit(file_id, edit);
Ok(source_change)
}

Expand Down
21 changes: 11 additions & 10 deletions crates/ide-db/src/text_edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,19 @@ pub struct Indel {
pub insert: String,
/// Refers to offsets in the original text
pub delete: TextRange,
pub annotation: Option<ChangeAnnotationId>,
}

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

#[derive(Debug, Default, Clone)]
pub struct TextEditBuilder {
indels: Vec<Indel>,
annotation: Option<ChangeAnnotationId>,
}

impl Indel {
Expand All @@ -40,7 +41,7 @@ impl Indel {
Indel::replace(range, String::new())
}
pub fn replace(range: TextRange, replace_with: String) -> Indel {
Indel { delete: range, insert: replace_with, annotation: None }
Indel { delete: range, insert: replace_with }
}

pub fn apply(&self, text: &mut String) {
Expand Down Expand Up @@ -142,12 +143,12 @@ impl TextEdit {
Some(res)
}

pub fn set_annotation(&mut self, annotation: Option<ChangeAnnotationId>) {
if annotation.is_some() {
for indel in &mut self.indels {
indel.annotation = annotation;
}
}
pub(crate) fn set_annotation(&mut self, conflict_annotation: Option<ChangeAnnotationId>) {
self.annotation = conflict_annotation;
}

pub fn change_annotation(&self) -> Option<ChangeAnnotationId> {
self.annotation
}
}

Expand Down Expand Up @@ -183,10 +184,10 @@ impl TextEditBuilder {
self.indel(Indel::insert(offset, text));
}
pub fn finish(self) -> TextEdit {
let mut indels = self.indels;
let TextEditBuilder { mut indels, annotation } = self;
assert_disjoint_or_equal(&mut indels);
indels = coalesce_indels(indels);
TextEdit { indels }
TextEdit { indels, annotation }
}
pub fn invalidates_offset(&self, offset: TextSize) -> bool {
self.indels.iter().any(|indel| indel.delete.contains_inclusive(offset))
Expand Down
Loading

0 comments on commit f81fcab

Please sign in to comment.