backport fix(lsp): send didClose, didOpen when languageId changes (#39519)

fix(lsp): send didClose, didOpen when languageId changes

Problem:
If a buffer's filetype changes after the LSP client has already
attached (e.g. from json to jsonc via a modeline), but the client
supports both filetypes, it stays attached. It does not notify the
server of the new languageId, causing the server to incorrectly process
the file using the old languageId.

Solution:
Save the languageId used during textDocument/didOpen, and send
textDocument/didClose + textDocument/didOpen when buffer's languageId
changed.

Lsp spec:
0003fb53f1/_specifications/lsp/3.18/textDocument/didOpen.md (L5)
> If the language id of a document changes, the client
> needs to send a textDocument/didClose to the server followed by a
> textDocument/didOpen with the new language id if the server handles
> the new language id as well.

AI-assisted: Gemini 3.1 Pro

Co-authored-by: phanium <91544758+phanen@users.noreply.github.com>
This commit is contained in:
Justin M. Keyes
2026-04-30 09:09:55 -04:00
committed by GitHub
parent d147d0434d
commit 4b424a06c5
20 changed files with 243 additions and 283 deletions

View File

@@ -154,7 +154,8 @@ local all_clients = {}
--- @class vim.lsp.Client
---
--- @field attached_buffers table<integer,true>
--- Each buffer's last used `languageId`.
--- @field attached_buffers table<integer,string>
---
--- Capabilities provided by the client (editor or tool), at startup.
--- @field capabilities lsp.ClientCapabilities
@@ -1109,6 +1110,18 @@ function Client:exec_cmd(cmd, context, handler)
self:request('workspace/executeCommand', params, handler, context.bufnr)
end
--- Default handler for the 'textDocument/didClose' LSP notification.
---
--- @param buf integer Number of the buffer, or 0 for current
function Client:_text_document_did_close_handler(buf)
if not self:supports_method('textDocument/didClose') then
return
end
local uri = vim.uri_from_bufnr(buf)
local params = { textDocument = { uri = uri } }
self:notify('textDocument/didClose', params)
end
--- Default handler for the 'textDocument/didOpen' LSP notification.
---
--- @param bufnr integer Number of the buffer, or 0 for current
@@ -1177,7 +1190,7 @@ function Client:on_attach(bufnr)
end
end)
self.attached_buffers[bufnr] = true
self.attached_buffers[bufnr] = self:_get_language_id(bufnr)
end
--- @private
@@ -1368,11 +1381,7 @@ function Client:_on_detach(bufnr)
changetracking.reset_buf(self, bufnr)
if self:supports_method('textDocument/didClose') then
local uri = vim.uri_from_bufnr(bufnr)
local params = { textDocument = { uri = uri } }
self:notify('textDocument/didClose', params)
end
self:_text_document_did_close_handler(bufnr)
self.attached_buffers[bufnr] = nil