[Backport release-0.9] fix(codelens): add buffer and line checks before displaying codelens (#24014)

fix(codelens): add buffer and line checks before displaying codelens

(cherry picked from commit 928dc33053)

Co-authored-by: Rohit Sukumaran <rohit.sukumaran@kredx.com>
This commit is contained in:
github-actions[bot]
2023-06-13 19:55:00 +02:00
committed by GitHub
parent db41f29b7c
commit 0f121fea81

View File

@@ -135,6 +135,10 @@ end
---@param bufnr integer
---@param client_id integer
function M.display(lenses, bufnr, client_id)
if not api.nvim_buf_is_loaded(bufnr) then
return
end
local ns = namespaces[client_id]
if not lenses or not next(lenses) then
api.nvim_buf_clear_namespace(bufnr, ns, 0, -1)
@@ -180,6 +184,10 @@ end
---@param bufnr integer
---@param client_id integer
function M.save(lenses, bufnr, client_id)
if not api.nvim_buf_is_loaded(bufnr) then
return
end
local lenses_by_client = lens_cache_by_buf[bufnr]
if not lenses_by_client then
lenses_by_client = {}
@@ -220,19 +228,24 @@ local function resolve_lenses(lenses, bufnr, client_id, callback)
countdown()
else
client.request('codeLens/resolve', lens, function(_, result)
if result and result.command then
if api.nvim_buf_is_loaded(bufnr) and result and result.command then
lens.command = result.command
-- Eager display to have some sort of incremental feedback
-- Once all lenses got resolved there will be a full redraw for all lenses
-- So that multiple lens per line are properly displayed
api.nvim_buf_set_extmark(
bufnr,
ns,
lens.range.start.line,
0,
{ virt_text = { { lens.command.title, 'LspCodeLens' } }, hl_mode = 'combine' }
)
local num_lines = api.nvim_buf_line_count(bufnr)
if lens.range.start.line <= num_lines then
api.nvim_buf_set_extmark(
bufnr,
ns,
lens.range.start.line,
0,
{ virt_text = { { lens.command.title, 'LspCodeLens' } }, hl_mode = 'combine' }
)
end
end
countdown()
end, bufnr)
end