Be more lenient when dealing with rust-analyzer's flycheck commands (#36782)

Flycheck commands are global and makes sense to fall back to looking up
project's rust-analyzer even if the commands are run on a non-rust
buffer. If multiple rust-analyzers are found in the project, avoid
ambiguous commands and bail (as before).

Closes #ISSUE

Release Notes:

- Made it possible to run rust-analyzer's flycheck actions from anywhere
in the project
This commit is contained in:
Kirill Bulatov 2025-08-23 01:55:50 +03:00 committed by GitHub
parent 153724aad3
commit d24cad30f3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 114 additions and 64 deletions

View file

@ -9029,13 +9029,22 @@ impl LspStore {
lsp_store.update(&mut cx, |lsp_store, cx| {
if let Some(server) = lsp_store.language_server_for_id(server_id) {
let text_document = if envelope.payload.current_file_only {
let buffer_id = BufferId::new(envelope.payload.buffer_id)?;
lsp_store
.buffer_store()
.read(cx)
.get(buffer_id)
.and_then(|buffer| Some(buffer.read(cx).file()?.as_local()?.abs_path(cx)))
.map(|path| make_text_document_identifier(&path))
let buffer_id = envelope
.payload
.buffer_id
.map(|id| BufferId::new(id))
.transpose()?;
buffer_id
.and_then(|buffer_id| {
lsp_store
.buffer_store()
.read(cx)
.get(buffer_id)
.and_then(|buffer| {
Some(buffer.read(cx).file()?.as_local()?.abs_path(cx))
})
.map(|path| make_text_document_identifier(&path))
})
.transpose()?
} else {
None

View file

@ -1,8 +1,8 @@
use ::serde::{Deserialize, Serialize};
use anyhow::Context as _;
use gpui::{App, Entity, Task, WeakEntity};
use language::ServerHealth;
use lsp::{LanguageServer, LanguageServerName};
use gpui::{App, AsyncApp, Entity, Task, WeakEntity};
use language::{Buffer, ServerHealth};
use lsp::{LanguageServer, LanguageServerId, LanguageServerName};
use rpc::proto;
use crate::{LspStore, LspStoreEvent, Project, ProjectPath, lsp_store};
@ -83,31 +83,32 @@ pub fn register_notifications(lsp_store: WeakEntity<LspStore>, language_server:
pub fn cancel_flycheck(
project: Entity<Project>,
buffer_path: ProjectPath,
buffer_path: Option<ProjectPath>,
cx: &mut App,
) -> Task<anyhow::Result<()>> {
let upstream_client = project.read(cx).lsp_store().read(cx).upstream_client();
let lsp_store = project.read(cx).lsp_store();
let buffer = project.update(cx, |project, cx| {
project.buffer_store().update(cx, |buffer_store, cx| {
buffer_store.open_buffer(buffer_path, cx)
let buffer = buffer_path.map(|buffer_path| {
project.update(cx, |project, cx| {
project.buffer_store().update(cx, |buffer_store, cx| {
buffer_store.open_buffer(buffer_path, cx)
})
})
});
cx.spawn(async move |cx| {
let buffer = buffer.await?;
let Some(rust_analyzer_server) = project.read_with(cx, |project, cx| {
project.language_server_id_for_name(buffer.read(cx), &RUST_ANALYZER_NAME, cx)
})?
let buffer = match buffer {
Some(buffer) => Some(buffer.await?),
None => None,
};
let Some(rust_analyzer_server) = find_rust_analyzer_server(&project, buffer.as_ref(), cx)
else {
return Ok(());
};
let buffer_id = buffer.read_with(cx, |buffer, _| buffer.remote_id().to_proto())?;
if let Some((client, project_id)) = upstream_client {
let request = proto::LspExtCancelFlycheck {
project_id,
buffer_id,
language_server_id: rust_analyzer_server.to_proto(),
};
client
@ -130,28 +131,33 @@ pub fn cancel_flycheck(
pub fn run_flycheck(
project: Entity<Project>,
buffer_path: ProjectPath,
buffer_path: Option<ProjectPath>,
cx: &mut App,
) -> Task<anyhow::Result<()>> {
let upstream_client = project.read(cx).lsp_store().read(cx).upstream_client();
let lsp_store = project.read(cx).lsp_store();
let buffer = project.update(cx, |project, cx| {
project.buffer_store().update(cx, |buffer_store, cx| {
buffer_store.open_buffer(buffer_path, cx)
let buffer = buffer_path.map(|buffer_path| {
project.update(cx, |project, cx| {
project.buffer_store().update(cx, |buffer_store, cx| {
buffer_store.open_buffer(buffer_path, cx)
})
})
});
cx.spawn(async move |cx| {
let buffer = buffer.await?;
let Some(rust_analyzer_server) = project.read_with(cx, |project, cx| {
project.language_server_id_for_name(buffer.read(cx), &RUST_ANALYZER_NAME, cx)
})?
let buffer = match buffer {
Some(buffer) => Some(buffer.await?),
None => None,
};
let Some(rust_analyzer_server) = find_rust_analyzer_server(&project, buffer.as_ref(), cx)
else {
return Ok(());
};
let buffer_id = buffer.read_with(cx, |buffer, _| buffer.remote_id().to_proto())?;
if let Some((client, project_id)) = upstream_client {
let buffer_id = buffer
.map(|buffer| buffer.read_with(cx, |buffer, _| buffer.remote_id().to_proto()))
.transpose()?;
let request = proto::LspExtRunFlycheck {
project_id,
buffer_id,
@ -182,31 +188,32 @@ pub fn run_flycheck(
pub fn clear_flycheck(
project: Entity<Project>,
buffer_path: ProjectPath,
buffer_path: Option<ProjectPath>,
cx: &mut App,
) -> Task<anyhow::Result<()>> {
let upstream_client = project.read(cx).lsp_store().read(cx).upstream_client();
let lsp_store = project.read(cx).lsp_store();
let buffer = project.update(cx, |project, cx| {
project.buffer_store().update(cx, |buffer_store, cx| {
buffer_store.open_buffer(buffer_path, cx)
let buffer = buffer_path.map(|buffer_path| {
project.update(cx, |project, cx| {
project.buffer_store().update(cx, |buffer_store, cx| {
buffer_store.open_buffer(buffer_path, cx)
})
})
});
cx.spawn(async move |cx| {
let buffer = buffer.await?;
let Some(rust_analyzer_server) = project.read_with(cx, |project, cx| {
project.language_server_id_for_name(buffer.read(cx), &RUST_ANALYZER_NAME, cx)
})?
let buffer = match buffer {
Some(buffer) => Some(buffer.await?),
None => None,
};
let Some(rust_analyzer_server) = find_rust_analyzer_server(&project, buffer.as_ref(), cx)
else {
return Ok(());
};
let buffer_id = buffer.read_with(cx, |buffer, _| buffer.remote_id().to_proto())?;
if let Some((client, project_id)) = upstream_client {
let request = proto::LspExtClearFlycheck {
project_id,
buffer_id,
language_server_id: rust_analyzer_server.to_proto(),
};
client
@ -226,3 +233,40 @@ pub fn clear_flycheck(
anyhow::Ok(())
})
}
fn find_rust_analyzer_server(
project: &Entity<Project>,
buffer: Option<&Entity<Buffer>>,
cx: &mut AsyncApp,
) -> Option<LanguageServerId> {
project
.read_with(cx, |project, cx| {
buffer
.and_then(|buffer| {
project.language_server_id_for_name(buffer.read(cx), &RUST_ANALYZER_NAME, cx)
})
// If no rust-analyzer found for the current buffer (e.g. `settings.json`), fall back to the project lookup
// and use project's rust-analyzer if it's the only one.
.or_else(|| {
let rust_analyzer_servers = project
.lsp_store()
.read(cx)
.language_server_statuses
.iter()
.filter_map(|(server_id, server_status)| {
if server_status.name == RUST_ANALYZER_NAME {
Some(*server_id)
} else {
None
}
})
.collect::<Vec<_>>();
if rust_analyzer_servers.len() == 1 {
rust_analyzer_servers.first().copied()
} else {
None
}
})
})
.ok()?
}