lsp: Do not drop lsp buffer handle from editor when a language change leads to buffer having a legit language (#44469)

Fixes a bug that led to us unnecessarily restarting a language server
when we were looking at a single file of a given language.

Release Notes:

- Fixed a bug that led to Zed sometimes starting an excessive amount of
language servers
This commit is contained in:
Piotr Osiewicz
2025-12-09 21:37:39 +01:00
committed by GitHub
parent 5dd8561b06
commit 3180f44477
10 changed files with 23 additions and 16 deletions
+8 -5
View File
@@ -1,8 +1,8 @@
pub mod row_chunk;
use crate::{
DebuggerTextObject, LanguageScope, Outline, OutlineConfig, RunnableCapture, RunnableTag,
TextObject, TreeSitterOptions,
DebuggerTextObject, LanguageScope, Outline, OutlineConfig, PLAIN_TEXT, RunnableCapture,
RunnableTag, TextObject, TreeSitterOptions,
diagnostic_set::{DiagnosticEntry, DiagnosticEntryRef, DiagnosticGroup},
language_settings::{LanguageSettings, language_settings},
outline::OutlineItem,
@@ -353,7 +353,8 @@ pub enum BufferEvent {
/// The buffer is in need of a reload
ReloadNeeded,
/// The buffer's language was changed.
LanguageChanged,
/// The boolean indicates whether this buffer did not have a language before, but does now.
LanguageChanged(bool),
/// The buffer's syntax trees were updated.
Reparsed,
/// The buffer's diagnostics were updated.
@@ -1386,10 +1387,12 @@ impl Buffer {
) {
self.non_text_state_update_count += 1;
self.syntax_map.lock().clear(&self.text);
self.language = language;
let old_language = std::mem::replace(&mut self.language, language);
self.was_changed();
self.reparse(cx, may_block);
cx.emit(BufferEvent::LanguageChanged);
let has_fresh_language =
self.language.is_some() && old_language.is_none_or(|old| old == *PLAIN_TEXT);
cx.emit(BufferEvent::LanguageChanged(has_fresh_language));
}
/// Assign a language registry to the buffer. This allows the buffer to retrieve