change the way checking total number of clients attached #167

serverCapcities
ray-x 2 years ago
parent 8dbac5efc9
commit 8b43ed23e2

@ -13,7 +13,7 @@ local function call_hierarchy_handler(direction, err, result, ctx, cfg, error_me
log('call_hierarchy') log('call_hierarchy')
log('call_hierarchy', direction, err, result, ctx, cfg) log('call_hierarchy', direction, err, result, ctx, cfg)
assert(#vim.lsp.buf_get_clients() > 0, 'Must have a client running to use lsp_tags') assert(next(vim.lsp.buf_get_clients()), 'Must have a client running to use lsp_tags')
if err ~= nil then if err ~= nil then
log('hierarchy error', ctx, 'dir', direction, 'result', result, 'err', err) log('hierarchy error', ctx, 'dir', direction, 'result', result, 'err', err)
vim.notify('ERROR: ' .. error_message, vim.lsp.log_levels.WARN) vim.notify('ERROR: ' .. error_message, vim.lsp.log_levels.WARN)
@ -52,7 +52,8 @@ local call_hierarchy_handler_from = partial(call_hierarchy_handler, 'from')
local call_hierarchy_handler_to = partial(call_hierarchy_handler, 'to') local call_hierarchy_handler_to = partial(call_hierarchy_handler, 'to')
local function incoming_calls_handler(bang, err, result, ctx, cfg) local function incoming_calls_handler(bang, err, result, ctx, cfg)
assert(#vim.lsp.buf_get_clients() > 0, 'Must have a client running to use lsp_tags') local bufnr = vim.api.nvim_get_current_buf()
assert(next(vim.lsp.buf_get_clients(bufnr)), 'Must have a client running to use lsp_tags')
local results = call_hierarchy_handler_from(err, result, ctx, cfg, 'Incoming calls not found') local results = call_hierarchy_handler_from(err, result, ctx, cfg, 'Incoming calls not found')
@ -69,7 +70,8 @@ local function outgoing_calls_handler(bang, err, result, ctx, cfg)
end end
function M.incoming_calls(bang, opts) function M.incoming_calls(bang, opts)
assert(#vim.lsp.buf_get_clients() > 0, 'Must have a client running to use lsp_tags') local bufnr = vim.api.nvim_get_current_buf()
assert(next(vim.lsp.buf_get_clients(bufnr)), 'Must have a client running to use lsp_tags')
-- if not lsphelper.check_capabilities("call_hierarchy") then -- if not lsphelper.check_capabilities("call_hierarchy") then
-- return -- return
-- end -- end
@ -85,7 +87,8 @@ function M.incoming_calls(bang, opts)
end end
function M.outgoing_calls(bang, opts) function M.outgoing_calls(bang, opts)
assert(#vim.lsp.buf_get_clients() > 0, 'Must have a client running to use lsp_tags') local bufnr = vim.api.nvim_get_current_buf()
assert(next(vim.lsp.buf_get_clients(bufnr)), 'Must have a client running to use lsp_tags')
local params = vim.lsp.util.make_position_params() local params = vim.lsp.util.make_position_params()
params['levels'] = 2 params['levels'] = 2
params['callee'] = true params['callee'] = true

@ -92,7 +92,7 @@ end
M.lsp_clients = {} M.lsp_clients = {}
function M.refresh() function M.refresh()
if #vim.lsp.buf_get_clients() < 1 then if next(vim.lsp.buf_get_clients(0)) == nil then
log('Must have a client running to use lsp code action') log('Must have a client running to use lsp code action')
return return
end end
@ -130,7 +130,8 @@ M.inline = function()
if vim.fn.getcmdwintype() == ':' then if vim.fn.getcmdwintype() == ':' then
return return
end end
if #vim.lsp.buf_get_clients() == 0 then
if next(vim.lsp.buf_get_clients(0)) == nil then
return return
end end

@ -39,7 +39,7 @@ local function get_symbol()
end end
local function def_preview(timeout_ms) local function def_preview(timeout_ms)
assert(#vim.lsp.buf_get_clients() > 0, 'Must have a client running') assert(next(vim.lsp.buf_get_clients(0)), 'Must have a client running')
local method = 'textDocument/definition' local method = 'textDocument/definition'
local params = vim.lsp.util.make_position_params() local params = vim.lsp.util.make_position_params()
local result = vim.lsp.buf_request_sync(0, method, params, timeout_ms or 1000) local result = vim.lsp.buf_request_sync(0, method, params, timeout_ms or 1000)

@ -385,14 +385,16 @@ M.set_diag_loclist = function()
log('great, no errors!') log('great, no errors!')
return return
end end
local clients = vim.lsp.buf_get_clients(0)
local bufnr = vim.api.nvim_get_current_buf()
local clients = vim.lsp.buf_get_clients(bufnr)
local cfg = { open = diag_cnt > 0 } local cfg = { open = diag_cnt > 0 }
for _, client in pairs(clients) do for _, client in pairs(clients) do
cfg.client_id = client['id'] cfg.client_id = client['id']
break break
end end
if not vim.tbl_isempty(vim.lsp.buf_get_clients(0)) then if not vim.tbl_isempty(vim.lsp.buf_get_clients(bufnr)) then
local err_cnt = get_count(0, [[Error]]) local err_cnt = get_count(0, [[Error]])
if err_cnt > 0 and _NgConfigValues.lsp.disply_diagnostic_qf then if err_cnt > 0 and _NgConfigValues.lsp.disply_diagnostic_qf then
if diagnostic.set_loclist then if diagnostic.set_loclist then

@ -1,5 +1,5 @@
local log = require"navigator.util".log local log = require('navigator.util').log
local mk_handler = require"navigator.util".mk_handler local mk_handler = require('navigator.util').mk_handler
local lsp = vim.lsp local lsp = vim.lsp
local api = vim.api local api = vim.api
@ -18,7 +18,7 @@ M.servers_supporting_folding = {
texlab = true, texlab = true,
clangd = false, clangd = false,
gopls = true, gopls = true,
julials = false julials = false,
} }
M.active_folding_clients = {} M.active_folding_clients = {}
@ -29,11 +29,11 @@ function M.on_attach()
end end
function M.setup_plugin() function M.setup_plugin()
api.nvim_command("augroup FoldingCommand") api.nvim_command('augroup FoldingCommand')
api.nvim_command("autocmd! * <buffer>") api.nvim_command('autocmd! * <buffer>')
api.nvim_command("autocmd BufEnter <buffer> lua require'navigator.foldlsp'.update_folds()") api.nvim_command("autocmd BufEnter <buffer> lua require'navigator.foldlsp'.update_folds()")
api.nvim_command("autocmd BufWritePost <buffer> lua require'navigator.foldlsp'.update_folds()") api.nvim_command("autocmd BufWritePost <buffer> lua require'navigator.foldlsp'.update_folds()")
api.nvim_command("augroup end") api.nvim_command('augroup end')
-- vim.cmd([[ -- vim.cmd([[
-- --
@ -43,7 +43,7 @@ function M.setup_plugin()
-- --
-- ]]) -- ]])
local clients = vim.lsp.buf_get_clients() local clients = vim.lsp.buf_get_clients(0)
for _, client in pairs(clients) do for _, client in pairs(clients) do
local client_id = client['id'] local client_id = client['id']
@ -73,9 +73,8 @@ function M.update_folds()
-- XXX: better to pass callback in this method or add it directly in the config? -- XXX: better to pass callback in this method or add it directly in the config?
-- client.config.callbacks['textDocument/foldingRange'] = M.fold_handler -- client.config.callbacks['textDocument/foldingRange'] = M.fold_handler
local current_bufnr = api.nvim_get_current_buf() local current_bufnr = api.nvim_get_current_buf()
local params = {uri = vim.uri_from_bufnr(current_bufnr)} local params = { uri = vim.uri_from_bufnr(current_bufnr) }
client.request('textDocument/foldingRange', {textDocument = params}, M.fold_handler, client.request('textDocument/foldingRange', { textDocument = params }, M.fold_handler, current_bufnr)
current_bufnr)
end end
end end
end end
@ -93,7 +92,7 @@ M.fold_handler = mk_handler(function(err, result, ctx, config)
-- params: err, method, result, client_id, bufnr -- params: err, method, result, client_id, bufnr
-- XXX: handle err? -- XXX: handle err?
if err or result == nil or #result == 0 then if err or result == nil or #result == 0 then
vim.notify(string.format("%s %s ", tostring(err), vim.inspect(ctx)), vim.lsp.log_levels.WARN) vim.notify(string.format('%s %s ', tostring(err), vim.inspect(ctx)), vim.lsp.log_levels.WARN)
return return
end end
M.debug_folds() M.debug_folds()
@ -160,13 +159,12 @@ function M.get_fold_indic(lnum)
-- without any marker. -- without any marker.
return fold_level return fold_level
elseif is_foldstart then elseif is_foldstart then
return string.format(">%d", fold_level) return string.format('>%d', fold_level)
elseif is_foldend then elseif is_foldend then
return string.format("<%d", fold_level) return string.format('<%d', fold_level)
else else
return fold_level return fold_level
end end
end end
return M return M

@ -1,24 +1,26 @@
local gui = require "navigator.gui" local gui = require('navigator.gui')
local util = require "navigator.util" local util = require('navigator.util')
local log = util.log local log = util.log
local trace = util.trace local trace = util.trace
local partial = util.partial local partial = util.partial
local lsphelper = require "navigator.lspwrapper" local lsphelper = require('navigator.lspwrapper')
local path_sep = require"navigator.util".path_sep() local path_sep = require('navigator.util').path_sep()
local path_cur = require"navigator.util".path_cur() local path_cur = require('navigator.util').path_cur()
local cwd = vim.loop.cwd() local cwd = vim.loop.cwd()
local M = {} local M = {}
local function call_hierarchy_handler(direction, err, result, ctx, cfg, error_message) local function call_hierarchy_handler(direction, err, result, ctx, cfg, error_message)
if not result then if not result then
vim.notify ("No call hierarchy items found", vim.lsp.log_levels.WARN) vim.notify('No call hierarchy items found', vim.lsp.log_levels.WARN)
return return
end end
trace('call_hierarchy', result) trace('call_hierarchy', result)
assert(#vim.lsp.buf_get_clients() > 0, "Must have a client running to use lsp_tags")
local bufnr = vim.api.nvim_get_current_buf()
assert(next(vim.lsp.buf_get_clients(bufnr)), 'Must have a client running to use lsp_tags')
if err ~= nil then if err ~= nil then
log("dir", direction, "result", result, "err", err, ctx) log('dir', direction, 'result', result, 'err', err, ctx)
vim.notify("ERROR: " .. error_message, vim.lsp.log_levels.WARN) vim.notify('ERROR: ' .. error_message, vim.lsp.log_levels.WARN)
return return
end end
@ -28,14 +30,14 @@ local function call_hierarchy_handler(direction, err, result, ctx, cfg, error_me
local call_hierarchy_item = call_hierarchy_call[direction] local call_hierarchy_item = call_hierarchy_call[direction]
local kind = '' local kind = ''
if call_hierarchy_item.kind then if call_hierarchy_item.kind then
kind = require'navigator.lspclient.lspkind'.symbol_kind(call_hierarchy_item.kind) .. ' ' kind = require('navigator.lspclient.lspkind').symbol_kind(call_hierarchy_item.kind) .. ' '
end end
-- for _, range in pairs(call_hierarchy_call.fromRanges) do -- for _, range in pairs(call_hierarchy_call.fromRanges) do
range = call_hierarchy_item.range or call_hierarchy_item.selectionRange range = call_hierarchy_item.range or call_hierarchy_item.selectionRange
local filename = assert(vim.uri_to_fname(call_hierarchy_item.uri)) local filename = assert(vim.uri_to_fname(call_hierarchy_item.uri))
local display_filename = filename:gsub(cwd .. path_sep, path_cur, 1) local display_filename = filename:gsub(cwd .. path_sep, path_cur, 1)
call_hierarchy_item.detail = call_hierarchy_item.detail or "" call_hierarchy_item.detail = call_hierarchy_item.detail or ''
call_hierarchy_item.detail = call_hierarchy_item.detail:gsub("\n", "") call_hierarchy_item.detail = call_hierarchy_item.detail:gsub('\n', '')
trace(range, call_hierarchy_item) trace(range, call_hierarchy_item)
local disp_item = { local disp_item = {
@ -45,7 +47,7 @@ local function call_hierarchy_handler(direction, err, result, ctx, cfg, error_me
text = kind .. call_hierarchy_item.name .. '' .. call_hierarchy_item.detail, text = kind .. call_hierarchy_item.name .. '' .. call_hierarchy_item.detail,
range = range, range = range,
lnum = range.start.line + 1, lnum = range.start.line + 1,
col = range.start.character col = range.start.character,
} }
table.insert(items, disp_item) table.insert(items, disp_item)
@ -54,43 +56,46 @@ local function call_hierarchy_handler(direction, err, result, ctx, cfg, error_me
return items return items
end end
local call_hierarchy_handler_from = partial(call_hierarchy_handler, "from") local call_hierarchy_handler_from = partial(call_hierarchy_handler, 'from')
local call_hierarchy_handler_to = partial(call_hierarchy_handler, "to") local call_hierarchy_handler_to = partial(call_hierarchy_handler, 'to')
local function incoming_calls_handler(bang, err, result, ctx, cfg) local function incoming_calls_handler(bang, err, result, ctx, cfg)
assert(#vim.lsp.buf_get_clients() > 0, "Must have a client running to use lsp hierarchy") local bufnr = vim.api.nvim_get_current_buf()
local results = call_hierarchy_handler_from(err, result, ctx, cfg, "Incoming calls not found") assert(next(vim.lsp.buf_get_clients(bufnr)), 'Must have a client running to use lsp hierarchy')
local results = call_hierarchy_handler_from(err, result, ctx, cfg, 'Incoming calls not found')
local ft = vim.api.nvim_buf_get_option(ctx.bufnr, "ft") local ft = vim.api.nvim_buf_get_option(ctx.bufnr, 'ft')
gui.new_list_view({items = results, ft = ft, api = ''}) gui.new_list_view({ items = results, ft = ft, api = '' })
end end
local function outgoing_calls_handler(bang, err, result, ctx, cfg) local function outgoing_calls_handler(bang, err, result, ctx, cfg)
local results = call_hierarchy_handler_to(err, result, ctx, cfg, "Outgoing calls not found") local results = call_hierarchy_handler_to(err, result, ctx, cfg, 'Outgoing calls not found')
local ft = vim.api.nvim_buf_get_option(ctx.bufnr, "ft") local ft = vim.api.nvim_buf_get_option(ctx.bufnr, 'ft')
gui.new_list_view({items = results, ft = ft, api = ''}) gui.new_list_view({ items = results, ft = ft, api = '' })
-- fzf_locations(bang, "", "Outgoing Calls", results, false) -- fzf_locations(bang, "", "Outgoing Calls", results, false)
end end
function M.incoming_calls(bang, opts) function M.incoming_calls(bang, opts)
assert(#vim.lsp.buf_get_clients() > 0, "Must have a client running to use lsp hierarchy") local bufnr = vim.api.nvim_get_current_buf()
if not lsphelper.check_capabilities("call_hierarchy") then assert(next(vim.lsp.buf_get_clients(bufnr)), 'Must have a client running to use lsp hierarchy')
if not lsphelper.check_capabilities('call_hierarchy') then
return return
end end
local params = vim.lsp.util.make_position_params() local params = vim.lsp.util.make_position_params()
lsphelper.call_sync("callHierarchy/incomingCalls", params, opts, partial(incoming_calls_handler, bang)) lsphelper.call_sync('callHierarchy/incomingCalls', params, opts, partial(incoming_calls_handler, bang))
end end
function M.outgoing_calls(bang, opts) function M.outgoing_calls(bang, opts)
assert(#vim.lsp.buf_get_clients() > 0, "Must have a client running to use lsp_tags") local bufnr = vim.api.nvim_get_current_buf()
if not lsphelper.check_capabilities("call_hierarchy") then assert(next(vim.lsp.buf_get_clients(bufnr)), 'Must have a client running to use lsp_tags')
if not lsphelper.check_capabilities('call_hierarchy') then
return return
end end
local params = vim.lsp.util.make_position_params() local params = vim.lsp.util.make_position_params()
lsphelper.call_sync("callHierarchy/outgoingCalls", params, opts, partial(outgoing_calls_handler, bang)) lsphelper.call_sync('callHierarchy/outgoingCalls', params, opts, partial(outgoing_calls_handler, bang))
end end
M.incoming_calls_call = partial(M.incoming_calls, 0) M.incoming_calls_call = partial(M.incoming_calls, 0)

Loading…
Cancel
Save