Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(extension): Fix extension configuration for which-key.nvim extension #405

Merged
merged 2 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion doc/WHICH_KEY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
```
8 changes: 6 additions & 2 deletions lua/legendary/extensions/which_key.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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, opts.do_binding, opts.use_groups)
original(whichkey_tbls, whichkey_opts)
Log.trace('Successfully registered items from which-key.nvim')
end
Expand Down
41 changes: 29 additions & 12 deletions lua/legendary/util/which_key.lua
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -11,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
Expand All @@ -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
Expand All @@ -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')
Expand Down Expand Up @@ -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 = {}
Expand All @@ -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)

Expand All @@ -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
Expand Down
Loading