Skip to content

Commit

Permalink
Protect against panics from DidChangeTextDocument
Browse files Browse the repository at this point in the history
Summary: These panics were generated by experimenting with emacs.

Reviewed By: jcpetruzza

Differential Revision: D59684333

fbshipit-source-id: d4bf59614491d04d4b5e38b03eb44ccca6dc5a84
  • Loading branch information
alanz authored and facebook-github-bot committed Jul 12, 2024
1 parent e055181 commit 6de370c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
20 changes: 16 additions & 4 deletions crates/elp/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::ops::Range;
use elp_ide::elp_ide_db::LineIndex;
use lsp_types::TextDocumentContentChangeEvent;

use crate::from_proto::text_range;
use crate::from_proto::safe_text_range;
use crate::line_endings::LineEndings;

pub struct Document {
Expand Down Expand Up @@ -71,9 +71,21 @@ impl Document {
line_index = LineIndex::new(&self.content);
}
index_valid = IndexValid::UpToLineExclusive(range.start.line);
let range = text_range(&line_index, range);
self.content
.replace_range(Range::<usize>::from(range), &change.text);
if let Some(range) = safe_text_range(&line_index, range) {
if self.content.is_char_boundary(range.start().into())
&& self.content.is_char_boundary(range.end().into())
{
self.content
.replace_range(Range::<usize>::from(range), &change.text);
} else {
log::warn!(
"discarding invalid document change char boundary: {:?}",
&change
);
}
} else {
log::warn!("discarding invalid document change range: {:?}", &change);
}
}
None => {
self.content = change.text;
Expand Down
7 changes: 6 additions & 1 deletion crates/ide_db/src/line_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,12 @@ impl LineIndex {
pub fn offset(&self, line_col: LineCol) -> TextSize {
//FIXME: return Result
let col = self.utf16_to_utf8_col(line_col.line, line_col.col_utf16);
self.newlines[line_col.line as usize] + col
if let Some(offset) = self.newlines.get(line_col.line as usize) {
offset + col
} else {
log::warn!("line_index.offset, limiting");
self.newlines[self.newlines.len() - 1] + col
}
}

pub fn safe_offset(&self, line_col: LineCol) -> Option<TextSize> {
Expand Down

0 comments on commit 6de370c

Please sign in to comment.