Skip to content
forked from neovim/neovim

Commit

Permalink
fix(lsp): make sure to always reset active codelens refreshes (neovim…
Browse files Browse the repository at this point in the history
…#18331)

This fixes issues where subsequent calls to vim.lsp.codelens.refresh()
would have no effect due to the buffer not getting cleared from the
active_refresh table.

Examples of how such scenarios would occur are:
  - A textDocument/codeLens result yielded an error.
  - The 'textDocument/codeLens' handler was overriden in such a way that
    it no longer called vim.lsp.codelens.on_codelens().

(cherry picked from commit 94eb72c)
  • Loading branch information
williamboman authored and mfussenegger committed Jun 25, 2022
1 parent b3d754c commit f2b4652
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 3 deletions.
11 changes: 8 additions & 3 deletions runtime/lua/vim/lsp/codelens.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local util = require('vim.lsp.util')
local log = require('vim.lsp.log')
local api = vim.api
local M = {}

Expand Down Expand Up @@ -214,16 +215,20 @@ end
--- |lsp-handler| for the method `textDocument/codeLens`
---
function M.on_codelens(err, result, ctx, _)
assert(not err, vim.inspect(err))
if err then
active_refreshes[ctx.bufnr] = nil
local _ = log.error() and log.error("codelens", err)
return
end

M.save(result, ctx.bufnr, ctx.client_id)

-- Eager display for any resolved (and unresolved) lenses and refresh them
-- once resolved.
M.display(result, ctx.bufnr, ctx.client_id)
resolve_lenses(result, ctx.bufnr, ctx.client_id, function()
M.display(result, ctx.bufnr, ctx.client_id)
active_refreshes[ctx.bufnr] = nil
M.display(result, ctx.bufnr, ctx.client_id)
end)
end

Expand All @@ -245,7 +250,7 @@ function M.refresh()
return
end
active_refreshes[bufnr] = true
vim.lsp.buf_request(0, 'textDocument/codeLens', params)
vim.lsp.buf_request(0, 'textDocument/codeLens', params, M.on_codelens)
end


Expand Down
43 changes: 43 additions & 0 deletions test/functional/fixtures/fake-lsp-server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,49 @@ function tests.clientside_commands()
}
end

function tests.codelens_refresh_lock()
skeleton {
on_init = function()
return {
capabilities = {
codeLensProvider = { resolveProvider = true; };
}
}
end;
body = function()
notify('start')
expect_request("textDocument/codeLens", function ()
return {code = -32002, message = "ServerNotInitialized"}, nil
end)
expect_request("textDocument/codeLens", function ()
local lenses = {
{
range = {
start = { line = 0, character = 0, },
['end'] = { line = 0, character = 3 }
},
command = { title = 'Lens1', command = 'Dummy' }
},
}
return nil, lenses
end)
expect_request("textDocument/codeLens", function ()
local lenses = {
{
range = {
start = { line = 0, character = 0, },
['end'] = { line = 0, character = 3 }
},
command = { title = 'Lens2', command = 'Dummy' }
},
}
return nil, lenses
end)
notify('shutdown')
end;
}
end

-- Tests will be indexed by TEST_NAME

local kill_timer = vim.loop.new_timer()
Expand Down
79 changes: 79 additions & 0 deletions test/functional/plugin/lsp_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2771,5 +2771,84 @@ describe('LSP', function()
end
}
end)

it('releases buffer refresh lock', function()
local client
local expected_handlers = {
{NIL, {}, {method="shutdown", client_id=1}};
{NIL, {}, {method="start", client_id=1}};
}
test_rpc_server {
test_name = 'codelens_refresh_lock',
on_init = function(client_)
client = client_
end,
on_setup = function()
exec_lua([=[
local bufnr = vim.api.nvim_get_current_buf()
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, {'One line'})
vim.lsp.buf_attach_client(bufnr, TEST_RPC_CLIENT_ID)
CALLED = false
RESPONSE = nil
local on_codelens = vim.lsp.codelens.on_codelens
vim.lsp.codelens.on_codelens = function (err, result, ...)
CALLED = true
RESPONSE = { err = err, result = result }
return on_codelens(err, result, ...)
end
]=])
end,
on_exit = function(code, signal)
eq(0, code, "exit code", fake_lsp_logfile)
eq(0, signal, "exit signal", fake_lsp_logfile)
end,
on_handler = function(err, result, ctx)
eq(table.remove(expected_handlers), {err, result, ctx})
if ctx.method == 'start' then
-- 1. first codelens request errors
local response = exec_lua([=[
CALLED = false
vim.lsp.codelens.refresh()
vim.wait(100, function () return CALLED end)
return RESPONSE
]=])
eq( { err = { code = -32002, message = "ServerNotInitialized" } }, response)

-- 2. second codelens request runs
response = exec_lua([=[
CALLED = false
local cmd_called = nil
vim.lsp.commands["Dummy"] = function (command)
cmd_called = command
end
vim.lsp.codelens.refresh()
vim.wait(100, function () return CALLED end)
vim.lsp.codelens.run()
vim.wait(100, function () return cmd_called end)
return cmd_called
]=])
eq( { command = "Dummy", title = "Lens1" }, response)

-- 3. third codelens request runs
response = exec_lua([=[
CALLED = false
local cmd_called = nil
vim.lsp.commands["Dummy"] = function (command)
cmd_called = command
end
vim.lsp.codelens.refresh()
vim.wait(100, function () return CALLED end)
vim.lsp.codelens.run()
vim.wait(100, function () return cmd_called end)
return cmd_called
]=])
eq( { command = "Dummy", title = "Lens2" }, response)
elseif ctx.method == 'shutdown' then
client.stop()
end
end
}
end)
end)
end)

0 comments on commit f2b4652

Please sign in to comment.