@@ -132,7 +132,11 @@ pub mod util {
132132 ) -> Option<usize> {
133133 let pos_line = pos.line as usize;
134134 if pos_line > doc.len_lines() - 1 {
135− return None;
135+ // If it extends past the end, truncate it to the end. This is because the
136+ // way the LSP describes the range including the last newline is by
137+ // specifying a line number after what we would call the last line.
138+ log::warn!("LSP position {pos:?} out of range assuming EOF");
139+ return Some(doc.len_chars());
136140 }
137141
138142 // We need to be careful here to fully comply ith the LSP spec.
@@ -242,9 +246,20 @@ pub mod util {
242246
243247 pub fn lsp_range_to_range(
244248 doc: &Rope,
245− range: lsp::Range,
249+ mut range: lsp::Range,
246250 offset_encoding: OffsetEncoding,
247251 ) -> Option<Range> {
252+ // This is sort of an edgecase. It's not clear from the spec how to deal with
253+ // ranges where end < start. They don't make much sense but vscode simply caps start to end
254+ // and because it's not specified quite a few LS rely on this as a result (for example the TS server)
255+ if range.start > range.end {
256+ log::error!(
257+ "Invalid LSP range start {:?} > end {:?}, using an empty range at the end instead",
258+ range.start,
259+ range.end
260+ );
261+ range.start = range.end;
262+ }
248263 let start = lsp_pos_to_pos(doc, range.start, offset_encoding)?;
249264 let end = lsp_pos_to_pos(doc, range.end, offset_encoding)?;
250265
@@ -951,16 +966,16 @@ mod tests {
951966
952967 test_case!("", (0, 0) => Some(0));
953968 test_case!("", (0, 1) => Some(0));
954− test_case!("", (1, 0) => None);
969+ test_case!("", (1, 0) => Some(0));
955970 test_case!("\n\n", (0, 0) => Some(0));
956971 test_case!("\n\n", (1, 0) => Some(1));
957972 test_case!("\n\n", (1, 1) => Some(1));
958973 test_case!("\n\n", (2, 0) => Some(2));
959− test_case!("\n\n", (3, 0) => None);
974+ test_case!("\n\n", (3, 0) => Some(2));
960975 test_case!("test\n\n\n\ncase", (4, 3) => Some(11));
961976 test_case!("test\n\n\n\ncase", (4, 4) => Some(12));
962977 test_case!("test\n\n\n\ncase", (4, 5) => Some(12));
963− test_case!("", (u32::MAX, u32::MAX) => None);
978+ test_case!("", (u32::MAX, u32::MAX) => Some(0));
964979 }
965980
966981 #[test]