Skip to content

Commit

Permalink
refactor(lsp): replace util.buf_versions with changedtick (#28943)
Browse files Browse the repository at this point in the history
`lsp.util.buf_versions` was already derived from changedtick (`on_lines`
from `buf_attach` synced the version)

As far as I can tell there is no need to keep track of the state in a
separate table.
  • Loading branch information
mfussenegger committed May 30, 2024
1 parent b2bad0a commit 5c33815
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 57 deletions.
3 changes: 3 additions & 0 deletions runtime/doc/deprecated.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ API
LUA
- vim.region() Use |getregionpos()| instead.

LSP
- *vim.lsp.util.buf_versions* Use |b:changedtick| instead.

DIAGNOSTICS
- *vim.diagnostic.goto_next()* Use |vim.diagnostic.jump()| with `{count = 1}` instead.
- *vim.diagnostic.goto_prev()* Use |vim.diagnostic.jump()| with `{count = -1}` instead.
Expand Down
5 changes: 1 addition & 4 deletions runtime/lua/vim/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,6 @@ 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
Expand Down Expand Up @@ -520,7 +519,6 @@ 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)
Expand Down Expand Up @@ -576,12 +574,11 @@ local function buf_attach(bufnr)
})
-- First time, so attach and set up stuff.
api.nvim_buf_attach(bufnr, false, {
on_lines = function(_, _, changedtick, firstline, lastline, new_lastline)
on_lines = function(_, _, _, 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,

Expand Down
3 changes: 1 addition & 2 deletions runtime/lua/vim/lsp/_changetracking.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
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
Expand Down Expand Up @@ -277,7 +276,7 @@ local function send_changes(bufnr, sync_kind, state, buf_state)
client.notify(protocol.Methods.textDocument_didChange, {
textDocument = {
uri = uri,
version = util.buf_versions[bufnr],
version = vim.b[bufnr].changedtick,
},
contentChanges = changes,
})
Expand Down
5 changes: 2 additions & 3 deletions runtime/lua/vim/lsp/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -922,14 +922,13 @@ function Client:_text_document_did_open_handler(bufnr)

local params = {
textDocument = {
version = 0,
version = vim.b[bufnr].changedtick,
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()
Expand Down
4 changes: 2 additions & 2 deletions runtime/lua/vim/lsp/inlay_hint.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function M.on_inlayhint(err, result, ctx, _)
return
end
local bufnr = assert(ctx.bufnr)
if util.buf_versions[bufnr] ~= ctx.version then
if vim.b[bufnr].changedtick ~= ctx.version then
return
end
local client_id = ctx.client_id
Expand Down Expand Up @@ -324,7 +324,7 @@ api.nvim_set_decoration_provider(namespace, {
return
end

if bufstate.version ~= util.buf_versions[bufnr] then
if bufstate.version ~= vim.b[bufnr].changedtick then
return
end

Expand Down
6 changes: 3 additions & 3 deletions runtime/lua/vim/lsp/semantic_tokens.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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, util.buf_versions[bufnr])
coroutine.resume(co, vim.b[bufnr].changedtick)
end)
if request.version ~= coroutine.yield() then
-- request became stale since the last time the coroutine ran.
Expand Down Expand Up @@ -275,7 +275,7 @@ end
---
---@package
function STHighlighter:send_request()
local version = util.buf_versions[self.bufnr]
local version = vim.b[self.bufnr].changedtick

self:reset_timer()

Expand Down Expand Up @@ -418,7 +418,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 == util.buf_versions[self.bufnr] then
if current_result.version and current_result.version == vim.b[self.bufnr].changedtick then
if not current_result.namespace_cleared then
api.nvim_buf_clear_namespace(self.bufnr, state.namespace, 0, -1)
current_result.namespace_cleared = true
Expand Down
16 changes: 11 additions & 5 deletions runtime/lua/vim/lsp/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -509,8 +509,7 @@ function M.apply_text_document_edit(text_document_edit, index, offset_encoding)
and (
text_document.version
and text_document.version > 0
and M.buf_versions[bufnr]
and M.buf_versions[bufnr] > text_document.version
and vim.b[bufnr].changedtick > text_document.version
)
then
print('Buffer ', text_document.uri, ' newer than edits.')
Expand Down Expand Up @@ -2200,9 +2199,16 @@ function M._refresh(method, opts)
end
end

M._get_line_byte_from_position = get_line_byte_from_position

---@nodoc
M.buf_versions = {} ---@type table<integer,integer>
---@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

return M
14 changes: 7 additions & 7 deletions test/functional/fixtures/fake-lsp-server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ function tests.basic_check_buffer_open()
languageId = '',
text = table.concat({ 'testing', '123' }, '\n') .. '\n',
uri = 'file://',
version = 0,
version = 2,
},
})
expect_notification('finish')
Expand All @@ -498,7 +498,7 @@ function tests.basic_check_buffer_open_and_change()
languageId = '',
text = table.concat({ 'testing', '123' }, '\n') .. '\n',
uri = 'file://',
version = 0,
version = 2,
},
})
expect_notification('textDocument/didChange', {
Expand Down Expand Up @@ -534,7 +534,7 @@ function tests.basic_check_buffer_open_and_change_noeol()
languageId = '',
text = table.concat({ 'testing', '123' }, '\n'),
uri = 'file://',
version = 0,
version = 2,
},
})
expect_notification('textDocument/didChange', {
Expand Down Expand Up @@ -569,7 +569,7 @@ function tests.basic_check_buffer_open_and_change_multi()
languageId = '',
text = table.concat({ 'testing', '123' }, '\n') .. '\n',
uri = 'file://',
version = 0,
version = 2,
},
})
expect_notification('textDocument/didChange', {
Expand Down Expand Up @@ -614,7 +614,7 @@ function tests.basic_check_buffer_open_and_change_multi_and_close()
languageId = '',
text = table.concat({ 'testing', '123' }, '\n') .. '\n',
uri = 'file://',
version = 0,
version = 2,
},
})
expect_notification('textDocument/didChange', {
Expand Down Expand Up @@ -672,7 +672,7 @@ function tests.basic_check_buffer_open_and_change_incremental()
languageId = '',
text = table.concat({ 'testing', '123' }, '\n') .. '\n',
uri = 'file://',
version = 0,
version = 2,
},
})
expect_notification('textDocument/didChange', {
Expand Down Expand Up @@ -715,7 +715,7 @@ function tests.basic_check_buffer_open_and_change_incremental_editing()
languageId = '',
text = table.concat({ 'testing', '123' }, '\n'),
uri = 'file://',
version = 0,
version = 2,
},
})
expect_notification('textDocument/didChange', {
Expand Down
24 changes: 12 additions & 12 deletions test/functional/plugin/lsp/semantic_tokens_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,14 @@ describe('semantic token highlighting', function()
end)

it('buffer is highlighted when attached', function()
insert(text)
exec_lua([[
bufnr = vim.api.nvim_get_current_buf()
vim.api.nvim_win_set_buf(0, bufnr)
vim.bo[bufnr].filetype = 'some-filetype'
client_id = vim.lsp.start({ name = 'dummy', cmd = server.cmd })
]])

insert(text)

screen:expect {
grid = [[
#include <iostream> |
Expand All @@ -141,6 +140,7 @@ describe('semantic token highlighting', function()
end)

it('use LspTokenUpdate and highlight_token', function()
insert(text)
exec_lua([[
vim.api.nvim_create_autocmd("LspTokenUpdate", {
callback = function(args)
Expand All @@ -157,8 +157,6 @@ describe('semantic token highlighting', function()
client_id = vim.lsp.start({ name = 'dummy', cmd = server.cmd })
]])

insert(text)

screen:expect {
grid = [[
#include <iostream> |
Expand All @@ -180,14 +178,17 @@ describe('semantic token highlighting', function()
end)

it('buffer is unhighlighted when client is detached', function()
insert(text)

exec_lua([[
bufnr = vim.api.nvim_get_current_buf()
vim.api.nvim_win_set_buf(0, bufnr)
client_id = vim.lsp.start({ name = 'dummy', cmd = server.cmd })
vim.wait(1000, function()
return #server.messages > 1
end)
]])

insert(text)

exec_lua([[
vim.notify = function() end
vim.lsp.buf_detach_client(bufnr, client_id)
Expand Down Expand Up @@ -331,14 +332,13 @@ describe('semantic token highlighting', function()
end)

it('buffer is re-highlighted when force refreshed', function()
insert(text)
exec_lua([[
bufnr = vim.api.nvim_get_current_buf()
vim.api.nvim_win_set_buf(0, bufnr)
client_id = vim.lsp.start({ name = 'dummy', cmd = server.cmd })
]])

insert(text)

screen:expect {
grid = [[
#include <iostream> |
Expand Down Expand Up @@ -412,13 +412,14 @@ describe('semantic token highlighting', function()
end)

it('updates highlights with delta request on buffer change', function()
insert(text)

exec_lua([[
bufnr = vim.api.nvim_get_current_buf()
vim.api.nvim_win_set_buf(0, bufnr)
client_id = vim.lsp.start({ name = 'dummy', cmd = server.cmd })
]])

insert(text)
screen:expect {
grid = [[
#include <iostream> |
Expand Down Expand Up @@ -597,6 +598,7 @@ describe('semantic token highlighting', function()
end)

it('does not send delta requests if not supported by server', function()
insert(text)
exec_lua(
[[
local legend, response, edit_response = ...
Expand Down Expand Up @@ -625,7 +627,6 @@ describe('semantic token highlighting', function()
edit_response
)

insert(text)
screen:expect {
grid = [[
#include <iostream> |
Expand Down Expand Up @@ -1449,6 +1450,7 @@ int main()
},
}) do
it(test.it, function()
insert(test.text1)
exec_lua(create_server_definition)
exec_lua(
[[
Expand Down Expand Up @@ -1485,8 +1487,6 @@ int main()
test.response2
)

insert(test.text1)

test.expected_screen1()

local highlights = exec_lua([[
Expand Down
Loading

0 comments on commit 5c33815

Please sign in to comment.