Skip to content

Commit de53d64

Browse files
committed
chore(mappings): POC for help and :help on_attach keymaps
1 parent 2df2710 commit de53d64

File tree

3 files changed

+68
-3
lines changed

3 files changed

+68
-3
lines changed

lua/nvim-tree/keymap.lua

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ local Api = require "nvim-tree.api"
22

33
local M = {}
44

5-
M.DEFAULT_KEYMAPS = {
5+
-- BEGIN_DEFAULT_KEYMAPS
6+
local DEFAULT_KEYMAPS = {
67
{
78
key = { "<CR>", "o", "<2-LeftMouse>" },
89
callback = Api.node.open.edit,
@@ -372,6 +373,7 @@ M.DEFAULT_KEYMAPS = {
372373
},
373374
},
374375
}
376+
-- END_DEFAULT_KEYMAPS
375377

376378
function M.set_keymaps(bufnr)
377379
local opts = { noremap = true, silent = true, nowait = true, buffer = bufnr }
@@ -385,7 +387,7 @@ end
385387

386388
local function filter_default_mappings(keys_to_disable)
387389
local new_map = {}
388-
for _, m in pairs(M.DEFAULT_KEYMAPS) do
390+
for _, m in pairs(DEFAULT_KEYMAPS) do
389391
local keys = type(m.key) == "table" and m.key or { m.key }
390392
local reminding_keys = {}
391393
for _, key in pairs(keys) do
@@ -418,11 +420,13 @@ local function get_keymaps(keys_to_disable)
418420
return filter_default_mappings(keys_to_disable)
419421
end
420422

421-
return M.DEFAULT_KEYMAPS
423+
return DEFAULT_KEYMAPS
422424
end
423425

424426
function M.setup(opts)
425427
M.keymaps = get_keymaps(opts.remove_keymaps)
426428
end
427429

430+
M.DEFAULT_KEYMAPS = DEFAULT_KEYMAPS
431+
428432
return M

scripts/generate_default_keymaps.lua

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
-- luacheck:ignore 113
2+
---@diagnostic disable: undefined-global
3+
4+
-- write DEFAULT_KEYMAPS in various formats
5+
6+
local max_key_help = 0
7+
local max_short_help = 0
8+
local outs_help = {}
9+
10+
for _, m in pairs(DEFAULT_KEYMAPS) do
11+
local first = true
12+
local keys = type(m.key) == "table" and m.key or { m.key }
13+
for _, key in ipairs(keys) do
14+
local out = {}
15+
out.key = key
16+
max_key_help = math.max(#out.key, max_key_help)
17+
if first then
18+
out.short = m.desc.short
19+
max_short_help = math.max(#out.short, max_short_help)
20+
out.long = m.desc.long
21+
first = false
22+
end
23+
table.insert(outs_help, out)
24+
end
25+
end
26+
27+
-- help
28+
local file = io.open("/tmp/DEFAULT_KEYMAPS.help", "w")
29+
io.output(file)
30+
io.write "\n"
31+
local fmt = string.format("%%-%d.%ds %%-%d.%ds %%s\n", max_key_help, max_key_help, max_short_help, max_short_help)
32+
for _, m in pairs(outs_help) do
33+
if not m.short then
34+
io.write(string.format("%s\n", m.key))
35+
else
36+
io.write(string.format(fmt, m.key, m.short, m.long))
37+
end
38+
end
39+
io.write "\n"
40+
io.close(file)
41+
42+
-- lua on_attach
43+
file = io.open("/tmp/DEFAULT_KEYMAPS.on_attach.lua", "w")
44+
io.output(file)
45+
io.write "local function on_attach(bufnr, mode, opts)\n"
46+
io.write "local Api = require('nvim-tree.api')\n"
47+
for _, m in pairs(DEFAULT_KEYMAPS) do
48+
local keys = type(m.key) == "table" and m.key or { m.key }
49+
for _, key in ipairs(keys) do
50+
io.write(string.format(" vim.keymap.set(mode, '%s', %s, opts)\n", key, m.callback))
51+
end
52+
end
53+
io.write "end\n"
54+
io.close(file)

scripts/update-help.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,10 @@ sed -i -e "/${begin}/,/${end}/{ /${begin}/{p; r /tmp/DEFAULT_MAPPINGS.lua
3131
}; /${end}/p; d }" doc/nvim-tree-lua.txt
3232
sed -i -e "/^DEFAULT MAPPINGS/,/^>$/{ /^DEFAULT MAPPINGS/{p; r /tmp/DEFAULT_MAPPINGS.help
3333
}; /^>$/p; d }" doc/nvim-tree-lua.txt
34+
35+
# generate various DEFAULT_KEYMAPS
36+
begin="BEGIN_DEFAULT_KEYMAPS"
37+
end="END_DEFAULT_KEYMAPS"
38+
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
39+
cat /tmp/DEFAULT_KEYMAPS.M.lua scripts/generate_default_keymaps.lua | lua
40+

0 commit comments

Comments
 (0)