New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Panic when using Fixer: assertion failed: (left == right)
#146
Comments
I noticed that the original code from rust-analyzer has changed since you've copied it over, e.g. through https://github.com/rust-lang/rust-analyzer/pull/11573/files Since I don't actually need most of the functionality in that use rslint_parser::TextRange;
struct Change(usize, String);
pub trait TextEdit {
fn load(text: impl ToString) -> TextEditor;
fn insert_after(&mut self, range: TextRange, text: impl ToString);
fn insert_before(&mut self, range: TextRange, text: impl ToString);
fn apply(&mut self) -> String;
}
pub struct TextEditor {
changes: Vec<Change>,
source: String,
}
impl TextEdit for TextEditor {
fn load(text: impl ToString) -> TextEditor {
TextEditor {
changes: vec![],
source: text.to_string(),
}
}
fn insert_after(&mut self, range: TextRange, text: impl ToString) {
self.changes
.push(Change(range.end().into(), text.to_string()));
}
fn insert_before(&mut self, range: TextRange, text: impl ToString) {
self.changes
.push(Change(range.start().into(), text.to_string()));
}
fn apply(&mut self) -> String {
let new_source_length = {
let total_insertion_length: usize =
self.changes.iter().map(|change| change.1.len()).sum();
self.source.len() + total_insertion_length
};
self.changes.sort_by(|a, b| a.0.cmp(&b.0));
let mut buf = String::with_capacity(new_source_length);
let mut pointer = 0usize;
for change in &self.changes {
let current_pointer = change.0;
if current_pointer > pointer {
buf.push_str(&self.source[pointer..current_pointer]);
}
buf.push_str(change.1.as_str());
pointer = current_pointer
}
buf.push_str(&self.source[pointer..self.source.len()]);
buf
}
} |
Sorry for replying so late! But thank you for looking into it and finding the fixed version from rust-analyser. |
Has this fix been merged yet? I ran into a similar error where changing the order of two replacements "fixes" it and wondering if this is some kind of regression/different issue or the same. |
I'm using rslint's
Fixer
to make changes in source code but have found that certain order of operations cause an internal panic. I have narrowed it down to this:If
insert_interface()
is called beforeupdate_parameters()
then I get the intended output:However if I swap them around and call
update_parameters()
first, I receive this:I also found that if I call
update_parameters()
first but usefixer.insert_after()
for the interface (as opposed toinsert_before
), it also works.The text was updated successfully, but these errors were encountered: