Allow to fetch cargo diagnostics separately (#29706)
Adjusts the way `cargo` and `rust-analyzer` diagnostics are fetched into Zed. Nothing is changed for defaults: in this mode, Zed does nothing but reports file updates, which trigger rust-analyzers' mechanisms: * generating internal diagnostics, which it is able to produce on the fly, without blocking cargo lock. Unfortunately, there are not that many diagnostics in r-a, and some of them have false-positives compared to rustc ones * running `cargo check --workspace --all-targets` on each file save, taking the cargo lock For large projects like Zed, this might take a while, reducing the ability to choose how to work with the project: e.g. it's impossible to save multiple times without long diagnostics refreshes (may happen automatically on e.g. focus loss), save the project and run it instantly without waiting for cargo check to finish, etc. In addition, it's relatively tricky to reconfigure r-a to run a different command, with different arguments and maybe different env vars: that would require a language server restart (and a large project reindex) and fiddling with multiple JSON fields. The new mode aims to separate out cargo diagnostics into its own loop so that all Zed diagnostics features are supported still. For that, an extra mode was introduced: ```jsonc "rust": { // When enabled, Zed runs `cargo check --message-format=json`-based commands and // collect cargo diagnostics instead of rust-analyzer. "fetch_cargo_diagnostics": false, // A command override for fetching the cargo diagnostics. // First argument is the command, followed by the arguments. "diagnostics_fetch_command": [ "cargo", "check", "--quiet", "--workspace", "--message-format=json", "--all-targets", "--keep-going" ], // Extra environment variables to pass to the diagnostics fetch command. "env": {} } ``` which calls to cargo, parses its output and mixes in with the existing diagnostics: https://github.com/user-attachments/assets/e986f955-b452-4995-8aac-3049683dd22c Release Notes: - Added a way to get diagnostics from cargo and rust-analyzer without mutually locking each other - Added `ctrl-r` binding to refresh diagnostics in the project diagnostics editor context
This commit is contained in:
parent
5e4be013af
commit
e07ffe7cf1
20 changed files with 1306 additions and 89 deletions
|
@ -298,9 +298,9 @@ impl super::LspAdapter for CLspAdapter {
|
|||
&self,
|
||||
params: &mut lsp::PublishDiagnosticsParams,
|
||||
server_id: LanguageServerId,
|
||||
buffer_access: Option<&'_ Buffer>,
|
||||
buffer: Option<&'_ Buffer>,
|
||||
) {
|
||||
if let Some(buffer) = buffer_access {
|
||||
if let Some(buffer) = buffer {
|
||||
let snapshot = buffer.snapshot();
|
||||
let inactive_regions = buffer
|
||||
.get_diagnostics(server_id)
|
||||
|
|
|
@ -8,6 +8,7 @@ use http_client::github::AssetKind;
|
|||
use http_client::github::{GitHubLspBinaryVersion, latest_github_release};
|
||||
pub use language::*;
|
||||
use lsp::{InitializeParams, LanguageServerBinary};
|
||||
use project::lsp_store::rust_analyzer_ext::CARGO_DIAGNOSTICS_SOURCE_NAME;
|
||||
use project::project_settings::ProjectSettings;
|
||||
use regex::Regex;
|
||||
use serde_json::json;
|
||||
|
@ -252,13 +253,22 @@ impl LspAdapter for RustLspAdapter {
|
|||
}
|
||||
|
||||
fn disk_based_diagnostic_sources(&self) -> Vec<String> {
|
||||
vec!["rustc".into()]
|
||||
vec![CARGO_DIAGNOSTICS_SOURCE_NAME.to_owned()]
|
||||
}
|
||||
|
||||
fn disk_based_diagnostics_progress_token(&self) -> Option<String> {
|
||||
Some("rust-analyzer/flycheck".into())
|
||||
}
|
||||
|
||||
fn retain_old_diagnostic(&self, previous_diagnostic: &Diagnostic, cx: &App) -> bool {
|
||||
let zed_provides_cargo_diagnostics = ProjectSettings::get_global(cx)
|
||||
.diagnostics
|
||||
.fetch_cargo_diagnostics();
|
||||
// Zed manages the lifecycle of cargo diagnostics when configured so.
|
||||
zed_provides_cargo_diagnostics
|
||||
&& previous_diagnostic.source.as_deref() == Some(CARGO_DIAGNOSTICS_SOURCE_NAME)
|
||||
}
|
||||
|
||||
fn process_diagnostics(
|
||||
&self,
|
||||
params: &mut lsp::PublishDiagnosticsParams,
|
||||
|
@ -499,12 +509,27 @@ impl LspAdapter for RustLspAdapter {
|
|||
"kinds": [ "cargo", "shell" ],
|
||||
},
|
||||
});
|
||||
if let Some(ref mut original_experimental) = original.capabilities.experimental {
|
||||
if let Some(original_experimental) = &mut original.capabilities.experimental {
|
||||
merge_json_value_into(experimental, original_experimental);
|
||||
} else {
|
||||
original.capabilities.experimental = Some(experimental);
|
||||
}
|
||||
}
|
||||
|
||||
let zed_provides_cargo_diagnostics = ProjectSettings::get_global(cx)
|
||||
.diagnostics
|
||||
.fetch_cargo_diagnostics();
|
||||
if zed_provides_cargo_diagnostics {
|
||||
let disable_check_on_save = json!({
|
||||
"checkOnSave": false,
|
||||
});
|
||||
if let Some(initialization_options) = &mut original.initialization_options {
|
||||
merge_json_value_into(disable_check_on_save, initialization_options);
|
||||
} else {
|
||||
original.initialization_options = Some(disable_check_on_save);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(original)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue