Skip to content

Commit 0665d41

Browse files
committed
feat: Implement diagnostics pull model
1 parent 16785c8 commit 0665d41

File tree

4 files changed

+83
-17
lines changed

4 files changed

+83
-17
lines changed

crates/rust-analyzer/src/diagnostics.rs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -173,21 +173,6 @@ pub(crate) fn fetch_native_diagnostics(
173173
let _p = tracing::info_span!("fetch_native_diagnostics").entered();
174174
let _ctx = stdx::panic_context::enter("fetch_native_diagnostics".to_owned());
175175

176-
let convert_diagnostic =
177-
|line_index: &crate::line_index::LineIndex, d: ide::Diagnostic| lsp_types::Diagnostic {
178-
range: lsp::to_proto::range(line_index, d.range.range),
179-
severity: Some(lsp::to_proto::diagnostic_severity(d.severity)),
180-
code: Some(lsp_types::NumberOrString::String(d.code.as_str().to_owned())),
181-
code_description: Some(lsp_types::CodeDescription {
182-
href: lsp_types::Url::parse(&d.code.url()).unwrap(),
183-
}),
184-
source: Some("rust-analyzer".to_owned()),
185-
message: d.message,
186-
related_information: None,
187-
tags: d.unused.then(|| vec![lsp_types::DiagnosticTag::UNNECESSARY]),
188-
data: None,
189-
};
190-
191176
// the diagnostics produced may point to different files not requested by the concrete request,
192177
// put those into here and filter later
193178
let mut odd_ones = Vec::new();
@@ -246,3 +231,22 @@ pub(crate) fn fetch_native_diagnostics(
246231
}
247232
diagnostics
248233
}
234+
235+
pub(crate) fn convert_diagnostic(
236+
line_index: &crate::line_index::LineIndex,
237+
d: ide::Diagnostic,
238+
) -> lsp_types::Diagnostic {
239+
lsp_types::Diagnostic {
240+
range: lsp::to_proto::range(line_index, d.range.range),
241+
severity: Some(lsp::to_proto::diagnostic_severity(d.severity)),
242+
code: Some(lsp_types::NumberOrString::String(d.code.as_str().to_owned())),
243+
code_description: Some(lsp_types::CodeDescription {
244+
href: lsp_types::Url::parse(&d.code.url()).unwrap(),
245+
}),
246+
source: Some("rust-analyzer".to_owned()),
247+
message: d.message,
248+
related_information: None,
249+
tags: d.unused.then(|| vec![lsp_types::DiagnosticTag::UNNECESSARY]),
250+
data: None,
251+
}
252+
}

crates/rust-analyzer/src/handlers/request.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
//! Protocol. This module specifically handles requests.
33
44
use std::{
5+
collections::HashMap,
56
fs,
67
io::Write as _,
8+
ops::Not,
79
process::{self, Stdio},
810
};
911

@@ -36,6 +38,7 @@ use vfs::{AbsPath, AbsPathBuf, FileId, VfsPath};
3638

3739
use crate::{
3840
config::{Config, RustfmtConfig, WorkspaceSymbolConfig},
41+
diagnostics::convert_diagnostic,
3942
global_state::{FetchWorkspaceRequest, GlobalState, GlobalStateSnapshot},
4043
hack_recover_crate_name,
4144
line_index::LineEndings,
@@ -473,6 +476,49 @@ pub(crate) fn handle_on_type_formatting(
473476
Ok(Some(change))
474477
}
475478

479+
pub(crate) fn handle_document_diagnostics(
480+
snap: GlobalStateSnapshot,
481+
params: lsp_types::DocumentDiagnosticParams,
482+
) -> anyhow::Result<lsp_types::DocumentDiagnosticReportResult> {
483+
let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
484+
let source_root = snap.analysis.source_root_id(file_id)?;
485+
let line_index = snap.file_line_index(file_id)?;
486+
let config = snap.config.diagnostics(Some(source_root));
487+
if !config.enabled {
488+
return Ok(Default::default());
489+
}
490+
let supports_related = snap.config.text_document_diagnostic().unwrap_or(false);
491+
492+
let mut related_documents = HashMap::new();
493+
let diagnostics = snap
494+
.analysis
495+
.full_diagnostics(&config, AssistResolveStrategy::None, file_id)?
496+
.into_iter()
497+
.filter_map(|d| {
498+
let file = d.range.file_id;
499+
let diagnostic = convert_diagnostic(&line_index, d);
500+
if file == file_id {
501+
return Some(diagnostic);
502+
}
503+
if supports_related {
504+
related_documents.entry(file).or_insert_with(Vec::new).push(diagnostic);
505+
}
506+
None
507+
});
508+
// FIXME: flycheck diagnostics
509+
Ok(lsp_types::DocumentDiagnosticReportResult::Report(
510+
lsp_types::DocumentDiagnosticReport::Full(lsp_types::RelatedFullDocumentDiagnosticReport {
511+
related_documents: related_documents.is_empty().not().then(|| {
512+
related_documents.into_iter().map(|(id, d)| (to_proto::url(&snap, id), d)).collect()
513+
}),
514+
full_document_diagnostic_report: lsp_types::FullDocumentDiagnosticReport {
515+
result_id: None,
516+
items: diagnostics.collect(),
517+
},
518+
}),
519+
))
520+
}
521+
476522
pub(crate) fn handle_document_symbol(
477523
snap: GlobalStateSnapshot,
478524
params: lsp_types::DocumentSymbolParams,

crates/rust-analyzer/src/lsp/capabilities.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,15 @@ pub fn server_capabilities(config: &Config) -> ServerCapabilities {
155155
"ssr": true,
156156
"workspaceSymbolScopeKindFiltering": true,
157157
})),
158-
diagnostic_provider: None,
158+
diagnostic_provider: Some(lsp_types::DiagnosticServerCapabilities::Options(
159+
lsp_types::DiagnosticOptions {
160+
identifier: None,
161+
inter_file_dependencies: true,
162+
// FIXME
163+
workspace_diagnostics: false,
164+
work_done_progress_options: WorkDoneProgressOptions { work_done_progress: None },
165+
},
166+
)),
159167
inline_completion_provider: None,
160168
}
161169
}
@@ -380,6 +388,10 @@ impl ClientCapabilities {
380388
.unwrap_or_default()
381389
}
382390

391+
pub fn text_document_diagnostic(&self) -> Option<bool> {
392+
(|| -> _ { self.0.text_document.as_ref()?.diagnostic.as_ref()?.related_document_support })()
393+
}
394+
383395
pub fn code_action_group(&self) -> bool {
384396
self.experimental_bool("codeActionGroup")
385397
}

crates/rust-analyzer/src/main_loop.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,10 @@ impl GlobalState {
438438

439439
let project_or_mem_docs_changed =
440440
became_quiescent || state_changed || memdocs_added_or_removed;
441-
if project_or_mem_docs_changed && self.config.publish_diagnostics(None) {
441+
if project_or_mem_docs_changed
442+
&& self.config.text_document_diagnostic().is_none()
443+
&& self.config.publish_diagnostics(None)
444+
{
442445
self.update_diagnostics();
443446
}
444447
if project_or_mem_docs_changed && self.config.test_explorer() {
@@ -1080,6 +1083,7 @@ impl GlobalState {
10801083
.on_latency_sensitive::<NO_RETRY, lsp_request::SemanticTokensRangeRequest>(handlers::handle_semantic_tokens_range)
10811084
// FIXME: Some of these NO_RETRY could be retries if the file they are interested didn't change.
10821085
// All other request handlers
1086+
.on::<RETRY, lsp_request::DocumentDiagnosticRequest>(handlers::handle_document_diagnostics)
10831087
.on::<RETRY, lsp_request::DocumentSymbolRequest>(handlers::handle_document_symbol)
10841088
.on::<RETRY, lsp_request::FoldingRangeRequest>(handlers::handle_folding_range)
10851089
.on::<NO_RETRY, lsp_request::SignatureHelpRequest>(handlers::handle_signature_help)

0 commit comments

Comments
 (0)