navigator.lua/lua/navigator/codelens.lua

157 lines
4.3 KiB
Lua
Raw Normal View History

2021-08-21 23:19:11 +00:00
-- codelenses
-- https://github.com/josa42/nvim-lsp-codelenses/blob/master/lua/jg/lsp/codelenses.lua
-- https://github.com/neovim/neovim/blob/master/runtime/lua/vim/lsp/codelens.lua
local codelens = require('vim.lsp.codelens')
local log = require('navigator.util').log
local trace = require('navigator.util').trace
-- trace = log
local lsphelper = require('navigator.lspwrapper')
2021-08-21 23:19:11 +00:00
local api = vim.api
local M = {}
M.disabled = {}
local config = require('navigator').config_values()
local sign_name = 'NavigatorCodeLensLightBulb'
2021-08-21 23:19:11 +00:00
if vim.tbl_isempty(vim.fn.sign_getdefined(sign_name)) then
vim.fn.sign_define(sign_name, { text = config.icons.code_lens_action_icon, texthl = 'LspDiagnosticsSignHint' })
2021-08-21 23:19:11 +00:00
end
local sign_group = 'nvcodelensaction'
2021-08-21 23:19:11 +00:00
local get_current_winid = require('navigator.util').get_current_winid
local is_enabled = true
2021-08-21 23:19:11 +00:00
local code_lens_action = {}
local function _update_sign(line)
trace('update sign at line ', line)
2021-08-21 23:19:11 +00:00
local winid = get_current_winid()
if code_lens_action[winid] == nil then
code_lens_action[winid] = {}
end
if code_lens_action[winid].lightbulb_line ~= 0 then
vim.fn.sign_unplace(sign_group, { id = code_lens_action[winid].lightbulb_line, buffer = '%' })
2021-08-21 23:19:11 +00:00
end
if line then
-- log("updatasign", line, sign_group, sign_name)
vim.fn.sign_place(
line,
sign_group,
sign_name,
'%',
{ lnum = line + 1, priority = config.lsp.code_lens_action.sign_priority }
)
2021-08-21 23:19:11 +00:00
code_lens_action[winid].lightbulb_line = line
end
end
local codelens_hdlr = function(err, result, ctx, cfg)
2022-02-17 11:21:34 +00:00
trace(ctx, result)
M.codelens_ctx = ctx
2021-08-23 23:17:20 +00:00
if err or result == nil then
if err then
log('lsp code lens', vim.inspect(err), ctx, cfg)
end
2021-08-21 23:19:11 +00:00
return
end
trace('codelenes result', result)
2021-08-21 23:19:11 +00:00
for _, v in pairs(result) do
_update_sign(v.range.start.line)
end
end
2021-08-21 23:19:11 +00:00
2022-08-05 09:48:27 +00:00
function M.setup(bufnr)
2022-09-07 14:43:24 +00:00
log('setup for ****** ', bufnr)
vim.api.nvim_set_hl(0, 'LspCodeLens', { link = 'DiagnosticsHint', default = true })
vim.api.nvim_set_hl(0, 'LspCodeLensText', { link = 'DiagnosticsInformation', default = true })
vim.api.nvim_set_hl(0, 'LspCodeLensSign', { link = 'DiagnosticsInformation', default = true })
2022-08-05 09:48:27 +00:00
vim.api.nvim_set_hl(0, 'LspCodeLensSeparator', { link = 'Boolean', default = true })
vim.api.nvim_create_autocmd({ 'CursorHold', 'CursorHoldI', 'InsertLeave' }, {
group = vim.api.nvim_create_augroup('nv__codelenses', {}),
buffer = bufnr or vim.api.nvim_win_get_buf(),
callback = function()
2022-08-09 10:40:23 +00:00
require('navigator.codelens').refresh()
2022-08-05 09:48:27 +00:00
end,
})
2021-08-21 23:19:11 +00:00
end
M.lsp_clients = {}
function M.refresh()
2022-10-26 05:03:48 +00:00
if next(vim.lsp.get_active_clients({ buffer = 0 })) == nil then
log('Must have a client running to use lsp code action')
2021-08-23 02:56:59 +00:00
return
end
if not lsphelper.check_capabilities('codeLensProvider') then
2021-08-21 23:19:11 +00:00
return
end
2022-09-07 14:43:24 +00:00
M.inline()
2021-08-21 23:19:11 +00:00
end
local virtual_types_ns = api.nvim_create_namespace('ng_virtual_types')
2021-08-21 23:19:11 +00:00
function M.disable()
local bufnr = vim.api.nvim_get_current_buf()
vim.api.nvim_buf_clear_namespace(bufnr, virtual_types_ns, 0, -1)
is_enabled = false
end
function M.run_action()
local original_select = vim.ui.select
2022-02-17 11:21:34 +00:00
vim.ui.select = require('guihua.gui').select
2021-09-30 11:10:35 +00:00
2022-09-07 14:43:24 +00:00
log('codelens action')
codelens.run()
2022-02-17 11:21:34 +00:00
vim.defer_fn(function()
vim.ui.select = original_select
end, 1000)
end
M.inline = function()
local lsp = vim.lsp
if is_enabled == false then
return
end
if vim.fn.getcmdwintype() == ':' then
return
2021-08-22 10:13:00 +00:00
end
local bufnr = api.nvim_get_current_buf()
if next(vim.lsp.get_active_clients({ buffer = bufnr })) == nil then
return
end
if vim.tbl_contains(M.disabled, bufnr) then
return
end
local parameter = lsp.util.make_position_params()
2022-09-07 14:43:24 +00:00
local on_codelens = vim.lsp.handlers['textDocument/codeLens']
local ids = lsp.buf_request(bufnr, 'textDocument/codeLens', parameter, function(err, response, ctx, _)
if err then
log('lsp code lens', vim.inspect(err), ctx)
-- lets disable code lens for this buffer
vim.list_extend(M.disabled, { vim.api.nvim_get_current_buf() })
return
end
2022-09-07 14:43:24 +00:00
-- Clear previous highlighting
api.nvim_buf_clear_namespace(bufnr, virtual_types_ns, 0, -1)
if response then
trace(response)
on_codelens(err, response, ctx, _)
codelens_hdlr(err, response, ctx, _)
end
2022-09-07 14:43:24 +00:00
end)
if next(ids) == nil then
vim.list_extend(M.disabled, { vim.api.nvim_get_current_buf() })
end
2021-08-21 23:19:11 +00:00
end
return M