97 lines
2.5 KiB
Lua
97 lines
2.5 KiB
Lua
---@class ow.lsp.CodeLensResponse : ow.lsp.Response
|
|
---@field result lsp.CodeLens[]
|
|
|
|
local Row = require("lsp.codelens.row")
|
|
local Session = require("lsp.codelens.session")
|
|
local log = require("log")
|
|
|
|
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 {})
|
|
|
|
local method = vim.lsp.protocol.Methods.workspace_codeLens_refresh
|
|
vim.lsp.handlers[method] = function(err, _, ctx)
|
|
if err then
|
|
log.warning(
|
|
"client %d: error on %s: %s",
|
|
ctx.client_id,
|
|
method,
|
|
err.message
|
|
)
|
|
end
|
|
for buf, session in pairs(session_by_buf) do
|
|
if
|
|
session.enabled
|
|
and #vim.lsp.get_clients({ bufnr = buf, id = ctx.client_id })
|
|
> 0
|
|
then
|
|
session:refresh()
|
|
end
|
|
end
|
|
return vim.NIL
|
|
end
|
|
|
|
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
|