Skip to content

Commit

Permalink
feat: lhs blacklists (#34)
Browse files Browse the repository at this point in the history
Co-authored-by: Will Hopkins <willothyh@gmail.com>
Co-authored-by: tris203 <admin@snappeh.com>
  • Loading branch information
3 people authored Dec 21, 2023
1 parent f2e85f8 commit ce9dac1
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 16 deletions.
12 changes: 10 additions & 2 deletions lua/hawtkeys/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = {
Expand All @@ -49,6 +51,7 @@ local _defaultSet = {
}, -- method 6
}

---@type HawtKeyConfig
local defaultConfig = {
leader = " ",
homerow = 2,
Expand All @@ -61,6 +64,11 @@ local defaultConfig = {
HawtkeysMatchOk = { link = "DiagnosticWarn" },
HawtkeysMatchBad = { link = "DiagnosticError" },
},
--[[
<plug> is used internally by vim to map keys to functions
Þ is used internally by whickkey to map NOP functions for menu popup timeout
]]
lhsBlacklist = { "<plug>", "Þ" },
}

local function apply_highlights()
Expand Down
34 changes: 22 additions & 12 deletions lua/hawtkeys/ts.lua
Original file line number Diff line number Diff line change
Expand Up @@ -356,20 +356,29 @@ local function get_keymaps_from_vim()
local vimKeymaps = {}

local vimKeymapsRaw = vim.api.nvim_get_keymap("")
print("Collecting vim keymaps")

for _, vimKeymap in ipairs(vimKeymapsRaw) do
local rhs = vimKeymap.rhs
if rhs == nil or rhs == "" then
rhs = vimKeymap.desc
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
local rhs = vimKeymap.rhs
if rhs == nil or rhs == "" then
rhs = vimKeymap.desc
end
table.insert(vimKeymaps, {
mode = vimKeymap.mode,
-- TODO: leader subsitiution as vim keymaps contain raw leader
lhs = vimKeymap.lhs:gsub(
hawtkeys.config.leader,
"<leader>"
),
rhs = rhs,
from_file = "Vim Defaults",
})
end
end
end
table.insert(vimKeymaps, {
mode = vimKeymap.mode,
-- TODO: leader subsitiution as vim keymaps contain raw leader
rhs = rhs,
lhs = vimKeymap.lhs:gsub(hawtkeys.config.leader, "<leader>"),
from_file = "Vim Defaults",
})
end
return vimKeymaps
end
Expand Down Expand Up @@ -409,5 +418,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
11 changes: 11 additions & 0 deletions tests/hawtkeys/example_configs/blacklistCharacter_keymap.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
M = {}

vim.api.nvim_set_keymap('n', '<Plug>Test', '', {})
vim.api.nvim_set_keymap('n', '<leader>abÞ', '', {})

M.reset = function()
vim.api.nvim_del_keymap('n', '<Plug>Test')
vim.api.nvim_del_keymap('n', '<leader>abÞ')
end

return M
22 changes: 20 additions & 2 deletions tests/hawtkeys/ts_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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",
Expand All @@ -75,5 +77,21 @@ describe("Treesitter can extract keymaps", function()
eq(':echo "hello"<CR>', 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)

0 comments on commit ce9dac1

Please sign in to comment.