lsp: Implement support for the textDocument/diagnostic command (#19230)

Closes [#13107](https://github.com/zed-industries/zed/issues/13107)

Enabled pull diagnostics by default, for the language servers that
declare support in the corresponding capabilities.

```
"diagnostics": {
    "lsp_pull_diagnostics_debounce_ms": null
}
```
settings can be used to disable the pulling.

Release Notes:

- Added support for the LSP `textDocument/diagnostic` command.

# Brief

This is draft PR that implements the LSP `textDocument/diagnostic`
command. The goal is to receive your feedback and establish further
steps towards fully implementing this command. I tried to re-use
existing method and structures to ensure:

1. The existing functionality works as before
2. There is no interference between the diagnostics sent by a server and
the diagnostics requested by a client.

The current implementation is done via a new LSP command
`GetDocumentDiagnostics` that is sent when a buffer is saved and when a
buffer is edited. There is a new method called `pull_diagnostic` that is
called for such events. It has debounce to ensure we don't spam a server
with commands every time the buffer is edited. Probably, we don't need
the debounce when the buffer is saved.

All in all, the goal is basically to get your feedback and ensure I am
on the right track. Thanks!


## References

1.
https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_pullDiagnostics

## In action

You can clone any Ruby repo since the `ruby-lsp` supports the pull
diagnostics only.

Steps to reproduce:

1. Clone this repo https://github.com/vitallium/stimulus-lsp-error-zed
2. Install Ruby (via `asdf` or `mise).
4. Install Ruby gems via `bundle install`
5. Install Ruby LSP with `gem install ruby-lsp`
6. Check out this PR and build Zed
7. Open any file and start editing to see diagnostics in realtime.



https://github.com/user-attachments/assets/0ef6ec41-e4fa-4539-8f2c-6be0d8be4129

---------

Co-authored-by: Kirill Bulatov <mail4score@gmail.com>
Co-authored-by: Kirill Bulatov <kirill@zed.dev>
This commit is contained in:
Vitaly Slobodin 2025-06-05 21:42:52 +02:00 committed by GitHub
parent 04cd3fcd23
commit 7aa70a4858
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
24 changed files with 1408 additions and 124 deletions

View file

@ -74,8 +74,9 @@ pub use element::{
};
use feature_flags::{DebuggerFeatureFlag, FeatureFlagAppExt};
use futures::{
FutureExt,
FutureExt, StreamExt as _,
future::{self, Shared, join},
stream::FuturesUnordered,
};
use fuzzy::{StringMatch, StringMatchCandidate};
@ -108,9 +109,10 @@ pub use items::MAX_TAB_TITLE_LEN;
use itertools::Itertools;
use language::{
AutoindentMode, BracketMatch, BracketPair, Buffer, Capability, CharKind, CodeLabel,
CursorShape, DiagnosticEntry, DiffOptions, DocumentationConfig, EditPredictionsMode,
EditPreview, HighlightedText, IndentKind, IndentSize, Language, OffsetRangeExt, Point,
Selection, SelectionGoal, TextObject, TransactionId, TreeSitterOptions, WordsQuery,
CursorShape, DiagnosticEntry, DiagnosticSourceKind, DiffOptions, DocumentationConfig,
EditPredictionsMode, EditPreview, HighlightedText, IndentKind, IndentSize, Language,
OffsetRangeExt, Point, Selection, SelectionGoal, TextObject, TransactionId, TreeSitterOptions,
WordsQuery,
language_settings::{
self, InlayHintSettings, LspInsertMode, RewrapBehavior, WordsCompletionMode,
all_language_settings, language_settings,
@ -123,7 +125,7 @@ use markdown::Markdown;
use mouse_context_menu::MouseContextMenu;
use persistence::DB;
use project::{
BreakpointWithPosition, CompletionResponse, ProjectPath,
BreakpointWithPosition, CompletionResponse, LspPullDiagnostics, ProjectPath,
debugger::{
breakpoint_store::{
BreakpointEditAction, BreakpointSessionState, BreakpointState, BreakpointStore,
@ -1072,6 +1074,7 @@ pub struct Editor {
tasks_update_task: Option<Task<()>>,
breakpoint_store: Option<Entity<BreakpointStore>>,
gutter_breakpoint_indicator: (Option<PhantomBreakpointIndicator>, Option<Task<()>>),
pull_diagnostics_task: Task<()>,
in_project_search: bool,
previous_search_ranges: Option<Arc<[Range<Anchor>]>>,
breadcrumb_header: Option<String>,
@ -1690,6 +1693,10 @@ impl Editor {
editor.tasks_update_task =
Some(editor.refresh_runnables(window, cx));
}
editor.pull_diagnostics(window, cx);
}
project::Event::RefreshDocumentsDiagnostics => {
editor.pull_diagnostics(window, cx);
}
project::Event::SnippetEdit(id, snippet_edits) => {
if let Some(buffer) = editor.buffer.read(cx).buffer(*id) {
@ -1792,7 +1799,7 @@ impl Editor {
code_action_providers.push(Rc::new(project) as Rc<_>);
}
let mut this = Self {
let mut editor = Self {
focus_handle,
show_cursor_when_unfocused: false,
last_focused_descendant: None,
@ -1954,6 +1961,7 @@ impl Editor {
}),
],
tasks_update_task: None,
pull_diagnostics_task: Task::ready(()),
linked_edit_ranges: Default::default(),
in_project_search: false,
previous_search_ranges: None,
@ -1978,16 +1986,17 @@ impl Editor {
change_list: ChangeList::new(),
mode,
};
if let Some(breakpoints) = this.breakpoint_store.as_ref() {
this._subscriptions
if let Some(breakpoints) = editor.breakpoint_store.as_ref() {
editor
._subscriptions
.push(cx.observe(breakpoints, |_, _, cx| {
cx.notify();
}));
}
this.tasks_update_task = Some(this.refresh_runnables(window, cx));
this._subscriptions.extend(project_subscriptions);
editor.tasks_update_task = Some(editor.refresh_runnables(window, cx));
editor._subscriptions.extend(project_subscriptions);
this._subscriptions.push(cx.subscribe_in(
editor._subscriptions.push(cx.subscribe_in(
&cx.entity(),
window,
|editor, _, e: &EditorEvent, window, cx| match e {
@ -2032,14 +2041,15 @@ impl Editor {
},
));
if let Some(dap_store) = this
if let Some(dap_store) = editor
.project
.as_ref()
.map(|project| project.read(cx).dap_store())
{
let weak_editor = cx.weak_entity();
this._subscriptions
editor
._subscriptions
.push(
cx.observe_new::<project::debugger::session::Session>(move |_, _, cx| {
let session_entity = cx.entity();
@ -2054,40 +2064,44 @@ impl Editor {
);
for session in dap_store.read(cx).sessions().cloned().collect::<Vec<_>>() {
this._subscriptions
editor
._subscriptions
.push(cx.subscribe(&session, Self::on_debug_session_event));
}
}
this.end_selection(window, cx);
this.scroll_manager.show_scrollbars(window, cx);
jsx_tag_auto_close::refresh_enabled_in_any_buffer(&mut this, &buffer, cx);
editor.end_selection(window, cx);
editor.scroll_manager.show_scrollbars(window, cx);
jsx_tag_auto_close::refresh_enabled_in_any_buffer(&mut editor, &buffer, cx);
if full_mode {
let should_auto_hide_scrollbars = cx.should_auto_hide_scrollbars();
cx.set_global(ScrollbarAutoHide(should_auto_hide_scrollbars));
if this.git_blame_inline_enabled {
this.start_git_blame_inline(false, window, cx);
if editor.git_blame_inline_enabled {
editor.start_git_blame_inline(false, window, cx);
}
this.go_to_active_debug_line(window, cx);
editor.go_to_active_debug_line(window, cx);
if let Some(buffer) = buffer.read(cx).as_singleton() {
if let Some(project) = this.project.as_ref() {
if let Some(project) = editor.project.as_ref() {
let handle = project.update(cx, |project, cx| {
project.register_buffer_with_language_servers(&buffer, cx)
});
this.registered_buffers
editor
.registered_buffers
.insert(buffer.read(cx).remote_id(), handle);
}
}
this.minimap = this.create_minimap(EditorSettings::get_global(cx).minimap, window, cx);
editor.minimap =
editor.create_minimap(EditorSettings::get_global(cx).minimap, window, cx);
editor.pull_diagnostics(window, cx);
}
this.report_editor_event("Editor Opened", None, cx);
this
editor.report_editor_event("Editor Opened", None, cx);
editor
}
pub fn deploy_mouse_context_menu(
@ -15890,6 +15904,49 @@ impl Editor {
});
}
fn pull_diagnostics(&mut self, window: &Window, cx: &mut Context<Self>) -> Option<()> {
let project = self.project.as_ref()?.downgrade();
let debounce = Duration::from_millis(
ProjectSettings::get_global(cx)
.diagnostics
.lsp_pull_diagnostics_debounce_ms?,
);
let buffers = self.buffer.read(cx).all_buffers();
self.pull_diagnostics_task = cx.spawn_in(window, async move |editor, cx| {
cx.background_executor().timer(debounce).await;
let Ok(mut pull_diagnostics_tasks) = cx.update(|_, cx| {
buffers
.into_iter()
.flat_map(|buffer| {
Some(project.upgrade()?.pull_diagnostics_for_buffer(buffer, cx))
})
.collect::<FuturesUnordered<_>>()
}) else {
return;
};
while let Some(pull_task) = pull_diagnostics_tasks.next().await {
match pull_task {
Ok(()) => {
if editor
.update_in(cx, |editor, window, cx| {
editor.update_diagnostics_state(window, cx);
})
.is_err()
{
return;
}
}
Err(e) => log::error!("Failed to update project diagnostics: {e:#}"),
}
}
});
Some(())
}
pub fn set_selections_from_remote(
&mut self,
selections: Vec<Selection<Anchor>>,
@ -18603,7 +18660,7 @@ impl Editor {
match event {
multi_buffer::Event::Edited {
singleton_buffer_edited,
edited_buffer: buffer_edited,
edited_buffer,
} => {
self.scrollbar_marker_state.dirty = true;
self.active_indent_guides_state.dirty = true;
@ -18614,18 +18671,25 @@ impl Editor {
if self.has_active_inline_completion() {
self.update_visible_inline_completion(window, cx);
}
if let Some(buffer) = buffer_edited {
let buffer_id = buffer.read(cx).remote_id();
if !self.registered_buffers.contains_key(&buffer_id) {
if let Some(project) = self.project.as_ref() {
project.update(cx, |project, cx| {
self.registered_buffers.insert(
buffer_id,
project.register_buffer_with_language_servers(&buffer, cx),
);
})
if let Some(project) = self.project.as_ref() {
project.update(cx, |project, cx| {
// Diagnostics are not local: an edit within one file (`pub mod foo()` -> `pub mod bar()`), may cause errors in another files with `foo()`.
// Hence, emit a project-wide event to pull for every buffer's diagnostics that has an open editor.
if edited_buffer
.as_ref()
.is_some_and(|buffer| buffer.read(cx).file().is_some())
{
cx.emit(project::Event::RefreshDocumentsDiagnostics);
}
}
if let Some(buffer) = edited_buffer {
self.registered_buffers
.entry(buffer.read(cx).remote_id())
.or_insert_with(|| {
project.register_buffer_with_language_servers(&buffer, cx)
});
}
});
}
cx.emit(EditorEvent::BufferEdited);
cx.emit(SearchEvent::MatchesInvalidated);
@ -18744,15 +18808,19 @@ impl Editor {
| multi_buffer::Event::BufferDiffChanged => cx.emit(EditorEvent::TitleChanged),
multi_buffer::Event::Closed => cx.emit(EditorEvent::Closed),
multi_buffer::Event::DiagnosticsUpdated => {
self.refresh_active_diagnostics(cx);
self.refresh_inline_diagnostics(true, window, cx);
self.scrollbar_marker_state.dirty = true;
cx.notify();
self.update_diagnostics_state(window, cx);
}
_ => {}
};
}
fn update_diagnostics_state(&mut self, window: &mut Window, cx: &mut Context<'_, Editor>) {
self.refresh_active_diagnostics(cx);
self.refresh_inline_diagnostics(true, window, cx);
self.scrollbar_marker_state.dirty = true;
cx.notify();
}
pub fn start_temporary_diff_override(&mut self) {
self.load_diff_task.take();
self.temporary_diff_override = true;
@ -20319,6 +20387,12 @@ pub trait SemanticsProvider {
new_name: String,
cx: &mut App,
) -> Option<Task<Result<ProjectTransaction>>>;
fn pull_diagnostics_for_buffer(
&self,
buffer: Entity<Buffer>,
cx: &mut App,
) -> Task<anyhow::Result<()>>;
}
pub trait CompletionProvider {
@ -20836,6 +20910,61 @@ impl SemanticsProvider for Entity<Project> {
project.perform_rename(buffer.clone(), position, new_name, cx)
}))
}
fn pull_diagnostics_for_buffer(
&self,
buffer: Entity<Buffer>,
cx: &mut App,
) -> Task<anyhow::Result<()>> {
let diagnostics = self.update(cx, |project, cx| {
project
.lsp_store()
.update(cx, |lsp_store, cx| lsp_store.pull_diagnostics(buffer, cx))
});
let project = self.clone();
cx.spawn(async move |cx| {
let diagnostics = diagnostics.await.context("pulling diagnostics")?;
project.update(cx, |project, cx| {
project.lsp_store().update(cx, |lsp_store, cx| {
for diagnostics_set in diagnostics {
let LspPullDiagnostics::Response {
server_id,
uri,
diagnostics: project::PulledDiagnostics::Changed { diagnostics, .. },
} = diagnostics_set
else {
continue;
};
let adapter = lsp_store.language_server_adapter_for_id(server_id);
let disk_based_sources = adapter
.as_ref()
.map(|adapter| adapter.disk_based_diagnostic_sources.as_slice())
.unwrap_or(&[]);
lsp_store
.merge_diagnostics(
server_id,
lsp::PublishDiagnosticsParams {
uri: uri.clone(),
diagnostics,
version: None,
},
DiagnosticSourceKind::Pulled,
disk_based_sources,
|old_diagnostic, _| match old_diagnostic.source_kind {
DiagnosticSourceKind::Pulled => false,
DiagnosticSourceKind::Other | DiagnosticSourceKind::Pushed => {
true
}
},
cx,
)
.log_err();
}
})
})
})
}
}
fn inlay_hint_settings(

View file

@ -13650,6 +13650,7 @@ async fn go_to_prev_overlapping_diagnostic(executor: BackgroundExecutor, cx: &mu
},
],
},
DiagnosticSourceKind::Pushed,
&[],
cx,
)
@ -21562,3 +21563,134 @@ fn assert_hunk_revert(
cx.assert_editor_state(expected_reverted_text_with_selections);
assert_eq!(actual_hunk_statuses_before, expected_hunk_statuses_before);
}
#[gpui::test]
async fn test_pulling_diagnostics(cx: &mut TestAppContext) {
init_test(cx, |_| {});
let diagnostic_requests = Arc::new(AtomicUsize::new(0));
let counter = diagnostic_requests.clone();
let fs = FakeFs::new(cx.executor());
fs.insert_tree(
path!("/a"),
json!({
"first.rs": "fn main() { let a = 5; }",
"second.rs": "// Test file",
}),
)
.await;
let project = Project::test(fs, [path!("/a").as_ref()], cx).await;
let workspace = cx.add_window(|window, cx| Workspace::test_new(project.clone(), window, cx));
let cx = &mut VisualTestContext::from_window(*workspace, cx);
let language_registry = project.read_with(cx, |project, _| project.languages().clone());
language_registry.add(rust_lang());
let mut fake_servers = language_registry.register_fake_lsp(
"Rust",
FakeLspAdapter {
capabilities: lsp::ServerCapabilities {
diagnostic_provider: Some(lsp::DiagnosticServerCapabilities::Options(
lsp::DiagnosticOptions {
identifier: None,
inter_file_dependencies: true,
workspace_diagnostics: true,
work_done_progress_options: Default::default(),
},
)),
..Default::default()
},
..Default::default()
},
);
let editor = workspace
.update(cx, |workspace, window, cx| {
workspace.open_abs_path(
PathBuf::from(path!("/a/first.rs")),
OpenOptions::default(),
window,
cx,
)
})
.unwrap()
.await
.unwrap()
.downcast::<Editor>()
.unwrap();
let fake_server = fake_servers.next().await.unwrap();
let mut first_request = fake_server
.set_request_handler::<lsp::request::DocumentDiagnosticRequest, _, _>(move |params, _| {
counter.fetch_add(1, atomic::Ordering::Release);
assert_eq!(
params.text_document.uri,
lsp::Url::from_file_path(path!("/a/first.rs")).unwrap()
);
async move {
Ok(lsp::DocumentDiagnosticReportResult::Report(
lsp::DocumentDiagnosticReport::Full(lsp::RelatedFullDocumentDiagnosticReport {
related_documents: None,
full_document_diagnostic_report: lsp::FullDocumentDiagnosticReport {
items: Vec::new(),
result_id: None,
},
}),
))
}
});
cx.executor().advance_clock(Duration::from_millis(60));
cx.executor().run_until_parked();
assert_eq!(
diagnostic_requests.load(atomic::Ordering::Acquire),
1,
"Opening file should trigger diagnostic request"
);
first_request
.next()
.await
.expect("should have sent the first diagnostics pull request");
// Editing should trigger diagnostics
editor.update_in(cx, |editor, window, cx| {
editor.handle_input("2", window, cx)
});
cx.executor().advance_clock(Duration::from_millis(60));
cx.executor().run_until_parked();
assert_eq!(
diagnostic_requests.load(atomic::Ordering::Acquire),
2,
"Editing should trigger diagnostic request"
);
// Moving cursor should not trigger diagnostic request
editor.update_in(cx, |editor, window, cx| {
editor.change_selections(None, window, cx, |s| {
s.select_ranges([Point::new(0, 0)..Point::new(0, 0)])
});
});
cx.executor().advance_clock(Duration::from_millis(60));
cx.executor().run_until_parked();
assert_eq!(
diagnostic_requests.load(atomic::Ordering::Acquire),
2,
"Cursor movement should not trigger diagnostic request"
);
// Multiple rapid edits should be debounced
for _ in 0..5 {
editor.update_in(cx, |editor, window, cx| {
editor.handle_input("x", window, cx)
});
}
cx.executor().advance_clock(Duration::from_millis(60));
cx.executor().run_until_parked();
let final_requests = diagnostic_requests.load(atomic::Ordering::Acquire);
assert!(
final_requests <= 4,
"Multiple rapid edits should be debounced (got {} requests)",
final_requests
);
}

View file

@ -522,4 +522,12 @@ impl SemanticsProvider for BranchBufferSemanticsProvider {
) -> Option<Task<anyhow::Result<project::ProjectTransaction>>> {
None
}
fn pull_diagnostics_for_buffer(
&self,
_: Entity<Buffer>,
_: &mut App,
) -> Task<anyhow::Result<()>> {
Task::ready(Ok(()))
}
}