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: support all whichkey methods #82

Merged
merged 6 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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: 4 additions & 2 deletions lua/hawtkeys/ts.lua
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,9 @@ local function find_maps_in_file(filePath)
local strObj =
vim.treesitter.get_node_text(node.node, fileContent)
local ok, tableObj = pcall(function()
return loadstring("return " .. strObj)()
--Remove wrapping parens issue #81
tris203 marked this conversation as resolved.
Show resolved Hide resolved
strObj = strObj:gsub("^%s*%(%s*", ""):gsub("%s*%)%s*$", "")
return loadstring("return {" .. strObj .. "}")()
end)
if not ok then
vim.notify_once(
Expand All @@ -353,7 +355,7 @@ local function find_maps_in_file(filePath)
)
break
end
local wkMapping = which_key.parse(tableObj)
local wkMapping = which_key.parse(unpack(tableObj))

for _, mapping in ipairs(wkMapping) do
local map = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
local wk = require("which-key")

wk.register({
w = {
name = "file", -- optional group name
o = { ':lua print("hello")<CR>', "Find File" }, -- create a binding with label
},
}, { prefix = "<leader>" })
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
local wk = require("which-key")

wk.register({
["<leader>"] = {
w = {
name = "test",
t = { ':lua print("hello")<CR>', "test" },
},
},
})
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local whichkey = require("which-key")
local wk = require("which-key")

whichkey.register({
["<leader>"] = {
wk.register({
["<leader>w"] = {
name = "test",
["3"] = { ':lua print("hello")<CR>', "hello" },
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
local wk = require("which-key")

wk.register({
["<leader>wf"] = { ':lua print("hello")<CR>', "hello" },
})
39 changes: 33 additions & 6 deletions tests/hawtkeys/ts_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe("Uninstalled plugins", function()
["lazy"] = {
method = "lazy",
},
["whichkey.register"] = {
["wk.register"] = {
method = "which_key",
},
},
Expand All @@ -45,7 +45,7 @@ describe("Uninstalled plugins", function()
return require("which-key")
end)
local keymapWhichKey = ts.find_maps_in_file(
"tests/hawtkeys/example_configs/which-key.register_keymap.lua"
"tests/hawtkeys/example_configs/which-key.register_keymap_method1.lua"
)
local messages = vim.api.nvim_exec2("messages", { output = true })
eq(false, ok)
Expand Down Expand Up @@ -170,19 +170,46 @@ describe("Which Key Managed Maps", function()
ts.reset_scanned_files()
hawtkeys.setup({
customMaps = {
["whichkey.register"] = {
["wk.register"] = {
method = "which_key",
},
},
})
end)

it("extract whichkey.register() keymap", function()
it("extract whichkey method 1", function()
local keymap = ts.find_maps_in_file(
"tests/hawtkeys/example_configs/which-key.register_keymap.lua"
"tests/hawtkeys/example_configs/which-key.register_keymap_method1.lua"
)
eq("n", keymap[1].mode)
eq("<leader>3", keymap[1].lhs)
eq("<leader>wo", keymap[1].lhs)
eq(':lua print("hello")<CR>', keymap[1].rhs)
end)

it("extract whichkey method 2", function()
local keymap = ts.find_maps_in_file(
"tests/hawtkeys/example_configs/which-key.register_keymap_method2.lua"
)
eq("n", keymap[1].mode)
eq("<leader>wt", keymap[1].lhs)
eq(':lua print("hello")<CR>', keymap[1].rhs)
end)

it("extract whichkey method 3", function()
local keymap = ts.find_maps_in_file(
"tests/hawtkeys/example_configs/which-key.register_keymap_method3.lua"
)
eq("n", keymap[1].mode)
eq("<leader>w3", keymap[1].lhs)
eq(':lua print("hello")<CR>', keymap[1].rhs)
end)

it("extract whichkey method 4", function()
local keymap = ts.find_maps_in_file(
"tests/hawtkeys/example_configs/which-key.register_keymap_method4.lua"
)
eq("n", keymap[1].mode)
eq("<leader>wf", keymap[1].lhs)
eq(':lua print("hello")<CR>', keymap[1].rhs)
end)
end)
Expand Down
Loading