Skip to content

Commit

Permalink
lsp: handle pull-based diagnostics model for related documents
Browse files Browse the repository at this point in the history
  • Loading branch information
vitallium committed Dec 26, 2024
1 parent 73be1fd commit 26f6751
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
2 changes: 1 addition & 1 deletion crates/lsp/src/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ impl LanguageServer {
}),
diagnostic: Some(DiagnosticClientCapabilities {
dynamic_registration: Some(false),
related_document_support: Some(false),
related_document_support: Some(true),
}),
..TextDocumentClientCapabilities::default()
}),
Expand Down
50 changes: 37 additions & 13 deletions crates/project/src/lsp_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3260,36 +3260,60 @@ impl LspCommand for GetDocumentDiagnostics {
async fn response_from_lsp(
self,
message: lsp::DocumentDiagnosticReportResult,
_: Model<LspStore>,
lsp_store: Model<LspStore>,
buffer: Model<Buffer>,
server_id: LanguageServerId,
cx: AsyncAppContext,
mut cx: AsyncAppContext,
) -> Result<Self::Response> {
let uri = buffer.read_with(&cx, |buffer, cx| {
let file = buffer.file().and_then(|file| file.as_local())?;
Some(lsp::Url::from_file_path(file.abs_path(cx).clone()).unwrap())
buffer
.file()
.and_then(|file| file.as_local())
.map(|file| lsp::Url::from_file_path(file.abs_path(cx).clone()).unwrap())
})?;

if uri.is_none() {
let Some(uri) = uri else {
return Ok(None);
}
};

match message {
lsp::DocumentDiagnosticReportResult::Report(report) => match report {
lsp::DocumentDiagnosticReport::Full(report) => Ok(Some(LspDiagnostics {
server_id,
uri,
diagnostics: Some(report.full_document_diagnostic_report.items.clone()),
})),
lsp::DocumentDiagnosticReport::Full(report) => {
lsp_store.update(&mut cx, |store, cx| {
for (uri, report) in report.related_documents.into_iter().flatten() {
match report {
lsp::DocumentDiagnosticReportKind::Full(report) => {
store.update_diagnostics(
server_id,
lsp::PublishDiagnosticsParams {
diagnostics: report.items.clone(),
uri,
version: None,
},
&[],
cx,
).expect("Failed to update diagnostics for the related document");
}
lsp::DocumentDiagnosticReportKind::Unchanged(_) => (),
}
}
}).expect("Failed to update diagnostics for related documents");

Ok(Some(LspDiagnostics {
server_id,
uri: Some(uri),
diagnostics: Some(report.full_document_diagnostic_report.items.clone()),
}))
}
lsp::DocumentDiagnosticReport::Unchanged(_) => Ok(Some(LspDiagnostics {
server_id,
uri,
uri: Some(uri),
diagnostics: None,
})),
},
lsp::DocumentDiagnosticReportResult::Partial(_) => Ok(Some(LspDiagnostics {
server_id,
uri,
uri: Some(uri),
diagnostics: None,
})),
}
Expand Down

0 comments on commit 26f6751

Please sign in to comment.