Stop sending redundant LSP proto requests (#35581)

Before, each time any LSP feature was used on client remote, it always
produced a `proto::` request that always had been sent to the host, from
where returned as an empty response.

Instead, propagate more language server-related data to the client,
`lsp::ServerCapability`, so Zed client can omit certain requests if
those are not supported.

On top of that, rework the approach Zed uses to query for the data
refreshes: before, editors tried to fetch the data when the server start
was reported (locally and remotely).
Now, a later event is selected: on each `textDocument/didOpen` for the
buffer contained in this editor, we will query for new LSP data, reusing
the cache if needed.

Before, servers could reject unregistered files' LSP queries, or process
them slowly when starting up.
Now, such refreshes are happening later and should be cached.

This requires a collab DB change, to restore server data on rejoin.

Release Notes:

- Fixed excessive LSP requests sent during remote sessions
This commit is contained in:
Kirill Bulatov 2025-08-05 16:36:05 +03:00 committed by GitHub
parent 5b40b3618f
commit 22473fc611
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 793 additions and 351 deletions

View file

@ -277,6 +277,13 @@ pub enum Event {
LanguageServerAdded(LanguageServerId, LanguageServerName, Option<WorktreeId>),
LanguageServerRemoved(LanguageServerId),
LanguageServerLog(LanguageServerId, LanguageServerLogType, String),
// [`lsp::notification::DidOpenTextDocument`] was sent to this server using the buffer data.
// Zed's buffer-related data is updated accordingly.
LanguageServerBufferRegistered {
server_id: LanguageServerId,
buffer_id: BufferId,
buffer_abs_path: PathBuf,
},
Toast {
notification_id: SharedString,
message: String,
@ -2931,8 +2938,8 @@ impl Project {
}
LspStoreEvent::LanguageServerUpdate {
language_server_id,
message,
name,
message,
} => {
if self.is_local() {
self.enqueue_buffer_ordered_message(
@ -2944,6 +2951,32 @@ impl Project {
)
.ok();
}
match message {
proto::update_language_server::Variant::MetadataUpdated(update) => {
if let Some(capabilities) = update
.capabilities
.as_ref()
.and_then(|capabilities| serde_json::from_str(capabilities).ok())
{
self.lsp_store.update(cx, |lsp_store, _| {
lsp_store
.lsp_server_capabilities
.insert(*language_server_id, capabilities);
});
}
}
proto::update_language_server::Variant::RegisteredForBuffer(update) => {
if let Some(buffer_id) = BufferId::new(update.buffer_id).ok() {
cx.emit(Event::LanguageServerBufferRegistered {
buffer_id,
server_id: *language_server_id,
buffer_abs_path: PathBuf::from(&update.buffer_abs_path),
});
}
}
_ => (),
}
}
LspStoreEvent::Notification(message) => cx.emit(Event::Toast {
notification_id: "lsp".into(),
@ -3476,20 +3509,6 @@ impl Project {
})
}
fn document_highlights_impl(
&mut self,
buffer: &Entity<Buffer>,
position: PointUtf16,
cx: &mut Context<Self>,
) -> Task<Result<Vec<DocumentHighlight>>> {
self.request_lsp(
buffer.clone(),
LanguageServerToQuery::FirstCapable,
GetDocumentHighlights { position },
cx,
)
}
pub fn document_highlights<T: ToPointUtf16>(
&mut self,
buffer: &Entity<Buffer>,
@ -3497,7 +3516,12 @@ impl Project {
cx: &mut Context<Self>,
) -> Task<Result<Vec<DocumentHighlight>>> {
let position = position.to_point_utf16(buffer.read(cx));
self.document_highlights_impl(buffer, position, cx)
self.request_lsp(
buffer.clone(),
LanguageServerToQuery::FirstCapable,
GetDocumentHighlights { position },
cx,
)
}
pub fn document_symbols(
@ -3598,14 +3622,14 @@ impl Project {
.update(cx, |lsp_store, cx| lsp_store.hover(buffer, position, cx))
}
pub fn linked_edit(
pub fn linked_edits(
&self,
buffer: &Entity<Buffer>,
position: Anchor,
cx: &mut Context<Self>,
) -> Task<Result<Vec<Range<Anchor>>>> {
self.lsp_store.update(cx, |lsp_store, cx| {
lsp_store.linked_edit(buffer, position, cx)
lsp_store.linked_edits(buffer, position, cx)
})
}
@ -3697,19 +3721,6 @@ impl Project {
})
}
fn prepare_rename_impl(
&mut self,
buffer: Entity<Buffer>,
position: PointUtf16,
cx: &mut Context<Self>,
) -> Task<Result<PrepareRenameResponse>> {
self.request_lsp(
buffer,
LanguageServerToQuery::FirstCapable,
PrepareRename { position },
cx,
)
}
pub fn prepare_rename<T: ToPointUtf16>(
&mut self,
buffer: Entity<Buffer>,
@ -3717,7 +3728,12 @@ impl Project {
cx: &mut Context<Self>,
) -> Task<Result<PrepareRenameResponse>> {
let position = position.to_point_utf16(buffer.read(cx));
self.prepare_rename_impl(buffer, position, cx)
self.request_lsp(
buffer,
LanguageServerToQuery::FirstCapable,
PrepareRename { position },
cx,
)
}
pub fn perform_rename<T: ToPointUtf16>(