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

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
This commit is contained in:
phanium
2026-04-30 17:56:16 +08:00
committed by GitHub
parent 1e06e95662
commit 1e7edb2c52
5 changed files with 79 additions and 12 deletions

View File

@@ -167,7 +167,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
@@ -1124,6 +1125,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
@@ -1192,7 +1205,7 @@ function Client:on_attach(bufnr)
end
end)
self.attached_buffers[bufnr] = true
self.attached_buffers[bufnr] = self:_get_language_id(bufnr)
end
--- @private
@@ -1383,11 +1396,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