Skip to content

Commit

Permalink
robustly handle invalid LSP ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalkuthe committed Mar 31, 2023
1 parent 3cf0372 commit 4b5f548
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions helix-lsp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,11 @@ pub mod util {
) -> Option<usize> {
let pos_line = pos.line as usize;
if pos_line > doc.len_lines() - 1 {
return None;
// If it extends past the end, truncate it to the end. This is because the
// way the LSP describes the range including the last newline is by
// specifying a line number after what we would call the last line.
pos_line = doc.len_lines() - 1;
return Some(doc.len_chars());
}

// We need to be careful here to fully comply ith the LSP spec.
Expand Down Expand Up @@ -239,9 +243,20 @@ pub mod util {

pub fn lsp_range_to_range(
doc: &Rope,
range: lsp::Range,
mut range: lsp::Range,
offset_encoding: OffsetEncoding,
) -> Option<Range> {
// This is sort of an edgecase. It's not clear from the spec how to deal with
// ranges where end < start. They don't make much sense but vscode simply caps start to end
// and because it's not specified quite a few LS rely on this as a result (for example the TS server)
if range.start > range.end {
log::error!(
"Invalid LSP range start {:?} > end {:?}, using an empty range at the end instead",
range.start,
range.end
);
range.start = range.end;
}
let start = lsp_pos_to_pos(doc, range.start, offset_encoding)?;
let end = lsp_pos_to_pos(doc, range.end, offset_encoding)?;

Expand Down

0 comments on commit 4b5f548

Please sign in to comment.