From 3439c30a969d07dcc5435b9e547e43766fafd597 Mon Sep 17 00:00:00 2001 From: tris203 Date: Thu, 11 Jan 2024 22:42:03 +0000 Subject: [PATCH 1/6] fix: support all whichkey methods --- lua/hawtkeys/ts.lua | 3 ++ .../which-key.register_keymap_method1.lua | 14 ++++++++ .../which-key.register_keymap_method2.lua | 10 ++++++ ... => which-key.register_keymap_method3.lua} | 6 ++-- .../which-key.register_keymap_method4.lua | 5 +++ tests/hawtkeys/ts_spec.lua | 36 ++++++++++++++++--- 6 files changed, 67 insertions(+), 7 deletions(-) create mode 100644 tests/hawtkeys/example_configs/which-key.register_keymap_method1.lua create mode 100644 tests/hawtkeys/example_configs/which-key.register_keymap_method2.lua rename tests/hawtkeys/example_configs/{which-key.register_keymap.lua => which-key.register_keymap_method3.lua} (53%) create mode 100644 tests/hawtkeys/example_configs/which-key.register_keymap_method4.lua diff --git a/lua/hawtkeys/ts.lua b/lua/hawtkeys/ts.lua index 1a43acf..72d5b8b 100644 --- a/lua/hawtkeys/ts.lua +++ b/lua/hawtkeys/ts.lua @@ -344,6 +344,8 @@ local function find_maps_in_file(filePath) local strObj = vim.treesitter.get_node_text(node.node, fileContent) local ok, tableObj = pcall(function() + --Remove wrapping parens issue #81 + strObj = strObj:gsub("^%s*%(%s*", ""):gsub("%s*%)%s*$", "") return loadstring("return " .. strObj)() end) if not ok then @@ -354,6 +356,7 @@ local function find_maps_in_file(filePath) break end local wkMapping = which_key.parse(tableObj) + print(vim.inspect(wkMapping)) 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..426c560 --- /dev/null +++ b/tests/hawtkeys/example_configs/which-key.register_keymap_method1.lua @@ -0,0 +1,14 @@ +local wk = require("which-key") + +wk.register({ + w = { + name = "file", -- optional group name + o = { ":lua print('hello')", "Find File" }, -- create a binding with label + n = { + function() + print("bar") + end, + "Foobar", + }, -- you can also pass functions! + }, +}, { 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..6cedc5d 100644 --- a/tests/hawtkeys/ts_spec.lua +++ b/tests/hawtkeys/ts_spec.lua @@ -170,21 +170,49 @@ 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) describe("Lazy Managed Plugins", function() From 503e20fa7317000c2b4cfdb5e67af5849e17b470 Mon Sep 17 00:00:00 2001 From: tris203 Date: Thu, 11 Jan 2024 22:42:53 +0000 Subject: [PATCH 2/6] chore: style --- tests/hawtkeys/ts_spec.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/hawtkeys/ts_spec.lua b/tests/hawtkeys/ts_spec.lua index 6cedc5d..683aeeb 100644 --- a/tests/hawtkeys/ts_spec.lua +++ b/tests/hawtkeys/ts_spec.lua @@ -212,7 +212,6 @@ describe("Which Key Managed Maps", function() eq("wf", keymap[1].lhs) eq(':lua print("hello")', keymap[1].rhs) end) - end) describe("Lazy Managed Plugins", function() From 0b74611a6a2010b1222ca48b34fedd6ee45e658d Mon Sep 17 00:00:00 2001 From: tris203 Date: Thu, 11 Jan 2024 23:13:56 +0000 Subject: [PATCH 3/6] table and unpack --- lua/hawtkeys/ts.lua | 5 ++--- .../example_configs/which-key.register_keymap_method1.lua | 8 +------- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/lua/hawtkeys/ts.lua b/lua/hawtkeys/ts.lua index 72d5b8b..381b15e 100644 --- a/lua/hawtkeys/ts.lua +++ b/lua/hawtkeys/ts.lua @@ -346,7 +346,7 @@ local function find_maps_in_file(filePath) local ok, tableObj = pcall(function() --Remove wrapping parens issue #81 strObj = strObj:gsub("^%s*%(%s*", ""):gsub("%s*%)%s*$", "") - return loadstring("return " .. strObj)() + return loadstring("return {" .. strObj .. "}")() end) if not ok then vim.notify_once( @@ -355,8 +355,7 @@ local function find_maps_in_file(filePath) ) break end - local wkMapping = which_key.parse(tableObj) - print(vim.inspect(wkMapping)) + 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 index 426c560..b8abb9f 100644 --- a/tests/hawtkeys/example_configs/which-key.register_keymap_method1.lua +++ b/tests/hawtkeys/example_configs/which-key.register_keymap_method1.lua @@ -3,12 +3,6 @@ local wk = require("which-key") wk.register({ w = { name = "file", -- optional group name - o = { ":lua print('hello')", "Find File" }, -- create a binding with label - n = { - function() - print("bar") - end, - "Foobar", - }, -- you can also pass functions! + o = { ':lua print("hello")', "Find File" }, -- create a binding with label }, }, { prefix = "" }) From 1c7a65a977131a77faba513ee38358e9bb96e87b Mon Sep 17 00:00:00 2001 From: tris203 Date: Thu, 11 Jan 2024 23:46:08 +0000 Subject: [PATCH 4/6] tests: fix tests --- tests/hawtkeys/ts_spec.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/hawtkeys/ts_spec.lua b/tests/hawtkeys/ts_spec.lua index 683aeeb..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) From 7bb4ff9e21d79e94fb55f1e2c1113f2a299f8df8 Mon Sep 17 00:00:00 2001 From: Tristan Knight Date: Thu, 11 Jan 2024 23:49:02 +0000 Subject: [PATCH 5/6] Update lua/hawtkeys/ts.lua --- lua/hawtkeys/ts.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/hawtkeys/ts.lua b/lua/hawtkeys/ts.lua index 381b15e..98d7fc4 100644 --- a/lua/hawtkeys/ts.lua +++ b/lua/hawtkeys/ts.lua @@ -344,7 +344,7 @@ local function find_maps_in_file(filePath) local strObj = vim.treesitter.get_node_text(node.node, fileContent) local ok, tableObj = pcall(function() - --Remove wrapping parens issue #81 + --Remove wrapping parens and wrap in table and unpack - issue #81 strObj = strObj:gsub("^%s*%(%s*", ""):gsub("%s*%)%s*$", "") return loadstring("return {" .. strObj .. "}")() end) From db012d35e26ebb12b2589da94be691cf1a3a77b2 Mon Sep 17 00:00:00 2001 From: tris203 Date: Fri, 12 Jan 2024 00:15:12 +0000 Subject: [PATCH 6/6] Print object in error --- lua/hawtkeys/ts.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/lua/hawtkeys/ts.lua b/lua/hawtkeys/ts.lua index 98d7fc4..f99d981 100644 --- a/lua/hawtkeys/ts.lua +++ b/lua/hawtkeys/ts.lua @@ -353,6 +353,7 @@ local function find_maps_in_file(filePath) "Error parsing which-key table", vim.log.levels.ERROR ) + vim.notify(strObj, vim.log.levels.ERROR) break end local wkMapping = which_key.parse(unpack(tableObj))