Skip to content

Commit 76c2f4e

Browse files
bors[bot]lnicola
andauthored
Merge #4278
4278: Log panics in apply_document_changes r=matklad a=lnicola This doesn't necessarily help (because of #4263 (comment)), but maybe we could leave it in there for a while in case it catches something. Co-authored-by: Laurențiu Nicola <lnicola@dend.ro>
2 parents 682c079 + 074d1ac commit 76c2f4e

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

crates/rust-analyzer/src/main_loop.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,10 @@ fn apply_document_changes(
666666
mut line_index: Cow<'_, LineIndex>,
667667
content_changes: Vec<TextDocumentContentChangeEvent>,
668668
) {
669+
// Remove when https://github.com/rust-analyzer/rust-analyzer/issues/4263 is fixed.
670+
let backup_text = old_text.clone();
671+
let backup_changes = content_changes.clone();
672+
669673
// The changes we got must be applied sequentially, but can cross lines so we
670674
// have to keep our line index updated.
671675
// Some clients (e.g. Code) sort the ranges in reverse. As an optimization, we
@@ -693,7 +697,19 @@ fn apply_document_changes(
693697
}
694698
index_valid = IndexValid::UpToLine(range.start.line);
695699
let range = range.conv_with(&line_index);
696-
old_text.replace_range(Range::<usize>::from(range), &change.text);
700+
let mut text = old_text.to_owned();
701+
match std::panic::catch_unwind(move || {
702+
text.replace_range(Range::<usize>::from(range), &change.text);
703+
text
704+
}) {
705+
Ok(t) => *old_text = t,
706+
Err(e) => {
707+
eprintln!("Bug in incremental text synchronization. Please report the following output on https://github.com/rust-analyzer/rust-analyzer/issues/4263");
708+
dbg!(&backup_text);
709+
dbg!(&backup_changes);
710+
std::panic::resume_unwind(e);
711+
}
712+
}
697713
}
698714
None => {
699715
*old_text = change.text;

0 commit comments

Comments
 (0)