Skip to content

Commit

Permalink
Add validation in LspCommand::to_lsp + check for inverted ranges (#…
Browse files Browse the repository at this point in the history
…22731)

#22690 logged errors and flipped the range in this case. Instead it
brings more visibility to the issue to return errors.

Release Notes:

- N/A
  • Loading branch information
mgsloan authored Jan 6, 2025
1 parent 1c223d8 commit 1413932
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 162 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1134,7 +1134,7 @@ impl RandomizedTest for ProjectCollaborationTest {
let end = PointUtf16::new(end_row, end_column);
let range = if start > end { end..start } else { start..end };
highlights.push(lsp::DocumentHighlight {
range: range_to_lsp(range.clone()),
range: range_to_lsp(range.clone()).unwrap(),
kind: Some(lsp::DocumentHighlightKind::READ),
});
}
Expand Down
9 changes: 5 additions & 4 deletions crates/language/src/diagnostic_set.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{range_to_lsp, Diagnostic};
use anyhow::Result;
use collections::HashMap;
use lsp::LanguageServerId;
use std::{
Expand Down Expand Up @@ -54,24 +55,24 @@ pub struct Summary {
impl DiagnosticEntry<PointUtf16> {
/// Returns a raw LSP diagnostic used to provide diagnostic context to LSP
/// codeAction request
pub fn to_lsp_diagnostic_stub(&self) -> lsp::Diagnostic {
pub fn to_lsp_diagnostic_stub(&self) -> Result<lsp::Diagnostic> {
let code = self
.diagnostic
.code
.clone()
.map(lsp::NumberOrString::String);

let range = range_to_lsp(self.range.clone());
let range = range_to_lsp(self.range.clone())?;

lsp::Diagnostic {
Ok(lsp::Diagnostic {
code,
range,
severity: Some(self.diagnostic.severity),
source: self.diagnostic.source.clone(),
message: self.diagnostic.message.clone(),
data: self.diagnostic.data.clone(),
..Default::default()
}
})
}
}

Expand Down
19 changes: 12 additions & 7 deletions crates/language/src/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1849,14 +1849,19 @@ pub fn point_from_lsp(point: lsp::Position) -> Unclipped<PointUtf16> {
Unclipped(PointUtf16::new(point.line, point.character))
}

pub fn range_to_lsp(range: Range<PointUtf16>) -> lsp::Range {
let mut start = point_to_lsp(range.start);
let mut end = point_to_lsp(range.end);
if start > end {
log::error!("range_to_lsp called with inverted range {start:?}-{end:?}");
mem::swap(&mut start, &mut end);
pub fn range_to_lsp(range: Range<PointUtf16>) -> Result<lsp::Range> {
if range.start > range.end {
Err(anyhow!(
"Inverted range provided to an LSP request: {:?}-{:?}",
range.start,
range.end
))
} else {
Ok(lsp::Range {
start: point_to_lsp(range.start),
end: point_to_lsp(range.end),
})
}
lsp::Range { start, end }
}

pub fn range_from_lsp(range: lsp::Range) -> Range<Unclipped<PointUtf16>> {
Expand Down
Loading

0 comments on commit 1413932

Please sign in to comment.