fix(lsp): revert buf_versions deprecation/replacement (#29217)

* Revert "fix(lsp): account for changedtick version gap on modified reset (#29170)"

This reverts commit 2e6d295f79.

* Revert "refactor(lsp): replace util.buf_versions with changedtick (#28943)"

This reverts commit 5c33815448.
This commit is contained in:
Mathias Fußenegger
2024-06-07 11:36:46 +02:00
committed by GitHub
parent 6c7677e5d2
commit 6e45cd7f00
10 changed files with 69 additions and 67 deletions

View File

@@ -484,6 +484,7 @@ local function text_document_did_save_handler(bufnr)
text = lsp._buf_get_full_text(bufnr),
},
})
util.buf_versions[bufnr] = 0
end
local save_capability = vim.tbl_get(client.server_capabilities, 'textDocumentSync', 'save')
if save_capability then
@@ -519,6 +520,7 @@ local function buf_detach_client(bufnr, client)
end
client.attached_buffers[bufnr] = nil
util.buf_versions[bufnr] = nil
local namespace = lsp.diagnostic.get_namespace(client.id)
vim.diagnostic.reset(namespace, bufnr)
@@ -574,11 +576,12 @@ local function buf_attach(bufnr)
})
-- First time, so attach and set up stuff.
api.nvim_buf_attach(bufnr, false, {
on_lines = function(_, _, _, firstline, lastline, new_lastline)
on_lines = function(_, _, changedtick, firstline, lastline, new_lastline)
if #lsp.get_clients({ bufnr = bufnr }) == 0 then
-- detach if there are no clients
return #lsp.get_clients({ bufnr = bufnr, _uninitialized = true }) == 0
end
util.buf_versions[bufnr] = changedtick
changetracking.send_changes(bufnr, firstline, lastline, new_lastline)
end,

View File

@@ -1,5 +1,6 @@
local protocol = require('vim.lsp.protocol')
local sync = require('vim.lsp.sync')
local util = require('vim.lsp.util')
local api = vim.api
local uv = vim.uv
@@ -276,7 +277,7 @@ local function send_changes(bufnr, sync_kind, state, buf_state)
client.notify(protocol.Methods.textDocument_didChange, {
textDocument = {
uri = uri,
version = vim.b[bufnr].changedtick,
version = util.buf_versions[bufnr],
},
contentChanges = changes,
})

View File

@@ -673,8 +673,8 @@ function Client:_request(method, params, handler, bufnr)
end
-- Ensure pending didChange notifications are sent so that the server doesn't operate on a stale state
changetracking.flush(self, bufnr)
local version = lsp.util.buf_versions[bufnr]
bufnr = resolve_bufnr(bufnr)
local version = vim.b[bufnr].changedtick
log.debug(self._log_prefix, 'client.request', self.id, method, params, handler, bufnr)
local success, request_id = self.rpc.request(method, params, function(err, result)
local context = {
@@ -922,13 +922,14 @@ function Client:_text_document_did_open_handler(bufnr)
local params = {
textDocument = {
version = vim.b[bufnr].changedtick,
version = 0,
uri = vim.uri_from_bufnr(bufnr),
languageId = self.get_language_id(bufnr, filetype),
text = lsp._buf_get_full_text(bufnr),
},
}
self.notify(ms.textDocument_didOpen, params)
lsp.util.buf_versions[bufnr] = params.textDocument.version
-- Next chance we get, we should re-do the diagnostics
vim.schedule(function()

View File

@@ -43,7 +43,7 @@ function M.on_inlayhint(err, result, ctx, _)
return
end
local bufnr = assert(ctx.bufnr)
if vim.b[bufnr].changedtick ~= ctx.version then
if util.buf_versions[bufnr] ~= ctx.version then
return
end
local client_id = ctx.client_id
@@ -324,7 +324,7 @@ api.nvim_set_decoration_provider(namespace, {
return
end
if bufstate.version ~= vim.b[bufnr].changedtick then
if bufstate.version ~= util.buf_versions[bufnr] then
return
end

View File

@@ -116,7 +116,7 @@ local function tokens_to_ranges(data, bufnr, client, request)
if elapsed_ns > yield_interval_ns then
vim.schedule(function()
coroutine.resume(co, vim.b[bufnr].changedtick)
coroutine.resume(co, util.buf_versions[bufnr])
end)
if request.version ~= coroutine.yield() then
-- request became stale since the last time the coroutine ran.
@@ -269,7 +269,7 @@ end
---
---@package
function STHighlighter:send_request()
local version = vim.b[self.bufnr].changedtick
local version = util.buf_versions[self.bufnr]
self:reset_timer()
@@ -412,7 +412,7 @@ end
function STHighlighter:on_win(topline, botline)
for client_id, state in pairs(self.client_state) do
local current_result = state.current_result
if current_result.version and current_result.version == vim.b[self.bufnr].changedtick then
if current_result.version and current_result.version == util.buf_versions[self.bufnr] then
if not current_result.namespace_cleared then
api.nvim_buf_clear_namespace(self.bufnr, state.namespace, 0, -1)
current_result.namespace_cleared = true

View File

@@ -502,11 +502,6 @@ function M.apply_text_document_edit(text_document_edit, index, offset_encoding)
should_check_version = false
end
-- changedtick increases on save but server only receives version updates
-- on line changes (via didChange)
-- This allows a gap of 1 to account for the servers outdated view
local version_offset = vim.b[bufnr].modified and 0 or 1
-- `VersionedTextDocumentIdentifier`s version may be null
-- https://microsoft.github.io/language-server-protocol/specification#versionedTextDocumentIdentifier
if
@@ -514,7 +509,8 @@ function M.apply_text_document_edit(text_document_edit, index, offset_encoding)
and (
text_document.version
and text_document.version > 0
and vim.b[bufnr].changedtick > (text_document.version + version_offset)
and M.buf_versions[bufnr]
and M.buf_versions[bufnr] > text_document.version
)
then
print('Buffer ', text_document.uri, ' newer than edits.')
@@ -2206,16 +2202,9 @@ function M._refresh(method, opts)
end
end
---@nodoc
---@deprecated
---@type table<integer,integer>
M.buf_versions = setmetatable({}, {
__index = function(_, bufnr)
vim.deprecate('vim.lsp.util.buf_versions', 'vim.b.changedtick', '0.13')
return vim.b[bufnr].changedtick
end,
})
M._get_line_byte_from_position = get_line_byte_from_position
---@nodoc
M.buf_versions = {} ---@type table<integer,integer>
return M