refactor(codelens): split into modules and add cancellation

This commit is contained in:
2026-04-19 00:56:34 +02:00
parent 721f4cb257
commit 20dcd20873
5 changed files with 471 additions and 354 deletions
+73
View File
@@ -0,0 +1,73 @@
---@class ow.lsp.CodeLensResponse : ow.lsp.Response
---@field result lsp.CodeLens[]
local Row = require("lsp.codelens.row")
local Session = require("lsp.codelens.session")
local GROUP = vim.api.nvim_create_augroup("ow.lsp.codelens", { clear = true })
---@type table<integer, ow.lsp.codelens.Session>
local session_by_buf = {}
local M = {}
---@param buf integer
---@return ow.lsp.codelens.Session
local function get_session(buf)
session_by_buf[buf] = session_by_buf[buf] or Session.new(buf)
return session_by_buf[buf]
end
---@param buf? integer
---@return boolean
function M.is_enabled(buf)
buf = buf or vim.api.nvim_get_current_buf()
local session = session_by_buf[buf]
return session ~= nil and session.enabled
end
---@param value? boolean
---@param buf? integer
function M.enable(value, buf)
if value == nil then
value = true
end
buf = buf or vim.api.nvim_get_current_buf()
get_session(buf):enable(value)
end
---@param buf? integer
function M.toggle(buf)
buf = buf or vim.api.nvim_get_current_buf()
get_session(buf):toggle()
end
---@class ow.lsp.codelens.SetupOpts : ow.lsp.codelens.row.ConfigureOpts
---@param opts? ow.lsp.codelens.SetupOpts
function M.setup(opts)
Row.configure(opts or {})
vim.api.nvim_create_autocmd({ "BufEnter", "LspAttach" }, {
group = GROUP,
callback = function(ev)
local session = session_by_buf[ev.buf]
if session and session.enabled then
session:refresh()
end
end,
})
vim.api.nvim_create_autocmd({ "BufDelete", "BufWipeout" }, {
group = GROUP,
callback = function(ev)
local session = session_by_buf[ev.buf]
if session then
session:abort()
end
session_by_buf[ev.buf] = nil
end,
})
end
return M