From 354b810738281cb50886b91f1a22bf72d9cc8bca Mon Sep 17 00:00:00 2001 From: tris203 Date: Thu, 21 Dec 2023 22:10:23 +0000 Subject: [PATCH] feat: lhs blacklists --- lua/hawtkeys/init.lua | 12 +++++++-- lua/hawtkeys/ts.lua | 26 ++++++++++++++----- .../blacklistCharacter_keymap.lua | 11 ++++++++ tests/hawtkeys/ts_spec.lua | 22 ++++++++++++++-- 4 files changed, 60 insertions(+), 11 deletions(-) create mode 100644 tests/hawtkeys/example_configs/blacklistCharacter_keymap.lua diff --git a/lua/hawtkeys/init.lua b/lua/hawtkeys/init.lua index 8e48fad..310c0d3 100644 --- a/lua/hawtkeys/init.lua +++ b/lua/hawtkeys/init.lua @@ -10,9 +10,10 @@ local M = {} ---@field homerow number ---@field powerFingers number[] ---@field keyboardLayout HawtKeySupportedKeyboardLayouts ----@field keyMapSet { [string] : TSKeyMapArgs | WhichKeyMapargs } | nil +---@field keyMapSet { [string] : TSKeyMapArgs | WhichKeyMapargs } ---@field customMaps { [string] : TSKeyMapArgs | WhichKeyMapargs } | nil ----@field highlights HawtKeyHighlights | nil +---@field highlights HawtKeyHighlights +---@field lhsBlacklist string[] ---@class HawtKeyHighlights ---@field HawtkeysMatchGreat vim.api.keyset.highlight | nil @@ -27,6 +28,7 @@ local M = {} ---@field keyboardLayout HawtKeySupportedKeyboardLayouts | nil ---@field customMaps { [string] : TSKeyMapArgs | WhichKeyMapargs } | nil ---@field highlights HawtKeyHighlights | nil +---@field lhsBlacklist string[] | nil ---@type { [string] : TSKeyMapArgs | WhichKeyMapargs }--- local _defaultSet = { @@ -49,6 +51,7 @@ local _defaultSet = { }, -- method 6 } +---@type HawtKeyConfig local defaultConfig = { leader = " ", homerow = 2, @@ -61,6 +64,11 @@ local defaultConfig = { HawtkeysMatchOk = { link = "DiagnosticWarn" }, HawtkeysMatchBad = { link = "DiagnosticError" }, }, + --[[ + is used internally by vim to map keys to functions +Þ is used internally by whickkey to map NOP functions for menu popup timeout +]] + lhsBlacklist = { "", "Þ" }, } local function apply_highlights() diff --git a/lua/hawtkeys/ts.lua b/lua/hawtkeys/ts.lua index 1d8d0ae..8eb95bf 100644 --- a/lua/hawtkeys/ts.lua +++ b/lua/hawtkeys/ts.lua @@ -358,13 +358,24 @@ local function get_keymaps_from_vim() local vimKeymapsRaw = vim.api.nvim_get_keymap("") print("Collecting vim keymaps") for _, vimKeymap in ipairs(vimKeymapsRaw) do - table.insert(vimKeymaps, { - mode = vimKeymap.mode, - -- TODO: leader subsitiution as vim keymaps contain raw leader - lhs = vimKeymap.lhs:gsub(hawtkeys.config.leader, ""), - rhs = vimKeymap.rhs, - from_file = "Vim Defaults", - }) + local count = vim.tbl_count(hawtkeys.config.lhsBlacklist) + for _, blacklist in ipairs(hawtkeys.config.lhsBlacklist) do + if not vimKeymap.lhs:lower():match(blacklist) then + count = count - 1 + if count == 0 then + table.insert(vimKeymaps, { + mode = vimKeymap.mode, + -- TODO: leader subsitiution as vim keymaps contain raw leader + lhs = vimKeymap.lhs:gsub( + hawtkeys.config.leader, + "" + ), + rhs = vimKeymap.rhs, + from_file = "Vim Defaults", + }) + end + end + end end return vimKeymaps end @@ -404,5 +415,6 @@ M.reset_scanned_files = function() end M.find_maps_in_file = find_maps_in_file +M.get_keymaps_from_vim = get_keymaps_from_vim return M diff --git a/tests/hawtkeys/example_configs/blacklistCharacter_keymap.lua b/tests/hawtkeys/example_configs/blacklistCharacter_keymap.lua new file mode 100644 index 0000000..9d8ca08 --- /dev/null +++ b/tests/hawtkeys/example_configs/blacklistCharacter_keymap.lua @@ -0,0 +1,11 @@ +M = {} + +vim.api.nvim_set_keymap('n', 'Test', '', {}) +vim.api.nvim_set_keymap('n', 'abÞ', '', {}) + +M.reset = function() + vim.api.nvim_del_keymap('n', 'Test') + vim.api.nvim_del_keymap('n', 'abÞ') +end + +return M diff --git a/tests/hawtkeys/ts_spec.lua b/tests/hawtkeys/ts_spec.lua index ac90121..985ade7 100644 --- a/tests/hawtkeys/ts_spec.lua +++ b/tests/hawtkeys/ts_spec.lua @@ -2,6 +2,8 @@ local hawtkeys = require("hawtkeys") local ts = require("hawtkeys.ts") ---@diagnostic disable-next-line: undefined-field local eq = assert.are.same +---@diagnostic disable-next-line: undefined-field +local falsy = assert.falsy describe("Treesitter can extract keymaps", function() before_each(function() @@ -58,7 +60,7 @@ describe("Treesitter can extract keymaps", function() hawtkeys.setup({ customMaps = { ["normalMap"] = { - modeIndex = 'n', + modeIndex = "n", lhsIndex = 1, rhsIndex = 2, method = "function_call", @@ -75,5 +77,21 @@ describe("Treesitter can extract keymaps", function() eq(':echo "hello"', keymap[2].rhs) end) - + it("Keymaps containing blacklisted characters are ignored", function() + local numberOfExamples = 2 + local beforeCount = #vim.api.nvim_get_keymap("n") + require("tests.hawtkeys.example_configs.blacklistCharacter_keymap") + local afterCount = #vim.api.nvim_get_keymap("n") + eq(numberOfExamples, afterCount - beforeCount) + local keymap = ts.get_keymaps_from_vim() + local blacklist = hawtkeys.config.lhsBlacklist + for _, map in pairs(keymap) do + for _, blacklistedItem in pairs(blacklist) do + falsy(string.find(map.lhs, blacklistedItem)) + end + end + require("tests.hawtkeys.example_configs.blacklistCharacter_keymap").reset() + local finalCount = #vim.api.nvim_get_keymap("n") + eq(beforeCount, finalCount) + end) end)