diff --git a/README.md b/README.md index fb092fa..cae5ab3 100644 --- a/README.md +++ b/README.md @@ -618,8 +618,8 @@ require'navigator'.setup({ require('rust-tools').setup({ server = { - on_attach = function(_, _) - require('navigator.lspclient.mapping').setup() -- setup navigator keymaps here, + on_attach = function(client, bufnr) + require('navigator.lspclient.mapping').setup({client=client, bufnr=bufnr}) -- setup navigator keymaps here, -- otherwise, you can define your own commands to call navigator functions end, } @@ -627,8 +627,8 @@ require('rust-tools').setup({ require("clangd_extensions").setup { server = { - on_attach = function(_, _) - require('navigator.lspclient.mapping').setup() -- setup navigator keymaps here, + on_attach = function(client, bufnr) + require('navigator.lspclient.mapping').setup({client=client, bufnr=bufnr}) -- setup navigator keymaps here, -- otherwise, you can define your own commands to call navigator functions end, } diff --git a/lua/navigator/lspclient/mapping.lua b/lua/navigator/lspclient/mapping.lua index 51240cd..482f62f 100644 --- a/lua/navigator/lspclient/mapping.lua +++ b/lua/navigator/lspclient/mapping.lua @@ -1,7 +1,6 @@ local util = require('navigator.util') local log = util.log local trace = util.trace - local event_hdlrs = { { ev = 'BufWritePre', func = [[require "navigator.diagnostics".set_diag_loclist()]] }, { ev = 'CursorHold', func = 'document_highlight()' }, @@ -122,12 +121,17 @@ local function set_cmds(_) end end -local function set_mapping(lsp_info) +-- should works for both 1)attach from known lsp client or from a disabled lsp client +local function set_mapping(lsp_attach_info) local opts = { noremap = true, silent = true } - lsp_info = lsp_info or {} - log('setup mapping', lsp_info.client.name, lsp_info.client.cmd) + vim.validate({ + lsp_attach_info = { lsp_attach_info, 'table' }, + }) + if _NgConfigValues.debug then + log('setup mapping for client', lsp_attach_info.client.name, lsp_attach_info.client.cmd) + end local user_key = _NgConfigValues.keymaps or {} - local bufnr = lsp_info.bufnr or 0 + local bufnr = lsp_attach_info.bufnr or 0 local function del_keymap(...) vim.api.nvim_buf_del_keymap(bufnr, ...) @@ -139,7 +143,7 @@ local function set_mapping(lsp_info) -- local function buf_set_option(...) -- vim.api.nvim_buf_set_option(bufnr, ...) -- end - local doc_fmt, range_fmt, ccls = check_cap(lsp_info) + local doc_fmt, range_fmt, ccls = check_cap(lsp_attach_info) if ccls then vim.list_extend(key_maps, ccls_mappings) @@ -203,8 +207,8 @@ local function set_mapping(lsp_info) del_keymap('n', fmtkey) end - if lsp_info.cap and lsp_info.cap.document_range_formatting then - log('formatting enabled', lsp_info.cap) + if lsp_attach_info.cap and lsp_attach_info.cap.document_range_formatting then + log('formatting enabled', lsp_attach_info.cap) end if not range_fmt and rfmtkey then @@ -271,15 +275,21 @@ M.toggle_lspformat = function(on) end end -function M.setup(user_opts) - user_opts = user_opts or _NgConfigValues - set_mapping(user_opts) - set_cmds(user_opts) +function M.setup(attach_opts) + if not attach_opts or not attach_opts.client then + vim.notify( + 'please call require"navigator.mapping".setup({bufnr=bufnr, client=client}) inside on_attach(client,bufnr)', + vim.lsp.log_levels.WARN + ) + end + attach_opts = attach_opts or { bufnr = 0, client = {}, cap = {} } + set_mapping(attach_opts) + set_cmds(attach_opts) autocmd() - set_event_handler(user_opts) + set_event_handler(attach_opts) - local client = user_opts.client or {} + local client = attach_opts.client or {} local cap = client.server_capabilities or vim.lsp.protocol.make_client_capabilities() log('lsp cap:', cap.codeActionProvider) diff --git a/playground/init.lua b/playground/init.lua index 0785b5b..e73ce97 100644 --- a/playground/init.lua +++ b/playground/init.lua @@ -42,7 +42,7 @@ local function load_plugins() use({ 'ray-x/aurora' }) use({ 'ray-x/navigator.lua', - -- '~/github/navigator.lua', + -- '~/github/ray-x/navigator.lua', requires = { 'ray-x/guihua.lua', run = 'cd lua/fzy && make' }, config = function() require('navigator').setup({