From df21595fa7d5fb75f3b0b3ebdd105e47f5698387 Mon Sep 17 00:00:00 2001 From: Mat Jones Date: Wed, 11 Oct 2023 07:10:13 -0400 Subject: [PATCH 1/2] fix(extension): Fix extension configuration for which-key.nvim extension --- doc/WHICH_KEY.md | 6 +++- lua/legendary/extensions/which_key.lua | 8 ++++-- lua/legendary/util/which_key.lua | 39 ++++++++++++++++++-------- 3 files changed, 39 insertions(+), 14 deletions(-) diff --git a/doc/WHICH_KEY.md b/doc/WHICH_KEY.md index f59e1ecb..1a9a31b6 100644 --- a/doc/WHICH_KEY.md +++ b/doc/WHICH_KEY.md @@ -32,7 +32,11 @@ require('legendary.util.which-key').bind_whichkey( your_which_key_opts, -- false if which-key.nvim handles binding them, -- set to true if you want legendary.nvim to handle binding - -- the mappings; if not passed, true by default + -- the mappings; if not passed, true by default. false, + -- true if you want to use item groups, + -- false if you want to add the items to the toplevel list + -- in legendary.nvim; defaults to true if omitted. + true, ) ``` diff --git a/lua/legendary/extensions/which_key.lua b/lua/legendary/extensions/which_key.lua index db3a0993..cde0f2ad 100644 --- a/lua/legendary/extensions/which_key.lua +++ b/lua/legendary/extensions/which_key.lua @@ -20,15 +20,19 @@ return function(opts) opts.do_binding = false end + if opts.use_groups == nil then + opts.use_groups = true + end + if #vim.tbl_keys(opts.mappings or {}) > 0 then - util.bind_whichkey(opts.mappings, opts.opts, opts.do_binding) + util.bind_whichkey(opts.mappings, opts.opts, opts.do_binding, opts.use_groups) end end local original = wk.register local listener = function(whichkey_tbls, whichkey_opts) Log.trace('Preparing to register items from which-key.nvim automatically') - util.bind_whichkey(whichkey_tbls, whichkey_opts, false) + util.bind_whichkey(whichkey_tbls, whichkey_opts, false, opts.use_groups) original(whichkey_tbls, whichkey_opts) Log.trace('Successfully registered items from which-key.nvim') end diff --git a/lua/legendary/util/which_key.lua b/lua/legendary/util/which_key.lua index 5130959c..b4475861 100644 --- a/lua/legendary/util/which_key.lua +++ b/lua/legendary/util/which_key.lua @@ -1,6 +1,5 @@ local State = require('legendary.data.state') local Keymap = require('legendary.data.keymap') -local Config = require('legendary.config') local Lazy = require('legendary.vendor.lazy') ---@type ItemGroup @@ -21,8 +20,13 @@ end ---@param wk table ---@param wk_opts table +---@param use_groups boolean ---@return table -local function wk_to_legendary(wk, wk_opts, wk_groups) +local function wk_to_legendary(wk, wk_opts, wk_groups, use_groups) + if use_groups == nil then + use_groups = true + end + local legendary = {} legendary[1] = wk.prefix if wk.cmd then @@ -31,11 +35,11 @@ local function wk_to_legendary(wk, wk_opts, wk_groups) if wk_opts and wk_opts.mode then legendary.mode = wk_opts.mode end - if wk.group == true and #wk.name > 0 and Config.which_key.use_groups then + if wk.group == true and #wk.name > 0 and use_groups then legendary.itemgroup = wk.name end - local group = Config.which_key.use_groups and longest_matching_group(wk, wk_groups) or nil - if group and Config.which_key.use_groups then + local group = use_groups and longest_matching_group(wk, wk_groups) or nil + if group and use_groups then legendary.itemgroup = group end legendary.description = wk.label or vim.tbl_get(wk, 'opts', 'desc') @@ -69,12 +73,17 @@ end --- and parse them into legendary.nvim tables ---@param which_key_tbls table[] ---@param which_key_opts table ----@param do_binding boolean whether to bind the keymaps or let which-key handle it +---@param do_binding boolean whether to bind the keymaps or let which-key handle it; default true +---@param use_groups boolean whether to use item groups; default true ---@return LegendaryItem[] -function M.parse_whichkey(which_key_tbls, which_key_opts, do_binding) +function M.parse_whichkey(which_key_tbls, which_key_opts, do_binding, use_groups) if do_binding == nil then do_binding = true end + if use_groups == nil then + use_groups = true + end + local wk_parsed = require('which-key.mappings').parse(which_key_tbls, which_key_opts) local legendary_tbls = {} local wk_groups = {} @@ -86,7 +95,7 @@ function M.parse_whichkey(which_key_tbls, which_key_opts, do_binding) end, wk_parsed) vim.tbl_map(function(wk) if vim.tbl_get(wk, 'opts', 'desc') and wk.group ~= true then - table.insert(legendary_tbls, wk_to_legendary(wk, which_key_opts, wk_groups)) + table.insert(legendary_tbls, wk_to_legendary(wk, which_key_opts, wk_groups, use_groups)) end end, wk_parsed) @@ -103,9 +112,17 @@ end --- Bind a which-key.nvim table with legendary.nvim ---@param wk_tbls table ---@param wk_opts table ----@param do_binding boolean whether to bind the keymaps or let which-key handle it -function M.bind_whichkey(wk_tbls, wk_opts, do_binding) - local legendary_tbls = M.parse_whichkey(wk_tbls, wk_opts, do_binding) +---@param do_binding boolean whether to bind the keymaps or let which-key handle it; default true +---@param use_groups boolean whether to use item groups; default true +function M.bind_whichkey(wk_tbls, wk_opts, do_binding, use_groups) + if do_binding == nil then + do_binding = true + end + if use_groups == nil then + use_groups = true + end + + local legendary_tbls = M.parse_whichkey(wk_tbls, wk_opts, do_binding, use_groups) State.items:add(vim.tbl_map(function(keymap) local parsed if keymap.itemgroup and keymap.keymaps then From 431d6e775a7fb7e80389d999f840f3f88ac8ca90 Mon Sep 17 00:00:00 2001 From: Mat Jones Date: Wed, 11 Oct 2023 07:34:40 -0400 Subject: [PATCH 2/2] fix(extension): Fix which-key toplevel group parsing --- lua/legendary/extensions/which_key.lua | 2 +- lua/legendary/util/which_key.lua | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/legendary/extensions/which_key.lua b/lua/legendary/extensions/which_key.lua index cde0f2ad..9e715bd2 100644 --- a/lua/legendary/extensions/which_key.lua +++ b/lua/legendary/extensions/which_key.lua @@ -32,7 +32,7 @@ return function(opts) local original = wk.register local listener = function(whichkey_tbls, whichkey_opts) Log.trace('Preparing to register items from which-key.nvim automatically') - util.bind_whichkey(whichkey_tbls, whichkey_opts, false, opts.use_groups) + util.bind_whichkey(whichkey_tbls, whichkey_opts, opts.do_binding, opts.use_groups) original(whichkey_tbls, whichkey_opts) Log.trace('Successfully registered items from which-key.nvim') end diff --git a/lua/legendary/util/which_key.lua b/lua/legendary/util/which_key.lua index b4475861..8abb6840 100644 --- a/lua/legendary/util/which_key.lua +++ b/lua/legendary/util/which_key.lua @@ -10,7 +10,7 @@ local M = {} local function longest_matching_group(wk, wk_groups) local matching_group = {} for prefix, group_data in pairs(wk_groups) do - if vim.startswith(wk.prefix, prefix) and #prefix > #(matching_group[1] or '') then + if prefix == '' or vim.startswith(wk.prefix, prefix) and #prefix > #(matching_group[1] or '') then matching_group = { prefix, group_data } end end