84 lines
2.1 KiB
Lua
84 lines
2.1 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 })
|
|
|
|
local M = {}
|
|
|
|
---@param buf? integer
|
|
---@return boolean
|
|
function M.is_enabled(buf)
|
|
buf = buf or vim.api.nvim_get_current_buf()
|
|
local session = Session.find(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()
|
|
Session.get(buf):enable(value)
|
|
end
|
|
|
|
---@param buf? integer
|
|
function M.toggle(buf)
|
|
buf = buf or vim.api.nvim_get_current_buf()
|
|
Session.get(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 {})
|
|
|
|
---@param err lsp.ResponseError?
|
|
---@param ctx lsp.HandlerContext
|
|
vim.lsp.handlers["workspace/codeLens/refresh"] = function(err, _, ctx)
|
|
if err then
|
|
log.warning(
|
|
"client %d: error on %s: %s",
|
|
ctx.client_id,
|
|
ctx.method,
|
|
err.message
|
|
)
|
|
end
|
|
for buf, session in pairs(Session.all()) 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.find(ev.buf)
|
|
if session and session.enabled then
|
|
session:refresh()
|
|
end
|
|
end,
|
|
})
|
|
|
|
vim.api.nvim_create_autocmd({ "BufDelete", "BufWipeout" }, {
|
|
group = GROUP,
|
|
callback = function(ev)
|
|
Session.remove(ev.buf)
|
|
end,
|
|
})
|
|
end
|
|
|
|
return M
|