From de53d6499a5aa37798f0386bc78191c2c6348795 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Mon, 1 Aug 2022 15:32:50 +1000 Subject: [PATCH] chore(mappings): POC for help and :help on_attach keymaps --- lua/nvim-tree/keymap.lua | 10 ++++-- scripts/generate_default_keymaps.lua | 54 ++++++++++++++++++++++++++++ scripts/update-help.sh | 7 ++++ 3 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 scripts/generate_default_keymaps.lua diff --git a/lua/nvim-tree/keymap.lua b/lua/nvim-tree/keymap.lua index d7fd0525..b54bb31d 100644 --- a/lua/nvim-tree/keymap.lua +++ b/lua/nvim-tree/keymap.lua @@ -2,7 +2,8 @@ local Api = require "nvim-tree.api" local M = {} -M.DEFAULT_KEYMAPS = { +-- BEGIN_DEFAULT_KEYMAPS +local DEFAULT_KEYMAPS = { { key = { "", "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 diff --git a/scripts/generate_default_keymaps.lua b/scripts/generate_default_keymaps.lua new file mode 100644 index 00000000..daa46a7c --- /dev/null +++ b/scripts/generate_default_keymaps.lua @@ -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) diff --git a/scripts/update-help.sh b/scripts/update-help.sh index 46ac7c68..8b284049 100755 --- a/scripts/update-help.sh +++ b/scripts/update-help.sh @@ -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 +