Skip to content

Commit

Permalink
fix: lazy mapped keys with functions (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
tris203 authored Dec 29, 2023
1 parent 120b1ff commit ee2374e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
11 changes: 10 additions & 1 deletion lua/hawtkeys/ts.lua
Original file line number Diff line number Diff line change
Expand Up @@ -376,9 +376,18 @@ local function get_keymaps_from_lazy()
for _, v in ipairs(lazy) do
if v and v._ and v._.handlers and v._.handlers.keys then
for _, key in pairs(v._.handlers.keys) do
if type(key.rhs) == "table" then
key.rhs = tostring(key.rhs)
elseif type(key.rhs) == "function" then
local debugInfo =
debug.getinfo(key.rhs --[[@as fun()]], "S")
key.rhs = utils.reduceHome(debugInfo.short_src)
.. ":"
.. debugInfo.linedefined
end
local map = {
lhs = key.lhs,
rhs = key.rhs,
rhs = tostring(key.rhs),
mode = key.mode,
from_file = "Lazy Init:" .. tostring(v[1]),
}
Expand Down
7 changes: 4 additions & 3 deletions lua/hawtkeys/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ local M = {}
local Hawtkeys = require("hawtkeys.score")
local ShowAll = require("hawtkeys.show_all")
local showDuplicates = require("hawtkeys.duplicates")
local utils = require("hawtkeys.utils")

local Namespace = vim.api.nvim_create_namespace("hawtkeys")

Expand Down Expand Up @@ -263,7 +264,7 @@ M.show_all = function()
local all = ShowAll.show_all()
local pattern = "%s (%s) - %s"
for i, data in ipairs(all) do
local filename = data.from_file:gsub(vim.env.HOME, "~")
local filename = utils.reduceHome(data.from_file)
local line = pattern:format(data.lhs, data.mode, filename)

local offset_mode = #data.lhs + 2
Expand Down Expand Up @@ -319,8 +320,8 @@ M.show_dupes = function()
local dupes = showDuplicates.show_duplicates()
local pattern = "%s : %s"
for i, data in ipairs(dupes) do
local filename1 = data.file1:gsub(vim.env.HOME, "~")
local filename2 = data.file2:gsub(vim.env.HOME, "~")
local filename1 = utils.reduceHome(data.file1)
local filename2 = utils.reduceHome(data.file2)
local line = pattern:format(filename1, filename2)

local l2 = data.key
Expand Down
7 changes: 7 additions & 0 deletions lua/hawtkeys/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,11 @@ function M.find_duplicates(keymaps)
return duplicates
end

---@param path string
---@return string
function M.reduceHome(path)
local reduced = path:gsub(vim.env.HOME, "~")
return reduced
end

return M
30 changes: 28 additions & 2 deletions tests/hawtkeys/ts_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ describe("Lazy Managed Plugins", function()
package.loaded[modname] = nil
end
end
end)
it("extract keys set in a Lazy init", function()
local lazy = require("lazy")
lazy.setup({
"ellisonleao/nvim-plugin-template",
Expand All @@ -185,8 +187,6 @@ describe("Lazy Managed Plugins", function()
},
},
})
end)
it("extract keys set in a Lazy init", function()
local lazyKeymaps = ts.get_keymaps_from_lazy()
eq("n", lazyKeymaps[1].mode)
eq("<leader>1", lazyKeymaps[1].lhs)
Expand All @@ -196,4 +196,30 @@ describe("Lazy Managed Plugins", function()
lazyKeymaps[1].from_file
)
end)
it(
"extract keys where the lazy key setting is a function and returns as string",
function()
local lazy = require("lazy")
lazy.setup({
"ellisonleao/nvim-plugin-template",
keys = {
{
"<leader>1",
function()
print(1)
end,
desc = "Test Lazy Print 1",
},
},
})
local lazyKeymaps = ts.get_keymaps_from_lazy()
eq("n", lazyKeymaps[1].mode)
eq("<leader>1", lazyKeymaps[1].lhs)
eq("string", type(lazyKeymaps[1].rhs))
eq(
"Lazy Init:ellisonleao/nvim-plugin-template",
lazyKeymaps[1].from_file
)
end
)
end)

0 comments on commit ee2374e

Please sign in to comment.