chore(mappings): POC for help and :help on_attach keymaps
This commit is contained in:
@@ -2,7 +2,8 @@ local Api = require "nvim-tree.api"
|
||||
|
||||
local M = {}
|
||||
|
||||
M.DEFAULT_KEYMAPS = {
|
||||
-- BEGIN_DEFAULT_KEYMAPS
|
||||
local DEFAULT_KEYMAPS = {
|
||||
{
|
||||
key = { "<CR>", "o", "<2-LeftMouse>" },
|
||||
callback = Api.node.open.edit,
|
||||
@@ -372,6 +373,7 @@ M.DEFAULT_KEYMAPS = {
|
||||
},
|
||||
},
|
||||
}
|
||||
-- END_DEFAULT_KEYMAPS
|
||||
|
||||
function M.set_keymaps(bufnr)
|
||||
local opts = { noremap = true, silent = true, nowait = true, buffer = bufnr }
|
||||
@@ -385,7 +387,7 @@ end
|
||||
|
||||
local function filter_default_mappings(keys_to_disable)
|
||||
local new_map = {}
|
||||
for _, m in pairs(M.DEFAULT_KEYMAPS) do
|
||||
for _, m in pairs(DEFAULT_KEYMAPS) do
|
||||
local keys = type(m.key) == "table" and m.key or { m.key }
|
||||
local reminding_keys = {}
|
||||
for _, key in pairs(keys) do
|
||||
@@ -418,11 +420,13 @@ local function get_keymaps(keys_to_disable)
|
||||
return filter_default_mappings(keys_to_disable)
|
||||
end
|
||||
|
||||
return M.DEFAULT_KEYMAPS
|
||||
return DEFAULT_KEYMAPS
|
||||
end
|
||||
|
||||
function M.setup(opts)
|
||||
M.keymaps = get_keymaps(opts.remove_keymaps)
|
||||
end
|
||||
|
||||
M.DEFAULT_KEYMAPS = DEFAULT_KEYMAPS
|
||||
|
||||
return M
|
||||
|
||||
54
scripts/generate_default_keymaps.lua
Normal file
54
scripts/generate_default_keymaps.lua
Normal file
@@ -0,0 +1,54 @@
|
||||
-- luacheck:ignore 113
|
||||
---@diagnostic disable: undefined-global
|
||||
|
||||
-- write DEFAULT_KEYMAPS in various formats
|
||||
|
||||
local max_key_help = 0
|
||||
local max_short_help = 0
|
||||
local outs_help = {}
|
||||
|
||||
for _, m in pairs(DEFAULT_KEYMAPS) do
|
||||
local first = true
|
||||
local keys = type(m.key) == "table" and m.key or { m.key }
|
||||
for _, key in ipairs(keys) do
|
||||
local out = {}
|
||||
out.key = key
|
||||
max_key_help = math.max(#out.key, max_key_help)
|
||||
if first then
|
||||
out.short = m.desc.short
|
||||
max_short_help = math.max(#out.short, max_short_help)
|
||||
out.long = m.desc.long
|
||||
first = false
|
||||
end
|
||||
table.insert(outs_help, out)
|
||||
end
|
||||
end
|
||||
|
||||
-- help
|
||||
local file = io.open("/tmp/DEFAULT_KEYMAPS.help", "w")
|
||||
io.output(file)
|
||||
io.write "\n"
|
||||
local fmt = string.format("%%-%d.%ds %%-%d.%ds %%s\n", max_key_help, max_key_help, max_short_help, max_short_help)
|
||||
for _, m in pairs(outs_help) do
|
||||
if not m.short then
|
||||
io.write(string.format("%s\n", m.key))
|
||||
else
|
||||
io.write(string.format(fmt, m.key, m.short, m.long))
|
||||
end
|
||||
end
|
||||
io.write "\n"
|
||||
io.close(file)
|
||||
|
||||
-- lua on_attach
|
||||
file = io.open("/tmp/DEFAULT_KEYMAPS.on_attach.lua", "w")
|
||||
io.output(file)
|
||||
io.write "local function on_attach(bufnr, mode, opts)\n"
|
||||
io.write "local Api = require('nvim-tree.api')\n"
|
||||
for _, m in pairs(DEFAULT_KEYMAPS) do
|
||||
local keys = type(m.key) == "table" and m.key or { m.key }
|
||||
for _, key in ipairs(keys) do
|
||||
io.write(string.format(" vim.keymap.set(mode, '%s', %s, opts)\n", key, m.callback))
|
||||
end
|
||||
end
|
||||
io.write "end\n"
|
||||
io.close(file)
|
||||
@@ -31,3 +31,10 @@ sed -i -e "/${begin}/,/${end}/{ /${begin}/{p; r /tmp/DEFAULT_MAPPINGS.lua
|
||||
}; /${end}/p; d }" doc/nvim-tree-lua.txt
|
||||
sed -i -e "/^DEFAULT MAPPINGS/,/^>$/{ /^DEFAULT MAPPINGS/{p; r /tmp/DEFAULT_MAPPINGS.help
|
||||
}; /^>$/p; d }" doc/nvim-tree-lua.txt
|
||||
|
||||
# generate various DEFAULT_KEYMAPS
|
||||
begin="BEGIN_DEFAULT_KEYMAPS"
|
||||
end="END_DEFAULT_KEYMAPS"
|
||||
sed -n -e "/${begin}/,/${end}/{ /${begin}/d; /${end}/d; s/callback = \(.*\),/callback = '\1',/g; p; }" lua/nvim-tree/keymap.lua > /tmp/DEFAULT_KEYMAPS.M.lua
|
||||
cat /tmp/DEFAULT_KEYMAPS.M.lua scripts/generate_default_keymaps.lua | lua
|
||||
|
||||
|
||||
Reference in New Issue
Block a user