diff --git a/lua/hawtkeys/ts.lua b/lua/hawtkeys/ts.lua index 1a43acf..f99d981 100644 --- a/lua/hawtkeys/ts.lua +++ b/lua/hawtkeys/ts.lua @@ -344,16 +344,19 @@ 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 and wrap in table and unpack - issue #81 + strObj = strObj:gsub("^%s*%(%s*", ""):gsub("%s*%)%s*$", "") + return loadstring("return {" .. strObj .. "}")() end) if not ok then vim.notify_once( "Error parsing which-key table", vim.log.levels.ERROR ) + vim.notify(strObj, vim.log.levels.ERROR) break end - local wkMapping = which_key.parse(tableObj) + local wkMapping = which_key.parse(unpack(tableObj)) for _, mapping in ipairs(wkMapping) do local map = { diff --git a/tests/hawtkeys/example_configs/which-key.register_keymap_method1.lua b/tests/hawtkeys/example_configs/which-key.register_keymap_method1.lua new file mode 100644 index 0000000..b8abb9f --- /dev/null +++ b/tests/hawtkeys/example_configs/which-key.register_keymap_method1.lua @@ -0,0 +1,8 @@ +local wk = require("which-key") + +wk.register({ + w = { + name = "file", -- optional group name + o = { ':lua print("hello")', "Find File" }, -- create a binding with label + }, +}, { prefix = "" }) diff --git a/tests/hawtkeys/example_configs/which-key.register_keymap_method2.lua b/tests/hawtkeys/example_configs/which-key.register_keymap_method2.lua new file mode 100644 index 0000000..8b9f91a --- /dev/null +++ b/tests/hawtkeys/example_configs/which-key.register_keymap_method2.lua @@ -0,0 +1,10 @@ +local wk = require("which-key") + +wk.register({ + [""] = { + w = { + name = "test", + t = { ':lua print("hello")', "test" }, + }, + }, +}) diff --git a/tests/hawtkeys/example_configs/which-key.register_keymap.lua b/tests/hawtkeys/example_configs/which-key.register_keymap_method3.lua similarity index 53% rename from tests/hawtkeys/example_configs/which-key.register_keymap.lua rename to tests/hawtkeys/example_configs/which-key.register_keymap_method3.lua index fdd187c..c26b175 100644 --- a/tests/hawtkeys/example_configs/which-key.register_keymap.lua +++ b/tests/hawtkeys/example_configs/which-key.register_keymap_method3.lua @@ -1,7 +1,7 @@ -local whichkey = require("which-key") +local wk = require("which-key") -whichkey.register({ - [""] = { +wk.register({ + ["w"] = { name = "test", ["3"] = { ':lua print("hello")', "hello" }, }, diff --git a/tests/hawtkeys/example_configs/which-key.register_keymap_method4.lua b/tests/hawtkeys/example_configs/which-key.register_keymap_method4.lua new file mode 100644 index 0000000..6b00cc5 --- /dev/null +++ b/tests/hawtkeys/example_configs/which-key.register_keymap_method4.lua @@ -0,0 +1,5 @@ +local wk = require("which-key") + +wk.register({ + ["wf"] = { ':lua print("hello")', "hello" }, +}) diff --git a/tests/hawtkeys/ts_spec.lua b/tests/hawtkeys/ts_spec.lua index faa7bab..7b819b9 100644 --- a/tests/hawtkeys/ts_spec.lua +++ b/tests/hawtkeys/ts_spec.lua @@ -33,7 +33,7 @@ describe("Uninstalled plugins", function() ["lazy"] = { method = "lazy", }, - ["whichkey.register"] = { + ["wk.register"] = { method = "which_key", }, }, @@ -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) @@ -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("3", keymap[1].lhs) + eq("wo", keymap[1].lhs) + eq(':lua print("hello")', 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("wt", keymap[1].lhs) + eq(':lua print("hello")', 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("w3", keymap[1].lhs) + eq(':lua print("hello")', 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("wf", keymap[1].lhs) eq(':lua print("hello")', keymap[1].rhs) end) end)