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')
|
|
|
|
|
2021-12-16 21:54:36 +00:00
|
|
|
local log = require('navigator.util').log
|
|
|
|
local mk_handler = require('navigator.util').mk_handler
|
2022-02-17 11:21:34 +00:00
|
|
|
local nvim_0_6_1 = require('navigator.util').nvim_0_6_1
|
2021-12-16 21:54:36 +00:00
|
|
|
local trace = require('navigator.util').trace
|
2021-08-21 23:19:11 +00:00
|
|
|
|
2021-12-16 21:54:36 +00:00
|
|
|
local lsphelper = require('navigator.lspwrapper')
|
2021-08-21 23:19:11 +00:00
|
|
|
local api = vim.api
|
2021-12-16 21:54:36 +00:00
|
|
|
local gui = require('navigator.gui')
|
2021-08-21 23:19:11 +00:00
|
|
|
local M = {}
|
|
|
|
|
2021-12-16 21:54:36 +00:00
|
|
|
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
|
2021-12-16 21:54:36 +00:00
|
|
|
vim.fn.sign_define(sign_name, { text = config.icons.code_lens_action_icon, texthl = 'LspDiagnosticsSignHint' })
|
2021-08-21 23:19:11 +00:00
|
|
|
end
|
|
|
|
|
2021-12-16 21:54:36 +00:00
|
|
|
local sign_group = 'nvcodelensaction'
|
2021-08-21 23:19:11 +00:00
|
|
|
|
|
|
|
local get_current_winid = require('navigator.util').get_current_winid
|
|
|
|
|
2021-12-16 21:54:36 +00:00
|
|
|
local is_enabled = true
|
2021-08-21 23:19:11 +00:00
|
|
|
local code_lens_action = {}
|
|
|
|
|
|
|
|
local function _update_sign(line)
|
2021-12-16 21:54:36 +00:00
|
|
|
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
|
2021-12-16 21:54:36 +00:00
|
|
|
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)
|
2021-12-16 21:54:36 +00:00
|
|
|
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
|
|
|
|
|
2021-09-05 22:34:26 +00:00
|
|
|
local codelens_hdlr = mk_handler(function(err, result, ctx, cfg)
|
2022-02-17 11:21:34 +00:00
|
|
|
trace(ctx, result)
|
2021-12-16 21:54:36 +00:00
|
|
|
M.codelens_ctx = ctx
|
2021-08-23 23:17:20 +00:00
|
|
|
if err or result == nil then
|
2021-09-20 15:44:55 +00:00
|
|
|
if err then
|
2021-12-16 21:54:36 +00:00
|
|
|
log('lsp code lens', vim.inspect(err), ctx, cfg)
|
2021-09-20 15:44:55 +00:00
|
|
|
end
|
2021-08-21 23:19:11 +00:00
|
|
|
return
|
|
|
|
end
|
2021-12-16 21:54:36 +00:00
|
|
|
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
|
2021-09-05 22:34:26 +00:00
|
|
|
end)
|
2021-08-21 23:19:11 +00:00
|
|
|
|
|
|
|
function M.setup()
|
|
|
|
vim.cmd('highlight! link LspCodeLens LspDiagnosticsHint')
|
|
|
|
vim.cmd('highlight! link LspCodeLensText LspDiagnosticsInformation')
|
|
|
|
vim.cmd('highlight! link LspCodeLensTextSign LspDiagnosticsSignInformation')
|
|
|
|
vim.cmd('highlight! link LspCodeLensTextSeparator Boolean')
|
|
|
|
|
|
|
|
vim.cmd('augroup navigator.codelenses')
|
|
|
|
vim.cmd(' autocmd!')
|
2021-12-16 21:54:36 +00:00
|
|
|
vim.cmd("autocmd BufEnter,CursorHold,InsertLeave <buffer> lua require('navigator.codelens').refresh()")
|
2021-08-21 23:19:11 +00:00
|
|
|
vim.cmd('augroup end')
|
2021-12-16 21:54:36 +00:00
|
|
|
local on_codelens = vim.lsp.handlers['textDocument/codeLens']
|
|
|
|
vim.lsp.handlers['textDocument/codeLens'] = mk_handler(function(err, result, ctx, cfg)
|
|
|
|
-- trace(err, result, ctx.client_id, ctx.bufnr, cfg or {})
|
|
|
|
cfg = cfg or {}
|
|
|
|
ctx = ctx or { bufnr = vim.api.nvim_get_current_buf() }
|
2022-02-17 11:21:34 +00:00
|
|
|
if nvim_0_6_1() then
|
2021-12-16 21:54:36 +00:00
|
|
|
on_codelens(err, result, ctx, cfg)
|
|
|
|
codelens_hdlr(err, result, ctx, cfg)
|
|
|
|
else
|
|
|
|
on_codelens(err, ctx.method, result, ctx.client_id, ctx.bufnr)
|
|
|
|
codelens_hdlr(err, nil, result, ctx.client_id or 0, ctx.bufnr or 0)
|
|
|
|
end
|
|
|
|
end)
|
2021-08-21 23:19:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
M.lsp_clients = {}
|
|
|
|
|
|
|
|
function M.refresh()
|
2021-08-23 02:56:59 +00:00
|
|
|
if #vim.lsp.buf_get_clients() < 1 then
|
2021-12-16 21:54:36 +00:00
|
|
|
log('Must have a client running to use lsp code action')
|
2021-08-23 02:56:59 +00:00
|
|
|
return
|
|
|
|
end
|
2021-12-16 21:54:36 +00:00
|
|
|
if not lsphelper.check_capabilities('code_lens') then
|
2021-08-21 23:19:11 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
vim.lsp.codelens.refresh()
|
|
|
|
end
|
|
|
|
|
2021-12-16 21:54:36 +00:00
|
|
|
local virtual_types_ns = api.nvim_create_namespace('ng_virtual_types')
|
2021-08-21 23:19:11 +00:00
|
|
|
|
2021-12-16 21:54:36 +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
|
2021-09-19 02:42:34 +00:00
|
|
|
|
2021-12-16 21:54:36 +00:00
|
|
|
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
|
|
|
|
2021-12-16 21:54:36 +00:00
|
|
|
log('codeaction')
|
2021-09-19 02:42:34 +00:00
|
|
|
|
2021-12-16 21:54:36 +00:00
|
|
|
codelens.run()
|
2022-02-17 11:21:34 +00:00
|
|
|
vim.defer_fn(function()
|
|
|
|
vim.ui.select = original_select
|
|
|
|
end, 1000)
|
2021-09-19 02:42:34 +00:00
|
|
|
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
|
2021-09-19 02:42:34 +00:00
|
|
|
if #vim.lsp.buf_get_clients() == 0 then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local bufnr = api.nvim_get_current_buf()
|
|
|
|
local parameter = lsp.util.make_position_params()
|
2021-12-16 21:54:36 +00:00
|
|
|
local response = lsp.buf_request_sync(bufnr, 'textDocument/codeLens', parameter)
|
2021-09-19 02:42:34 +00:00
|
|
|
|
|
|
|
-- Clear previous highlighting
|
|
|
|
api.nvim_buf_clear_namespace(bufnr, virtual_types_ns, 0, -1)
|
|
|
|
|
|
|
|
if response then
|
|
|
|
log(response)
|
|
|
|
for _, v in ipairs(response) do
|
|
|
|
if v == nil or v.result == nil then
|
|
|
|
return
|
|
|
|
end -- no response
|
|
|
|
for _, vv in pairs(v.result) do
|
|
|
|
local start_line = -1
|
|
|
|
for _, vvv in pairs(vv.range) do
|
|
|
|
start_line = tonumber(vvv.line)
|
|
|
|
end
|
|
|
|
|
|
|
|
local cmd = vv.command
|
|
|
|
local msg = _NgConfigValues.icons.code_action_icon .. ' '
|
|
|
|
if cmd then
|
|
|
|
local txt = cmd.title or ''
|
|
|
|
txt = txt .. ' ' .. (cmd.command or '') .. ' '
|
|
|
|
msg = msg .. txt .. ' '
|
|
|
|
end
|
|
|
|
|
|
|
|
log(msg)
|
|
|
|
api.nvim_buf_set_extmark(bufnr, virtual_types_ns, start_line, -1, {
|
2021-12-16 21:54:36 +00:00
|
|
|
virt_text = { { msg, 'LspCodeLensText' } },
|
2021-09-19 02:42:34 +00:00
|
|
|
virt_text_pos = 'overlay',
|
2021-12-16 21:54:36 +00:00
|
|
|
hl_mode = 'combine',
|
2021-09-19 02:42:34 +00:00
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- else
|
|
|
|
-- api.nvim_command("echohl WarningMsg | echo 'VirtualTypes: No response' | echohl None")
|
|
|
|
end
|
2021-08-21 23:19:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return M
|