doc update for lsp-installer

pull/117/head
ray-x 2 years ago
parent 5c13477220
commit ed96b65003

@ -490,6 +490,17 @@ cmd = { "/Users/username/.local/share/nvim/lsp_servers/python/node_modules/.bin/
```
The lsp servers installed by nvim-lsp-installer is in following dir
```lua
local path = require 'nvim-lsp-installer.path'
local install_root_dir = path.concat {vim.fn.stdpath 'data', 'lsp_servers'}
```
And you can setup binary full path to this: (e.g. with gopls)
`install_root_dir .. '/go/gopls'`
## Usage
Please refer to lua/navigator/lspclient/mapping.lua on key mappings. Should be able to work out-of-box.

@ -5,13 +5,13 @@ local code_action = {}
local gui = require "navigator.gui"
local config = require("navigator").config_values()
local api = vim.api
-- trace = log
trace = log
local sign_name = "NavigatorLightBulb"
--- `codeAction/resolve`
-- from neovim buf.lua, change vim.ui.select to gui
local function on_code_action_results(results, ctx)
local function on_code_action_results(results, ctx, client)
local trace = log
local action_tuples = {}
@ -126,6 +126,7 @@ end
local diagnostic = vim.diagnostic or vim.lsp.diagnostic
code_action.code_action_handler = util.mk_handler(function(err, results, ctx, cfg)
log(ctx)
if err ~= nil then
log("code action err", err, results, ctx, cfg)
return

@ -3,55 +3,61 @@
-- 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 mk_handler = require"navigator.util".mk_handler
local nvim_0_6 = require"navigator.util".nvim_0_6
local trace = require"navigator.util".trace
local log = require('navigator.util').log
local mk_handler = require('navigator.util').mk_handler
local nvim_0_6 = require('navigator.util').nvim_0_6
local trace = require('navigator.util').trace
local lsphelper = require "navigator.lspwrapper"
local lsphelper = require('navigator.lspwrapper')
local api = vim.api
local gui = require "navigator.gui"
local gui = require('navigator.gui')
local M = {}
local config = require("navigator").config_values()
local sign_name = "NavigatorCodeLensLightBulb"
local config = require('navigator').config_values()
local sign_name = 'NavigatorCodeLensLightBulb'
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"})
vim.fn.sign_define(sign_name, { text = config.icons.code_lens_action_icon, texthl = 'LspDiagnosticsSignHint' })
end
local sign_group = "nvcodelensaction"
local sign_group = 'nvcodelensaction'
local get_current_winid = require('navigator.util').get_current_winid
local code_lens_action = {}
local function _update_sign(line)
trace("update sign at line ", line)
trace('update sign at line ', line)
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 = "%"})
vim.fn.sign_unplace(sign_group, { id = code_lens_action[winid].lightbulb_line, buffer = '%' })
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})
vim.fn.sign_place(
line,
sign_group,
sign_name,
'%',
{ lnum = line + 1, priority = config.lsp.code_lens_action.sign_priority }
)
code_lens_action[winid].lightbulb_line = line
end
end
local codelens_hdlr = mk_handler(function(err, result, ctx, cfg)
log(ctx, result)
M.codelens_ctx = ctx
if err or result == nil then
if err then
log("lsp code lens", vim.inspect(err), ctx, cfg)
log('lsp code lens', vim.inspect(err), ctx, cfg)
end
return
end
trace("codelenes result", result)
trace('codelenes result', result)
for _, v in pairs(result) do
_update_sign(v.range.start.line)
end
@ -65,43 +71,41 @@ function M.setup()
vim.cmd('augroup navigator.codelenses')
vim.cmd(' autocmd!')
vim.cmd(
"autocmd BufEnter,CursorHold,InsertLeave <buffer> lua require('navigator.codelens').refresh()")
vim.cmd("autocmd BufEnter,CursorHold,InsertLeave <buffer> lua require('navigator.codelens').refresh()")
vim.cmd('augroup end')
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()}
if nvim_0_6() then
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, _, result, ctx.client_id or 0, ctx.bufnr or 0)
end
end)
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() }
if nvim_0_6() then
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, _, result, ctx.client_id or 0, ctx.bufnr or 0)
end
end)
end
M.lsp_clients = {}
function M.refresh()
if #vim.lsp.buf_get_clients() < 1 then
log("Must have a client running to use lsp code action")
log('Must have a client running to use lsp code action')
return
end
if not lsphelper.check_capabilities("code_lens") then
if not lsphelper.check_capabilities('code_lens') then
return
end
vim.lsp.codelens.refresh()
end
function M.run_action()
log("run code len action")
log('run code len action')
assert(#vim.lsp.buf_get_clients() > 0, "Must have a client running to use lsp code action")
if not lsphelper.check_capabilities("code_lens") then
assert(#vim.lsp.buf_get_clients() > 0, 'Must have a client running to use lsp code action')
if not lsphelper.check_capabilities('code_lens') then
return
end
@ -116,14 +120,14 @@ function M.run_action()
local width = 40
local data = {
" " .. _NgConfigValues.icons.code_lens_action_icon .. " CodeLens Action <C-o> Apply <C-e> Exit"
' ' .. _NgConfigValues.icons.code_lens_action_icon .. ' CodeLens Action <C-o> Apply <C-e> Exit',
}
local idx = 1
for i, lens in pairs(lenses) do
if lens.range.start.line == (line - 1) then
local title = lens.command.title:gsub("\r\n", "\\r\\n")
title = title:gsub("\n", "\\n")
title = string.format("[%d] %s", idx, title)
local title = lens.command.title:gsub('\r\n', '\\r\\n')
title = title:gsub('\n', '\\n')
title = string.format('[%d] %s', idx, title)
table.insert(data, title)
lenses[i].display_title = title
width = math.max(width, #lens.command.title)
@ -139,21 +143,21 @@ function M.run_action()
end
end
if action_chosen == nil then
log("no match for ", action, lenses)
log('no match for ', action, lenses)
return
end
apply(action_chosen)
apply(action_chosen, M.codelens_ctx)
end
local divider = string.rep('', width + 2)
table.insert(data, 2, divider)
if #data > 2 then
local lv = gui.new_list_view {
local lv = gui.new_list_view({
items = data,
width = width + 4,
loc = "top_center",
relative = "cursor",
loc = 'top_center',
relative = 'cursor',
rawdata = true,
data = data,
on_confirm = function(pos)
@ -163,17 +167,16 @@ function M.run_action()
on_move = function(pos)
log(pos)
return pos
end
}
end,
})
vim.api.nvim_buf_add_highlight(lv.bufnr, -1, 'Title', 0, 0, -1)
else
print('no codelense in current line')
end
end
local virtual_types_ns = api.nvim_create_namespace("ng_virtual_types");
local virtual_types_ns = api.nvim_create_namespace('ng_virtual_types')
function M.disable()
local bufnr = vim.api.nvim_get_current_buf()
@ -195,7 +198,7 @@ M.inline = function()
local bufnr = api.nvim_get_current_buf()
local parameter = lsp.util.make_position_params()
local response = lsp.buf_request_sync(bufnr, "textDocument/codeLens", parameter)
local response = lsp.buf_request_sync(bufnr, 'textDocument/codeLens', parameter)
-- Clear previous highlighting
api.nvim_buf_clear_namespace(bufnr, virtual_types_ns, 0, -1)
@ -222,16 +225,15 @@ M.inline = function()
log(msg)
api.nvim_buf_set_extmark(bufnr, virtual_types_ns, start_line, -1, {
virt_text = {{msg, "LspCodeLensText"}},
virt_text = { { msg, 'LspCodeLensText' } },
virt_text_pos = 'overlay',
hl_mode = 'combine'
hl_mode = 'combine',
})
end
end
-- else
-- api.nvim_command("echohl WarningMsg | echo 'VirtualTypes: No response' | echohl None")
end
end
return M

@ -417,27 +417,11 @@ function M.locations_to_items(locations, max_items)
return items, width + 24, second_part -- TODO handle long line?
end
function M.apply_action(action, ctx, client)
assert(action ~= nil, 'action must not be nil')
if action.edit then
vim.lsp.util.apply_workspace_edit(action.edit)
end
if action.command then
local command = type(action.command) == 'table' and action.command or action
local fn = client.commands[command.command] or (vim.lsp.commands and vim.lsp.commands[command.command])
if fn then
local enriched_ctx = vim.deepcopy(ctx)
enriched_ctx.client_id = client.id
fn(command, enriched_ctx)
else
M.execute_command(command)
end
end
local function apply_action(action, client)
-- local client = vim.lsp.get_client_by_id(ctx.client_id)
log(action)
end
local function apply_action(action, client, ctx)
log(action, client)
assert(action ~= nil, 'action must not be nil')
if action.edit then
require('vim.lsp.util').apply_workspace_edit(action.edit)
end
@ -454,7 +438,10 @@ local function apply_action(action, client, ctx)
end
end
M.apply_action = apply_action
function M.on_user_choice(action_tuple, ctx)
log(ctx)
if not action_tuple then
return
end
@ -471,7 +458,27 @@ function M.on_user_choice(action_tuple, ctx)
-- command: string
-- arguments?: any[]
--
local client = vim.lsp.get_client_by_id(action_tuple[1])
local function apply_code_action(action, client)
if action.edit then
util.apply_workspace_edit(action.edit)
end
if action.command then
local command = type(action.command) == 'table' and action.command or action
local fn = client.commands[command.command] or vim.lsp.commands[command.command]
if fn then
local enriched_ctx = vim.deepcopy(ctx)
enriched_ctx.client_id = client.id
fn(command, enriched_ctx)
else
M.execute_command(command)
end
end
end
if action_tuple[1] ~= nil then
ctx.client_id = action_tuple[1]
end
local client = vim.lsp.get_client_by_id(ctx.client_id)
local action = action_tuple[2]
if
not action.edit
@ -479,15 +486,16 @@ function M.on_user_choice(action_tuple, ctx)
and type(client.resolved_capabilities.code_action) == 'table'
and client.resolved_capabilities.code_action.resolveProvider
then
client.request('codeAction/resolve', action, function(err, resolved_action)
client.request('codeAction/resolve', action, function(err, resolved_action, c)
log(resolved_action, c)
if err then
vim.notify(err.code .. ': ' .. err.message, vim.log.levels.ERROR)
return
end
apply_action(resolved_action, client, ctx)
apply_code_action(resolved_action, client)
end)
else
apply_action(action, client, ctx)
apply_action(action, client)
end
end

Loading…
Cancel
Save