mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-02 14:51:25 +00:00
[red-knot] Use line/column for server diagnostics if available (#12881)
## Summary This PR adds very basic support for using the line / column information from the diagnostic message. This makes it easier to validate diagnostics in an editor as oppose to going through the diff one diagnostic at a time and confirming it at the location.
This commit is contained in:
parent
7fc39ad624
commit
05c35b6975
1 changed files with 35 additions and 13 deletions
|
@ -2,8 +2,9 @@ use std::borrow::Cow;
|
||||||
|
|
||||||
use lsp_types::request::DocumentDiagnosticRequest;
|
use lsp_types::request::DocumentDiagnosticRequest;
|
||||||
use lsp_types::{
|
use lsp_types::{
|
||||||
Diagnostic, DocumentDiagnosticParams, DocumentDiagnosticReport, DocumentDiagnosticReportResult,
|
Diagnostic, DiagnosticSeverity, DocumentDiagnosticParams, DocumentDiagnosticReport,
|
||||||
FullDocumentDiagnosticReport, Range, RelatedFullDocumentDiagnosticReport, Url,
|
DocumentDiagnosticReportResult, FullDocumentDiagnosticReport, Position, Range,
|
||||||
|
RelatedFullDocumentDiagnosticReport, Url,
|
||||||
};
|
};
|
||||||
|
|
||||||
use red_knot_workspace::db::RootDatabase;
|
use red_knot_workspace::db::RootDatabase;
|
||||||
|
@ -56,9 +57,31 @@ fn compute_diagnostics(snapshot: &DocumentSnapshot, db: &RootDatabase) -> Vec<Di
|
||||||
diagnostics
|
diagnostics
|
||||||
.as_slice()
|
.as_slice()
|
||||||
.iter()
|
.iter()
|
||||||
.map(|message| Diagnostic {
|
.map(|message| to_lsp_diagnostic(message))
|
||||||
range: Range::default(),
|
.collect()
|
||||||
severity: None,
|
}
|
||||||
|
|
||||||
|
fn to_lsp_diagnostic(message: &str) -> Diagnostic {
|
||||||
|
let words = message.split(':').collect::<Vec<_>>();
|
||||||
|
|
||||||
|
let (range, message) = match words.as_slice() {
|
||||||
|
[_filename, line, column, message] => {
|
||||||
|
let line = line.parse::<u32>().unwrap_or_default();
|
||||||
|
let column = column.parse::<u32>().unwrap_or_default();
|
||||||
|
(
|
||||||
|
Range::new(
|
||||||
|
Position::new(line.saturating_sub(1), column.saturating_sub(1)),
|
||||||
|
Position::new(line, column),
|
||||||
|
),
|
||||||
|
message.trim(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
_ => (Range::default(), message),
|
||||||
|
};
|
||||||
|
|
||||||
|
Diagnostic {
|
||||||
|
range,
|
||||||
|
severity: Some(DiagnosticSeverity::ERROR),
|
||||||
tags: None,
|
tags: None,
|
||||||
code: None,
|
code: None,
|
||||||
code_description: None,
|
code_description: None,
|
||||||
|
@ -66,6 +89,5 @@ fn compute_diagnostics(snapshot: &DocumentSnapshot, db: &RootDatabase) -> Vec<Di
|
||||||
message: message.to_string(),
|
message: message.to_string(),
|
||||||
related_information: None,
|
related_information: None,
|
||||||
data: None,
|
data: None,
|
||||||
})
|
}
|
||||||
.collect()
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue