[ty] Show related information in diagnostic (#17359)

This commit is contained in:
Micha Reiser 2025-05-19 18:52:12 +02:00 committed by GitHub
parent 55a410a885
commit 6985de4c40
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 159 additions and 5 deletions

View file

@ -7,11 +7,13 @@ use lsp_types::{
NumberOrString, Range, RelatedFullDocumentDiagnosticReport,
};
use crate::document::ToRangeExt;
use crate::PositionEncoding;
use crate::document::{FileRangeExt, ToRangeExt};
use crate::server::api::traits::{BackgroundDocumentRequestHandler, RequestHandler};
use crate::server::{Result, client::Notifier};
use crate::session::DocumentSnapshot;
use ruff_db::diagnostic::Severity;
use ruff_db::diagnostic::{Annotation, Severity, SubDiagnostic};
use ruff_db::files::FileRange;
use ruff_db::source::{line_index, source_text};
use ty_project::{Db, ProjectDatabase};
@ -116,6 +118,31 @@ fn to_lsp_diagnostic(
})
.flatten();
let mut related_information = Vec::new();
related_information.extend(
diagnostic
.secondary_annotations()
.filter_map(|annotation| annotation_to_related_information(db, annotation, encoding)),
);
for sub_diagnostic in diagnostic.sub_diagnostics() {
related_information.extend(sub_diagnostic_to_related_information(
db,
sub_diagnostic,
encoding,
));
related_information.extend(
sub_diagnostic
.annotations()
.iter()
.filter_map(|annotation| {
annotation_to_related_information(db, annotation, encoding)
}),
);
}
Diagnostic {
range,
severity: Some(severity),
@ -124,7 +151,41 @@ fn to_lsp_diagnostic(
code_description,
source: Some("ty".into()),
message: diagnostic.concise_message().to_string(),
related_information: None,
related_information: Some(related_information),
data: None,
}
}
fn annotation_to_related_information(
db: &dyn Db,
annotation: &Annotation,
encoding: PositionEncoding,
) -> Option<lsp_types::DiagnosticRelatedInformation> {
let span = annotation.get_span();
let annotation_message = annotation.get_message()?;
let range = FileRange::try_from(span).ok()?;
let location = range.to_location(db.upcast(), encoding)?;
Some(lsp_types::DiagnosticRelatedInformation {
location,
message: annotation_message.to_string(),
})
}
fn sub_diagnostic_to_related_information(
db: &dyn Db,
diagnostic: &SubDiagnostic,
encoding: PositionEncoding,
) -> Option<lsp_types::DiagnosticRelatedInformation> {
let primary_annotation = diagnostic.primary_annotation()?;
let span = primary_annotation.get_span();
let range = FileRange::try_from(span).ok()?;
let location = range.to_location(db.upcast(), encoding)?;
Some(lsp_types::DiagnosticRelatedInformation {
location,
message: diagnostic.concise_message().to_string(),
})
}

View file

@ -38,8 +38,7 @@ impl ResolvedClientCapabilities {
let document_changes = client_capabilities
.workspace
.as_ref()
.and_then(|workspace| workspace.workspace_edit.as_ref())
.and_then(|workspace_edit| workspace_edit.document_changes)
.and_then(|workspace| workspace.workspace_edit.as_ref()?.document_changes)
.unwrap_or_default();
let declaration_link_support = client_capabilities