accept identical Indels when merging; add rename test case

This commit is contained in:
Anatol Ulrich 2021-11-02 21:38:38 +01:00
parent 1ac35532c4
commit 9bce4d6696
2 changed files with 57 additions and 15 deletions

View file

@ -100,6 +100,7 @@ pub(crate) fn rename(
def.rename(&sema, new_name) def.rename(&sema, new_name)
}) })
.collect(); .collect();
ops?.into_iter() ops?.into_iter()
.reduce(|acc, elem| acc.merge(elem)) .reduce(|acc, elem| acc.merge(elem))
.ok_or_else(|| format_err!("No references found at position")) .ok_or_else(|| format_err!("No references found at position"))
@ -186,13 +187,14 @@ fn find_definitions(
res res
}); });
let res: RenameResult<Vec<(ast::NameLike, Definition)>> = symbols.collect(); let res: RenameResult<Vec<_>> = symbols.collect();
match res { match res {
// remove duplicates
Ok(v) => { Ok(v) => {
if v.is_empty() { if v.is_empty() {
// FIXME: some semantic duplication between "empty vec" and "Err()"
Err(format_err!("No references found at position")) Err(format_err!("No references found at position"))
} else { } else {
// remove duplicates, comparing `Definition`s
Ok(v.into_iter().unique_by(|t| t.1)) Ok(v.into_iter().unique_by(|t| t.1))
} }
} }
@ -569,6 +571,36 @@ fn main() {
); );
} }
#[test]
fn test_rename_macro_multiple_occurrences() {
check(
"Baaah",
r#"macro_rules! foo {
($ident:ident) => {
const $ident: () = ();
struct $ident {}
};
}
foo!($0Foo);
const _: () = Foo;
const _: Foo = Foo {};
"#,
r#"
macro_rules! foo {
($ident:ident) => {
const $ident: () = ();
struct $ident {}
};
}
foo!(Baaah);
const _: () = Baaah;
const _: Baaah = Baaah {};
"#,
)
}
#[test] #[test]
fn test_rename_for_macro_args() { fn test_rename_for_macro_args() {
check( check(

View file

@ -3,12 +3,14 @@
//! `rust-analyzer` never mutates text itself and only sends diffs to clients, //! `rust-analyzer` never mutates text itself and only sends diffs to clients,
//! so `TextEdit` is the ultimate representation of the work done by //! so `TextEdit` is the ultimate representation of the work done by
//! rust-analyzer. //! rust-analyzer.
use std::collections::HashSet;
pub use text_size::{TextRange, TextSize}; pub use text_size::{TextRange, TextSize};
/// `InsertDelete` -- a single "atomic" change to text /// `InsertDelete` -- a single "atomic" change to text
/// ///
/// Must not overlap with other `InDel`s /// Must not overlap with other `InDel`s
#[derive(Debug, Clone)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Indel { pub struct Indel {
pub insert: String, pub insert: String,
/// Refers to offsets in the original text /// Refers to offsets in the original text
@ -114,13 +116,20 @@ impl TextEdit {
} }
pub fn union(&mut self, other: TextEdit) -> Result<(), TextEdit> { pub fn union(&mut self, other: TextEdit) -> Result<(), TextEdit> {
dbg!(&self, &other);
// FIXME: can be done without allocating intermediate vector // FIXME: can be done without allocating intermediate vector
let mut all = self.iter().chain(other.iter()).collect::<Vec<_>>(); let mut all = self.iter().chain(other.iter()).collect::<Vec<_>>();
if !check_disjoint(&mut all) { if !check_disjoint_or_equal(&mut all) {
return Err(other); return Err(other);
} }
self.indels.extend(other.indels);
assert_disjoint(&mut self.indels); // remove duplicates
// FIXME: maybe make indels a HashSet instead to get rid of this?
let our_indels = self.indels.clone();
let our_indels = our_indels.iter().collect::<HashSet<_>>();
let other_indels = other.indels.into_iter().filter(|i| !our_indels.contains(i));
self.indels.extend(other_indels);
Ok(()) Ok(())
} }
@ -173,7 +182,7 @@ impl TextEditBuilder {
} }
pub fn finish(self) -> TextEdit { pub fn finish(self) -> TextEdit {
let mut indels = self.indels; let mut indels = self.indels;
assert_disjoint(&mut indels); assert_disjoint_or_equal(&mut indels);
TextEdit { indels } TextEdit { indels }
} }
pub fn invalidates_offset(&self, offset: TextSize) -> bool { pub fn invalidates_offset(&self, offset: TextSize) -> bool {
@ -182,18 +191,19 @@ impl TextEditBuilder {
fn indel(&mut self, indel: Indel) { fn indel(&mut self, indel: Indel) {
self.indels.push(indel); self.indels.push(indel);
if self.indels.len() <= 16 { if self.indels.len() <= 16 {
assert_disjoint(&mut self.indels); assert_disjoint_or_equal(&mut self.indels);
} }
} }
} }
fn assert_disjoint(indels: &mut [impl std::borrow::Borrow<Indel>]) { fn assert_disjoint_or_equal(indels: &mut [impl std::borrow::Borrow<Indel>]) {
assert!(check_disjoint(indels)); assert!(check_disjoint_or_equal(indels));
} }
fn check_disjoint(indels: &mut [impl std::borrow::Borrow<Indel>]) -> bool { fn check_disjoint_or_equal(indels: &mut [impl std::borrow::Borrow<Indel>]) -> bool {
indels.sort_by_key(|indel| (indel.borrow().delete.start(), indel.borrow().delete.end())); indels.sort_by_key(|indel| (indel.borrow().delete.start(), indel.borrow().delete.end()));
indels indels.iter().zip(indels.iter().skip(1)).all(|(l, r)| {
.iter() let l = l.borrow();
.zip(indels.iter().skip(1)) let r = r.borrow();
.all(|(l, r)| l.borrow().delete.end() <= r.borrow().delete.start()) l.delete.end() <= r.delete.start() || l == r
})
} }