merge lsp_request check lsp cap before sending lsp request to client

neovim_0.6
ray-x 3 years ago
parent 36683e3646
commit c4bfcabd64

@ -100,8 +100,7 @@ _NgConfigValues = {
vim.cmd("command! -nargs=0 LspLog lua require'navigator.lspclient.config'.open_lsp_log()")
vim.cmd("command! -nargs=0 LspRestart lua require'navigator.lspclient.config'.reload_lsp()")
vim.cmd(
"command! -nargs=0 LspToggleFmt lua require'navigator.lspclient.mapping'.toggle_lspformat()<CR>")
vim.cmd("command! -nargs=0 LspToggleFmt lua require'navigator.lspclient.mapping'.toggle_lspformat()<CR>")
M.deprecated = function(cfg)
local warn = require'navigator.util'.warn
@ -170,9 +169,6 @@ M.setup = function(cfg)
require("navigator.implementation")
-- log("navigator loader")
if _NgConfigValues.lsp.code_action.enable then
vim.cmd [[autocmd CursorHold,CursorHoldI * lua require'navigator.codeAction'.code_action_prompt()]]
end
-- vim.cmd("autocmd BufNewFile,BufRead *.go setlocal noexpandtab tabstop=4 shiftwidth=4")
if not _NgConfigValues.loaded then

@ -123,8 +123,23 @@ local function def_preview(timeout_ms)
-- https://github.com/oblitum/goyo.vim/blob/master/autoload/goyo.vim#L108-L135
end
local def = function()
local bufnr = vim.api.nvim_get_current_buf()
local ref_params = vim.lsp.util.make_position_params()
vim.lsp.for_each_buffer_client(bufnr, function(client, client_id, _bufnr)
if client.resolved_capabilities.goto_definition then
client.request("textDocument/definition", ref_params, definition_hdlr , _bufnr)
end
end)
end
vim.lsp.handlers["textDocument/definition"] = definition_hdlr
return {
definition = def,
definition_handler = definition_hdlr,
definition_preview = def_preview,
declaration_handler = definition_hdlr,

@ -128,7 +128,8 @@ local function error_marker(result, ctx, config)
end
pos[#pos] = {line = p, sign = bar, severity = math.min(diag.severity, pos[#pos].severity)}
else
table.insert(pos, {
table.insert(pos,
{
line = p,
sign = _NgConfigValues.lsp.diagnostic_scrollbar_sign[1],
severity = diag.severity
@ -232,8 +233,7 @@ local diag_hdlr = mk_handler(function(err, result, ctx, config)
local row = pos.line
local line = (vim.api.nvim_buf_get_lines(bufnr1, row, row + 1, false) or {""})[1]
if line ~= nil then
item.text = head .. line .. _NgConfigValues.icons.diagnostic_head_description
.. v.message
item.text = head .. line .. _NgConfigValues.icons.diagnostic_head_description .. v.message
table.insert(item_list, item)
else
error("diagnostic result empty line", v, row, bufnr1)
@ -302,8 +302,7 @@ M.show_diagnostic = function()
if #display_items > 0 then
local listview = gui.new_list_view({
items = display_items,
api = _NgConfigValues.icons.diagnostic_file .. _NgConfigValues.icons.diagnostic_head
.. " Diagnostic ",
api = _NgConfigValues.icons.diagnostic_file .. _NgConfigValues.icons.diagnostic_head .. " Diagnostic ",
enable_preview_edit = true
})
trace("new buffer", listview.bufnr)
@ -352,8 +351,8 @@ function M.update_err_marker()
end
local bufnr = vim.api.nvim_get_current_buf()
local diag_cnt = get_count(bufnr, [[Error]]) + get_count(bufnr, [[Warning]])
+ get_count(bufnr, [[Info]]) + get_count(bufnr, [[Hint]])
local diag_cnt = get_count(bufnr, [[Error]]) + get_count(bufnr, [[Warning]]) + get_count(bufnr, [[Info]])
+ get_count(bufnr, [[Hint]])
-- redraw
if diag_cnt == 0 and _NG_VT_DIAG_NS ~= nil then

@ -19,8 +19,7 @@ local function add_locs(bufnr, result)
if #result < 1 then
return
end
symbol = string.format("%s_%i_%i_%i", symbol, bufnr, result[1].range.start.line,
result[1].range.start.character)
symbol = string.format("%s_%i_%i_%i", symbol, bufnr, result[1].range.start.line, result[1].range.start.character)
if _NG_hi_list[symbol] == nil then
_NG_hi_list[symbol] = {range = {}}
end
@ -201,6 +200,17 @@ local function cmd_nohl()
end
end
_G.nav_doc_hl = function()
local bufnr = vim.api.nvim_get_current_buf()
local ref_params = vim.lsp.util.make_position_params()
vim.lsp.for_each_buffer_client(bufnr, function(client, client_id, bufnr)
if client.resolved_capabilities.document_highlight then
client.request("textDocument/documentHighlight", ref_params, handle_document_highlight, bufnr)
end
end)
end
local function documentHighlight()
api.nvim_exec([[
autocmd ColorScheme * |
@ -210,35 +220,34 @@ local function documentHighlight()
augroup lsp_document_highlight
autocmd! * <buffer>
autocmd CursorHold <buffer> lua vim.lsp.buf.document_highlight()
autocmd CursorHold <buffer> lua nav_doc_hl()
autocmd CursorMoved <buffer> lua vim.lsp.buf.clear_references()
augroup END
]], false)
vim.lsp.handlers["textDocument/documentHighlight"] = mk_handler(
function(err, result, ctx)
local bufnr = ctx.bufnr
if err then
print(err)
return
end
if not result then
return
end
trace("dochl", result)
bufnr = api.nvim_get_current_buf()
vim.lsp.util.buf_clear_references(bufnr)
vim.lsp.util.buf_highlight_references(bufnr, result)
bufnr = bufnr or 0
if type(result) ~= "table" then
vim.lsp.util.buf_clear_references(bufnr)
return
end
table.sort(result, function(a, b)
return before(a.range, b.range)
end)
references[bufnr] = result
add_locs(bufnr, result)
end)
vim.lsp.handlers["textDocument/documentHighlight"] = mk_handler(function(err, result, ctx)
local bufnr = ctx.bufnr
if err then
print(err)
return
end
if not result then
return
end
trace("dochl", result)
bufnr = api.nvim_get_current_buf()
vim.lsp.util.buf_clear_references(bufnr)
vim.lsp.util.buf_highlight_references(bufnr, result)
bufnr = bufnr or 0
if type(result) ~= "table" then
vim.lsp.util.buf_clear_references(bufnr)
return
end
table.sort(result, function(a, b)
return before(a.range, b.range)
end)
references[bufnr] = result
add_locs(bufnr, result)
end)
end
return {

@ -7,11 +7,15 @@ return {
return
end
local util = require "navigator.util"
local log = util.log
-- If the buffer hasn't been modified before the formatting has finished,
-- update the buffer
-- if not vim.api.nvim_buf_get_option(ctx.bufnr, 'modified') then
vim.defer_fn(function()
log('fmt callback')
if ctx.bufnr == vim.api.nvim_get_current_buf()
or not vim.api.nvim_buf_get_option(ctx.bufnr, 'modified') then
@ -26,6 +30,7 @@ return {
vim.api.nvim_command('silent doautocmd <nomodeline> User FormatterPost')
-- end
end
end, 20)
end, 100)
end)
}

@ -20,7 +20,7 @@ M.on_attach = function(client, bufnr)
return {error = "invalid file", result = nil}
end
log("attaching", bufnr, client.name, uri)
trace(client)
log(client)
diagnostic_map(bufnr)
-- add highlight for Lspxxx
@ -51,6 +51,12 @@ M.on_attach = function(client, bufnr)
config.lsp[client.name].on_attach(client, bufnr)
end
if _NgConfigValues.lsp.code_action.enable then
if client.resolved_capabilities.code_action then
log('code action enabled for client', client.resolved_capabilities.code_action)
vim.cmd [[autocmd CursorHold,CursorHoldI <buffer> lua require'navigator.codeAction'.code_action_prompt()]]
end
end
end
-- M.setup = function(cfg)

@ -476,7 +476,17 @@ local function lsp_startup(ft, retry, user_lsp_opts)
if not _NG_Loaded['efm'] then
local efm_cfg = user_lsp_opts['efm']
if efm_cfg then
lspconfig.efm.setup(efm_cfg)
local cfg = {}
cfg = vim.tbl_deep_extend("keep", cfg, efm_cfg)
cfg.on_attach = function(client, bufnr)
if efm_cfg.on_attach then
efm_cfg.on_attach(client, bufnr)
end
on_attach(client, bufnr)
end
lspconfig.efm.setup(cfg)
log('efm loading')
_NG_Loaded['efm'] = true
end

@ -12,12 +12,12 @@ local double = {"╔", "═", "╗", "║", "╝", "═", "╚", "║"}
local single = {"", "", "", "", "", "", "", ""}
-- LuaFormatter off
local key_maps = {
{key = "gr", func = "references()"},
{key = "gr", func = "require('navigator.reference').reference()"},
{mode = "i", key = "<M-k>", func = "signature_help()"},
{key = "<c-k>", func = "signature_help()"},
{key = "g0", func = "document_symbol()"},
{key = "g0", func = "require('navigator.symbols').document_symbols()"},
{key = "gW", func = "workspace_symbol()"},
{key = "<c-]>", func = "definition()"},
{key = "<c-]>", func = "require('navigator.definition').definition()"},
{key = "gD", func = "declaration({ border = 'rounded', max_width = 80 })"},
{key = "gp", func = "require('navigator.definition').definition_preview()"},
{key = "gT", func = "require('navigator.treesitter').buf_ts()"},
@ -61,6 +61,7 @@ local check_cap = function()
local fmt, rfmt, ccls
for _, value in pairs(vim.lsp.buf_get_clients(0)) do
if value ~= nil and value.resolved_capabilities == nil then
log(value)
if value.resolved_capabilities.document_formatting then
fmt = true
end
@ -78,6 +79,7 @@ local check_cap = function()
end
local function set_mapping(user_opts)
log('setup mapping')
local opts = {noremap = true, silent = true}
user_opts = user_opts or {}
@ -138,6 +140,7 @@ local function set_mapping(user_opts)
end
-- if user_opts.cap.document_formatting then
if doc_fmt then
vim.cmd([[
aug NavigatorAuFormat
@ -180,8 +183,7 @@ local function set_event_handler(user_opts)
else
f = "lua vim.lsp.buf." .. value.func
end
local cmd = "autocmd FileType " .. file_types .. " autocmd nvim_lsp_autos " .. value.ev
.. " <buffer> silent! " .. f
local cmd = "autocmd FileType " .. file_types .. " autocmd nvim_lsp_autos " .. value.ev .. " <buffer> silent! " .. f
vim.api.nvim_command(cmd)
end
vim.api.nvim_command([[augroup END]])
@ -220,10 +222,8 @@ function M.setup(user_opts)
log('lsp cap:', cap)
if cap.call_hierarchy or cap.callHierarchy then
vim.lsp.handlers["callHierarchy/incomingCalls"] =
require"navigator.hierarchy".incoming_calls_handler
vim.lsp.handlers["callHierarchy/outgoingCalls"] =
require"navigator.hierarchy".outgoing_calls_handler
vim.lsp.handlers["callHierarchy/incomingCalls"] = require"navigator.hierarchy".incoming_calls_handler
vim.lsp.handlers["callHierarchy/outgoingCalls"] = require"navigator.hierarchy".outgoing_calls_handler
end
vim.lsp.handlers["textDocument/references"] = require"navigator.reference".reference_handler
@ -234,16 +234,12 @@ function M.setup(user_opts)
vim.lsp.handlers["textDocument/declaration"] = require"navigator.definition".declaration_handler
end
vim.lsp.handlers["textDocument/typeDefinition"] =
require"navigator.definition".typeDefinition_handler
vim.lsp.handlers["textDocument/implementation"] =
require"navigator.implementation".implementation_handler
vim.lsp.handlers["textDocument/typeDefinition"] = require"navigator.definition".typeDefinition_handler
vim.lsp.handlers["textDocument/implementation"] = require"navigator.implementation".implementation_handler
vim.lsp.handlers["textDocument/documentSymbol"] =
require"navigator.symbols".document_symbol_handler
vim.lsp.handlers["textDocument/documentSymbol"] = require"navigator.symbols".document_symbol_handler
vim.lsp.handlers["workspace/symbol"] = require"navigator.symbols".workspace_symbol_handler
vim.lsp.handlers["textDocument/publishDiagnostics"] =
require"navigator.diagnostics".diagnostic_handler
vim.lsp.handlers["textDocument/publishDiagnostics"] = require"navigator.diagnostics".diagnostic_handler
-- TODO: when active signature merge to neovim, remove this setup:
@ -254,11 +250,9 @@ function M.setup(user_opts)
sig.setup(_NgConfigValues.signature_help_cfg)
end
else
vim.lsp.handlers["textDocument/signatureHelp"] = vim.lsp.with(
require"navigator.signature".signature_handler,
{
border = {"", "", "", "", "", "", "", ""}
})
vim.lsp.handlers["textDocument/signatureHelp"] = vim.lsp.with(require"navigator.signature".signature_handler, {
border = {"", "", "", "", "", "", "", ""}
})
end
vim.lsp.handlers["textDocument/hover"] = vim.lsp.with(vim.lsp.handlers.hover, {border = single})

@ -64,8 +64,8 @@ function M.lines_from_locations(locations, include_filename)
local lines = {}
for _, loc in ipairs(locations) do
table.insert(lines, (fnamemodify(loc["filename"]) .. loc["lnum"] .. ":" .. loc["col"] .. ": "
.. vim.trim(loc["text"])))
table.insert(lines,
(fnamemodify(loc["filename"]) .. loc["lnum"] .. ":" .. loc["col"] .. ": " .. vim.trim(loc["text"])))
end
return lines
@ -142,8 +142,7 @@ end
function M.call_sync(method, params, opts, handler)
params = params or {}
opts = opts or {}
local results_lsp, err = lsp.buf_request_sync(0, method, params,
opts.timeout or vim.g.navtator_timeout or 1000)
local results_lsp, err = lsp.buf_request_sync(0, method, params, opts.timeout or vim.g.navtator_timeout or 1000)
if nvim_0_6() then
handler(err, extract_result(results_lsp), {method = method}, nil)
@ -214,8 +213,7 @@ local function ts_definition(uri, range)
return nil
end
local key = string.format('%s_%d_%d_%d', uri, range.start.line, range.start.character,
range['end'].line)
local key = string.format('%s_%d_%d_%d', uri, range.start.line, range.start.character, range['end'].line)
local tsnode = ts_nodes:get(key)
local ftime = ts_nodes_time:get(key)
@ -255,8 +253,7 @@ local function find_ts_func_by_range(funcs, range)
for _, value in pairs(funcs) do
local func_range = value.node_scope
-- note treesitter is C style
if func_range and func_range.start.line <= range.start.line and func_range['end'].line
>= range['end'].line then
if func_range and func_range.start.line <= range.start.line and func_range['end'].line >= range['end'].line then
table.insert(result, value)
end
end
@ -519,4 +516,13 @@ function M.symbol_to_items(locations)
return items
end
function M.request(method, hdlr) -- e.g textDocument/reference
local bufnr = vim.api.nvim_get_current_buf()
local ref_params = vim.lsp.util.make_position_params()
vim.lsp.for_each_buffer_client(bufnr, function(client, client_id, _bufnr)
client.request(method, ref_params, hdlr, bufnr)
end)
end
return M

@ -87,6 +87,7 @@ local ref_view = function(err, locations, ctx, cfg)
end
local ref_hdlr = mk_handler(function(err, locations, ctx, cfg)
_NgConfigValues.closer = nil
trace(err, locations, ctx, cfg)
M.async_hdlr = vim.loop.new_async(vim.schedule_wrap(function()
ref_view(err, locations, ctx, cfg)
@ -94,11 +95,41 @@ local ref_hdlr = mk_handler(function(err, locations, ctx, cfg)
end))
M.async_hdlr:send()
end)
local async_reference_request = function()
-- local async_reference_request = function()
-- local ref_params = vim.lsp.util.make_position_params()
-- ref_params.context = {includeDeclaration = true}
-- -- lsp.call_async("textDocument/references", ref_params, ref_hdlr) -- return asyncresult, canceller
-- lsp.call_async("textDocument/references", ref_params, ref_hdlr) -- return asyncresult, canceller
-- end
local ref_req = function()
if _NgConfigValues.closer ~= nil then
-- do not call it twice
_NgConfigValues.closer()
end
local ref_params = vim.lsp.util.make_position_params()
ref_params.context = {includeDeclaration = true}
-- lsp.call_async("textDocument/references", ref_params, ref_hdlr) -- return asyncresult, canceller
lsp.call_async("textDocument/definition", ref_params, ref_hdlr) -- return asyncresult, canceller
local bufnr = vim.api.nvim_get_current_buf()
log(bufnr)
local ids, closer = vim.lsp.buf_request(bufnr, "textDocument/references", ref_params, ref_hdlr)
log(ids)
_NgConfigValues.closer = closer
return ids, closer
end
local ref = function()
local bufnr = vim.api.nvim_get_current_buf()
local ref_params = vim.lsp.util.make_position_params()
vim.lsp.for_each_buffer_client(bufnr, function(client, client_id, bufnr)
if client.resolved_capabilities.find_references then
client.request("textDocument/references", ref_params, ref_hdlr, bufnr)
end
end)
end
return {reference_handler = ref_hdlr, show_reference = async_reference_request, ref_view = ref_view}
return {reference_handler = ref_hdlr, reference = ref_req, ref_view = ref_view}

@ -34,47 +34,48 @@ local symbols_to_items = lsphelper.symbols_to_items
-- end
-- end
function M.workspace_symbols(opts)
function M.workspace_symbols(query)
opts = opts or {}
assert(#vim.lsp.buf_get_clients() > 0, "Must have a client running")
local lspopts = {
loc = "top_center",
prompt = true,
-- rawdata = true,
api = ""
}
query = query or pcall(vim.fn.input, "Query: ")
local bufnr = vim.api.nvim_get_current_buf()
vim.list_extend(lspopts, opts)
local params = {query=query}
vim.lsp.for_each_buffer_client(bufnr, function(client, client_id, _bufnr)
if client.resolved_capabilities.workspace_symbol then
client.request("workspace/symbol", params, M.workspace_symbol_handler, _bufnr)
end
end)
end
function M.document_symbols(opts)
opts = opts or {}
local lspopts = {
loc = "top_center",
prompt = true,
-- rawdata = true,
api = ""
}
local bufnr = vim.api.nvim_get_current_buf()
vim.list_extend(lspopts, opts)
local params = vim.lsp.util.make_position_params()
params.context = {includeDeclaration = true}
params.query = opts.prompt or ""
local results_lsp = vim.lsp.buf_request_sync(0, "workspace/symbol", params,
lspopts.timeout or 15000)
if not results_lsp or vim.tbl_isempty(results_lsp) then
print("symbol not found for buf")
return
end
-- result_lsp
local result = {}
for i = 1, #results_lsp do
if results_lsp[i] ~= nil and results_lsp[i].result ~= nil and #results_lsp[i].result > 0 then
result = results_lsp[i].result
vim.lsp.for_each_buffer_client(bufnr, function(client, client_id, _bufnr)
if client.resolved_capabilities.document_symbol then
client.request("textDocument/documentSymbol", params, M.document_symbol_handler, _bufnr)
end
end
local items = symbols_to_items(result)
-- log(#items, items[1])
local ft = vim.api.nvim_buf_get_option(0, "ft")
if #items > 0 then
lspopts.items = items
lspopts.ft = ft
gui.new_list_view(lspopts)
else
print("symbols not found")
end
end)
end
M.document_symbol_handler = mk_handler(function(err, result, ctx)
if err then
print("failed to get document symbol", ctx)
@ -202,4 +203,5 @@ M.workspace_symbol_handler = mk_handler(function(err, result, ctx, cfg)
-- opts.data = data
end)
return M

Loading…
Cancel
Save