[Breaking] using vim.keymap.set, breaking changes (#200)

* using vim.keymap.set, breaking changes

* update keymaps

* doc updates
bugfix171
rayx 2 years ago committed by GitHub
parent 792fd2831a
commit acca6009e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -257,8 +257,8 @@ require'navigator'.setup({
-- end, -- end,
-- The attach code will apply to all LSP clients -- The attach code will apply to all LSP clients
default_mapping = true, -- set to false if you will remap every key default_mapping = true, -- set to false if you will remap every key or if you using old version of nvim-
keymaps = {{key = "gK", func = "declaration()"}}, -- a list of key maps keymaps = {{key = "gK", func = vim.lsp.declaration, doc = 'declaration'}}, -- a list of key maps
-- this kepmap gK will override "gD" mapping function declaration() in default kepmap -- this kepmap gK will override "gD" mapping function declaration() in default kepmap
-- please check mapping.lua for all keymaps -- please check mapping.lua for all keymaps
treesitter_analysis = true, -- treesitter variable context treesitter_analysis = true, -- treesitter variable context
@ -271,7 +271,7 @@ require'navigator'.setup({
signature_help_cfg = nil, -- if you would like to init ray-x/lsp_signature plugin in navigator, and pass in your own config to signature help signature_help_cfg = nil, -- if you would like to init ray-x/lsp_signature plugin in navigator, and pass in your own config to signature help
icons = { icons = {
-- Code action -- Code action
code_action_icon = "🏏", code_action_icon = "🏏", -- note: need terminal support, for those not support unicode, might crash
-- Diagnostics -- Diagnostics
diagnostic_head = '🐛', diagnostic_head = '🐛',
diagnostic_head_severity_1 = "🈲", diagnostic_head_severity_1 = "🈲",

@ -5,7 +5,9 @@ local function warn(msg)
end end
local function info(msg) local function info(msg)
vim.api.nvim_echo({ { 'Info: ' .. msg } }, true, {}) if _NgConfigValues.debug then
vim.api.nvim_echo({ { 'Info: ' .. msg } }, true, {})
end
end end
_NgConfigValues = { _NgConfigValues = {
@ -16,7 +18,7 @@ _NgConfigValues = {
preview_lines = 40, -- total lines in preview screen preview_lines = 40, -- total lines in preview screen
preview_lines_before = 5, -- lines before the highlight line preview_lines_before = 5, -- lines before the highlight line
default_mapping = true, default_mapping = true,
keymaps = {}, -- e.g keymaps={{key = "GR", func = "references()"}, } this replace gr default mapping keymaps = {}, -- e.g keymaps={{key = "GR", func = vim.lsp.buf.references}, } this replace gr default mapping
external = nil, -- true: enable for goneovim multigrid otherwise false external = nil, -- true: enable for goneovim multigrid otherwise false
border = 'single', -- border style, can be one of 'none', 'single', 'double', "shadow" border = 'single', -- border style, can be one of 'none', 'single', 'double', "shadow"
@ -204,7 +206,7 @@ local extend_config = function(opts)
info(string.format('[] extend LSP support for %s %s ', key, k)) info(string.format('[] extend LSP support for %s %s ', key, k))
end end
elseif key == 'keymaps' then elseif key == 'keymaps' then
info('keymap override') info('keymap override', v)
-- skip key check and allow mapping to handle that -- skip key check and allow mapping to handle that
else else
warn(string.format('[] Key %s %s not valid', key, k)) warn(string.format('[] Key %s %s not valid', key, k))

@ -31,4 +31,16 @@ return {
end end
end, 100) end, 100)
end, end,
range_foramt = function(err, result, ctx, _)
local old_func = vim.go.operatorfunc
_G.op_func_formatting = function()
local start = vim.api.nvim_buf_get_mark(0, '[')
local finish = vim.api.nvim_buf_get_mark(0, ']')
vim.lsp.buf.range_formatting({}, start, finish)
vim.go.operatorfunc = old_func
_G.op_func_formatting = nil
end
vim.go.operatorfunc = 'v:lua.op_func_formatting'
vim.api.nvim_feedkeys('g@', 'n', false)
end,
} }

@ -1,11 +1,12 @@
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 api = vim.api
local event_hdlrs = { local event_hdlrs = {
{ ev = 'BufWritePre', func = [[require "navigator.diagnostics".set_diag_loclist()]] }, { ev = 'BufWritePre', func = require('navigator.diagnostics').set_diag_loclist },
{ ev = 'CursorHold', func = 'document_highlight()' }, { ev = { 'CursorHold', 'CursorHoldI' }, func = vim.lsp.buf.document_highlight },
{ ev = 'CursorHoldI', func = 'document_highlight()' }, { ev = 'CursorMoved', func = vim.lsp.buf.clear_references },
{ ev = 'CursorMoved', func = 'clear_references()' },
} }
if vim.lsp.buf.format == nil then if vim.lsp.buf.format == nil then
@ -20,45 +21,55 @@ local single = { '╭', '─', '╮', '│', '╯', '─', '╰', '│' }
-- TODO https://github.com/neovim/neovim/pull/16591 use vimkeymap.set/del -- TODO https://github.com/neovim/neovim/pull/16591 use vimkeymap.set/del
-- LuaFormatter off -- LuaFormatter off
local key_maps = { local key_maps = {
{ key = 'gr', func = "require('navigator.reference').async_ref()" }, { key = 'gr', func = require('navigator.reference').async_ref, doc = 'async_ref' },
{ key = '<Leader>gr', func = "require('navigator.reference').reference()" }, -- reference deprecated { key = '<Leader>gr', func = require('navigator.reference').reference, doc = 'reference' }, -- reference deprecated
{ mode = 'i', key = '<M-k>', func = 'signature_help()' }, { mode = 'i', key = '<M-k>', func = vim.lsp.signature_help, doc = 'signature_help' },
{ key = '<c-k>', func = 'signature_help()' }, { key = '<c-k>', func = vim.lsp.buf.signature_help, doc = 'signature_help' },
{ key = 'g0', func = "require('navigator.symbols').document_symbols()" }, { key = 'g0', func = require('navigator.symbols').document_symbols, doc = 'document_symbols' },
{ key = 'gW', func = "require('navigator.workspace').workspace_symbol_live()" }, { key = 'gW', func = require('navigator.workspace').workspace_symbol_live, doc = 'workspace_symbol_live' },
{ key = '<c-]>', func = "require('navigator.definition').definition()" }, { key = '<c-]>', func = require('navigator.definition').definition, doc = 'definition' },
{ key = 'gd', func = "require('navigator.definition').definition()" }, { key = 'gd', func = require('navigator.definition').definition, doc = 'definition' },
{ key = 'gD', func = "declaration({ border = 'rounded', max_width = 80 })" }, { key = 'gD', func = vim.lsp.buf.declaration, doc = 'declaration' },
{ key = 'gp', func = "require('navigator.definition').definition_preview()" }, { key = 'gp', func = require('navigator.definition').definition_preview, doc = 'definition_preview' },
{ key = '<Leader>gt', func = "require('navigator.treesitter').buf_ts()" }, { key = '<Leader>gt', func = require('navigator.treesitter').buf_ts, doc = 'buf_ts' },
{ key = '<Leader>gT', func = "require('navigator.treesitter').bufs_ts()" }, { key = '<Leader>gT', func = require('navigator.treesitter').bufs_ts, doc = 'bufs_ts' },
{ key = '<Leader>ct', func = "require('navigator.ctags').ctags()" }, { key = '<Leader>ct', func = require('navigator.ctags').ctags, doc = 'ctags' },
{ key = 'K', func = 'hover({ popup_opts = { border = single, max_width = 80 }})' }, { key = 'K', func = vim.lsp.hover, doc = 'hover' },
{ key = '<Space>ca', mode = 'n', func = "require('navigator.codeAction').code_action()" }, { key = '<Space>ca', mode = 'n', func = require('navigator.codeAction').code_action, doc = 'code_action' },
{ key = '<Space>ca', mode = 'v', func = "require('navigator.codeAction').range_code_action()" }, {
key = '<Space>ca',
mode = 'v',
func = require('navigator.codeAction').range_code_action,
doc = 'range_code_action',
},
-- { key = '<Leader>re', func = 'rename()' }, -- { key = '<Leader>re', func = 'rename()' },
{ key = '<Space>rn', func = "require('navigator.rename').rename()" }, { key = '<Space>rn', func = require('navigator.rename').rename, doc = 'rename' },
{ key = '<Leader>gi', func = 'incoming_calls()' }, { key = '<Leader>gi', func = vim.lsp.buf.incoming_calls, doc = 'incoming_calls' },
{ key = '<Leader>go', func = 'outgoing_calls()' }, { key = '<Leader>go', func = vim.lsp.buf.outgoing_calls, doc = 'outgoing_calls' },
{ key = 'gi', func = 'implementation()' }, { key = 'gi', func = vim.lsp.buf.implementation, doc = 'implementation' },
{ key = '<Space>D', func = 'type_definition()' }, { key = '<Space>D', func = vim.lsp.buf.type_definition, doc = 'type_definition' },
{ key = 'gL', func = "require('navigator.diagnostics').show_diagnostics()" }, { key = 'gL', func = require('navigator.diagnostics').show_diagnostics, doc = 'show_diagnostics' },
{ key = 'gG', func = "require('navigator.diagnostics').show_buf_diagnostics()" }, { key = 'gG', func = require('navigator.diagnostics').show_buf_diagnostics, doc = 'show_buf_diagnostics' },
{ key = '<Leader>dt', func = "require('navigator.diagnostics').toggle_diagnostics()" }, { key = '<Leader>dt', func = require('navigator.diagnostics').toggle_diagnostics, doc = 'toggle_diagnostics' },
{ key = ']d', func = "diagnostic.goto_next({ border = 'rounded', max_width = 80})" }, { key = ']d', func = vim.diagnostic.goto_next, doc = 'next diagnostics' },
{ key = '[d', func = "diagnostic.goto_prev({ border = 'rounded', max_width = 80})" }, { key = '[d', func = vim.diagnostic.goto_prev, doc = 'prev diagnostics' },
{ key = ']O', func = 'diagnostic.set_loclist()' }, { key = ']O', func = vim.diagnostic.set_loclist, doc = 'diagnostics set loclist' },
{ key = ']r', func = "require('navigator.treesitter').goto_next_usage()" }, { key = ']r', func = require('navigator.treesitter').goto_next_usage, doc = 'goto_next_usage' },
{ key = '[r', func = "require('navigator.treesitter').goto_previous_usage()" }, { key = '[r', func = require('navigator.treesitter').goto_previous_usage, doc = 'goto_previous_usage' },
{ key = '<C-LeftMouse>', func = 'definition()' }, { key = '<C-LeftMouse>', func = vim.lsp.buf.definition, doc = 'definition' },
{ key = 'g<LeftMouse>', func = 'implementation()' }, { key = 'g<LeftMouse>', func = vim.lsp.buf.implementation, doc = 'implementation' },
{ key = '<Leader>k', func = "require('navigator.dochighlight').hi_symbol()" }, { key = '<Leader>k', func = require('navigator.dochighlight').hi_symbol, doc = 'hi_symbol' },
{ key = '<Space>wa', func = "require('navigator.workspace').add_workspace_folder()" }, { key = '<Space>wa', func = require('navigator.workspace').add_workspace_folder, doc = 'add_workspace_folder' },
{ key = '<Space>wr', func = "require('navigator.workspace').remove_workspace_folder()" }, {
{ key = '<Space>ff', func = 'format({async = true})', mode = 'n' }, key = '<Space>wr',
{ key = '<Space>ff', func = 'range_formatting()', mode = 'v' }, func = require('navigator.workspace').remove_workspace_folder,
{ key = '<Space>wl', func = "require('navigator.workspace').list_workspace_folders()" }, doc = 'remove_workspace_folder',
{ key = '<Space>la', mode = 'n', func = "require('navigator.codelens').run_action()" }, },
{ key = '<Space>ff', func = vim.lsp.buf.format, mode = 'n', doc = 'format' },
{ key = '<Space>ff', func = vim.lsp.buf.range_formatting, mode = 'v', doc = 'range format' },
{ key = '<Space>rf', func = require('navigator.formatting').range_format, mode = 'n', doc = 'range_fmt_v' },
{ key = '<Space>wl', func = require('navigator.workspace').list_workspace_folders, doc = 'list_workspace_folders' },
{ key = '<Space>la', mode = 'n', func = require('navigator.codelens').run_action, doc = 'run code lens action' },
} }
local commands = { local commands = {
@ -77,8 +88,8 @@ local key_maps_help = {}
local M = {} local M = {}
local ccls_mappings = { local ccls_mappings = {
{ key = '<Leader>gi', func = "require('navigator.cclshierarchy').incoming_calls()" }, { key = '<Leader>gi', func = require('navigator.cclshierarchy').incoming_calls, doc = 'incoming_calls' },
{ key = '<Leader>go', func = "require('navigator.cclshierarchy').outgoing_calls()" }, { key = '<Leader>go', func = require('navigator.cclshierarchy').outgoing_calls, doc = 'outgoing_calls' },
} }
local check_cap = function(opts) local check_cap = function(opts)
@ -133,13 +144,17 @@ local function set_mapping(lsp_attach_info)
local user_key = _NgConfigValues.keymaps or {} local user_key = _NgConfigValues.keymaps or {}
local bufnr = lsp_attach_info.bufnr or 0 local bufnr = lsp_attach_info.bufnr or 0
local function del_keymap(...) local function del_keymap(mode, key, ...)
vim.api.nvim_buf_del_keymap(bufnr, ...) local ks = vim.api.nvim_buf_get_keymap(bufnr, mode)
if vim.tbl_contains(ks, key) then
vim.api.nvim_buf_del_keymap(bufnr, mode, key, ...)
end
end end
local function set_keymap(...) local function set_keymap(...)
vim.api.nvim_buf_set_keymap(bufnr, ...) vim.api.nvim_buf_set_keymap(bufnr, ...)
end end
-- local function buf_set_option(...) -- local function buf_set_option(...)
-- vim.api.nvim_buf_set_option(bufnr, ...) -- vim.api.nvim_buf_set_option(bufnr, ...)
-- end -- end
@ -169,40 +184,66 @@ local function set_mapping(lsp_attach_info)
end end
local fmtkey, rfmtkey local fmtkey, rfmtkey
for _, value in pairs(key_maps) do for _, value in pairs(key_maps) do
local f = '<Cmd>lua vim.lsp.buf.' .. value.func .. '<CR>' if type(value.func) == 'string' then -- deprecated will remove when 0.8 is out
if string.find(value.func, 'require') or string.find(value.func, 'vim.') then vim.notify('keymap config updated: ' .. value.key .. ' func ' .. value.func .. ' should be a function')
f = '<Cmd>lua ' .. value.func .. '<CR>' local f = '<Cmd>lua vim.lsp.buf.' .. value.func .. '<CR>'
elseif string.find(value.func, 'diagnostic') then if string.find(value.func, 'require') or string.find(value.func, 'vim.') then
local diagnostic = '<Cmd>lua vim.' f = '<Cmd>lua ' .. value.func .. '<CR>'
diagnostic = '<Cmd>lua vim.' elseif string.find(value.func, 'diagnostic') then
f = diagnostic .. value.func .. '<CR>' local diagnostic = '<Cmd>lua vim.'
-- elseif string.find(value.func, 'vim.') then diagnostic = '<Cmd>lua vim.'
-- f = '<Cmd>lua ' .. value.func .. '<string.find(value.func, 'vim.')CR>' f = diagnostic .. value.func .. '<CR>'
end
local k = value.key
local m = value.mode or 'n'
if string.find(value.func, 'range_formatting') then
rfmtkey = value.key
elseif string.find(value.func, 'format') then
fmtkey = value.key
end
trace('binding', k, f)
set_keymap(m, k, f, opts)
end end
local k = value.key if type(value.func) == 'function' then -- new from 0.7.x
local m = value.mode or 'n' -- neovim 0.7.0
if string.find(value.func, 'range_formatting') then
rfmtkey = value.key opts.buffer = key_maps.buffer or value.buffer
elseif string.find(value.func, 'format') then vim.keymap.set(value.mode or 'n', value.key, value.func, opts)
fmtkey = value.key if string.find(value.doc, 'range format') then
rfmtkey = value.key
elseif string.find(value.doc, 'format') then
fmtkey = value.key
end
end end
trace('binding', k, f)
set_keymap(m, k, f, opts)
end end
for _, val in pairs(key_maps) do for _, val in pairs(key_maps) do
table.insert(key_maps_help, (val.mode or 'n') .. '|' .. val.key .. '|' .. val.func) local helper_msg = ''
if val.doc then
helper_msg = val.doc
elseif type(val.func) == 'string' then
helper_msg = val.func
end
local item = (val.mode or 'n') .. '|' .. val.key .. '|' .. helper_msg
if not vim.tbl_contains(key_maps_help, item) then
table.insert(key_maps_help, (val.mode or 'n') .. '|' .. val.key .. '|' .. helper_msg)
end
end end
-- if user_opts.cap.document_formatting then -- if user_opts.cap.document_formatting then
if doc_fmt and _NgConfigValues.lsp.format_on_save then if doc_fmt and _NgConfigValues.lsp.format_on_save then
vim.cmd([[ local gn = api.nvim_create_augroup('NavAuGroupFormat', {})
aug NavigatorAuFormat
au! api.nvim_create_autocmd({ 'BufWritePre' }, {
autocmd BufWritePre <buffer> lua vim.lsp.buf.format({async = true}) group = gn,
aug END buffer = bufnr,
]]) callback = function()
vim.lsp.buf.format({ async = true })
end,
})
elseif fmtkey then elseif fmtkey then
del_keymap('n', fmtkey) del_keymap('n', fmtkey)
end end
@ -219,41 +260,48 @@ local function set_mapping(lsp_attach_info)
end end
local function autocmd() local function autocmd()
vim.api.nvim_exec( local gn = api.nvim_create_augroup('NavAuGroupDocHlAu', {})
[[
aug NavigatorDocHlAu api.nvim_create_autocmd({ 'BufWritePre' }, {
au! group = gn,
au CmdlineLeave : lua require('navigator.dochighlight').cmd_nohl() callback = require('navigator.dochighlight').cmd_nohl,
aug END })
]],
false
)
end end
local function set_event_handler(user_opts) local function set_event_handler(user_opts)
user_opts = user_opts or {} user_opts = user_opts or {}
local file_types = local file_types = {
'c,cpp,h,go,python,vim,sh,javascript,html,css,lua,typescript,rust,javascriptreact,typescriptreact,kotlin,php,dart,nim,java' '*.c',
'*.cpp',
'*.h',
'*.go',
'*.python',
'*.vim',
'*.sh',
'*.javascript',
'*.html',
'*.css',
'*.lua',
'*.typescript',
'*.rust',
'*.javascriptreact',
'*.typescriptreact',
'*.kotlin',
'*.php',
'*.dart',
'*.nim',
'*.java',
}
-- local format_files = "c,cpp,h,go,python,vim,javascript,typescript" --html,css, -- local format_files = "c,cpp,h,go,python,vim,javascript,typescript" --html,css,
vim.api.nvim_command([[augroup nvim_nv_lsp_autos]])
vim.api.nvim_command([[autocmd!]])
local gn = api.nvim_create_augroup('nvim_nv_event_autos', {})
for _, value in pairs(event_hdlrs) do for _, value in pairs(event_hdlrs) do
local f = '' api.nvim_create_autocmd(value.ev, {
if string.find(value.func, 'require') ~= nil then group = gn,
f = 'lua ' .. value.func pattern = file_types,
else callback = value.func,
f = 'lua vim.lsp.buf.' .. value.func })
end
local cmd = 'autocmd FileType '
.. file_types
.. ' autocmd nvim_nv_lsp_autos '
.. value.ev
.. ' <buffer> silent! '
.. f
vim.api.nvim_command(cmd)
end end
vim.api.nvim_command([[augroup END]])
end end
M.toggle_lspformat = function(on) M.toggle_lspformat = function(on)
@ -348,7 +396,7 @@ M.get_keymaps_help = function()
border = 'none', border = 'none',
prompt = true, prompt = true,
enter = true, enter = true,
rect = { height = 20, width = 90 }, rect = { height = 24, width = 50 },
data = key_maps_help, data = key_maps_help,
}) })

@ -47,6 +47,8 @@ local function load_plugins()
config = function() config = function()
require('navigator').setup({ require('navigator').setup({
lsp_signature_help = true, lsp_signature_help = true,
debug = true,
keymaps = { { key = 'gK', func = vim.lsp.buf.definition, doc = 'definition' } },
}) })
end, end,
}) })

Loading…
Cancel
Save