From 2e9d3edc9716f3503acb940caef8696ab773cfa4 Mon Sep 17 00:00:00 2001 From: tris203 Date: Sun, 17 Dec 2023 21:50:03 +0000 Subject: [PATCH 1/2] feat: duplicate checks --- lua/hawtkeys/duplicates.lua | 22 +++++++++++++++++++++ lua/hawtkeys/init.lua | 5 +++++ lua/hawtkeys/ts.lua | 6 ++++++ lua/hawtkeys/ui.lua | 28 +++++++++++++++++++++++++++ lua/hawtkeys/utils.lua | 38 ++++++++++++++++++++++++++----------- 5 files changed, 88 insertions(+), 11 deletions(-) create mode 100644 lua/hawtkeys/duplicates.lua diff --git a/lua/hawtkeys/duplicates.lua b/lua/hawtkeys/duplicates.lua new file mode 100644 index 0000000..91647b2 --- /dev/null +++ b/lua/hawtkeys/duplicates.lua @@ -0,0 +1,22 @@ +local M = {} +local utils = require("hawtkeys.utils") +local tsSearch = require("hawtkeys.ts") + +---@return table +function M.show_duplicates() + local allKeys = tsSearch.get_all_keymaps() + local duplicates = utils.findDuplicates(allKeys) + local resultTable = {} + for _, data in ipairs(duplicates) do + table.insert(resultTable, tostring(data[1]) .. " duplicates found in ") + table.insert( + resultTable, + tostring(data[2][1].from_file) + .. ":" + .. tostring(data[2][1].from_file) + ) + end + return resultTable +end + +return M diff --git a/lua/hawtkeys/init.lua b/lua/hawtkeys/init.lua index 429bdf4..61be25c 100644 --- a/lua/hawtkeys/init.lua +++ b/lua/hawtkeys/init.lua @@ -14,6 +14,11 @@ function M.setup(config) "lua require('hawtkeys.ui').showAll()", {} ) + vim.api.nvim_create_user_command( + "HawtkeysDupes", + "lua require('hawtkeys.ui').showDupes()", + {} + ) end return M diff --git a/lua/hawtkeys/ts.lua b/lua/hawtkeys/ts.lua index 6703811..1e5680b 100644 --- a/lua/hawtkeys/ts.lua +++ b/lua/hawtkeys/ts.lua @@ -7,6 +7,7 @@ local ts = require("nvim-treesitter.compat") local ts_query = require("nvim-treesitter.query") local return_keymaps = {} +local scanned_files = {} ---@param dir string ---@return table local function find_files(dir) @@ -20,6 +21,11 @@ end ---@return table local function find_maps_in_file(file_path) print("Scanning files " .. file_path) + if scanned_files[file_path] then + print("Already scanned") + return {} + end + scanned_files[file_path] = true --if not a lua file, return empty table if not string.match(file_path, "%.lua$") then return {} diff --git a/lua/hawtkeys/ui.lua b/lua/hawtkeys/ui.lua index 4f0ce90..bf499a8 100644 --- a/lua/hawtkeys/ui.lua +++ b/lua/hawtkeys/ui.lua @@ -1,6 +1,8 @@ local M = {} local Hawtkeys = require("hawtkeys.score") local ShowAll = require("hawtkeys.show_all") +local showDuplicates = require("hawtkeys.duplicates") + local ResultWin = 0 local ResultBuf = 0 local SearchWin = 0 @@ -124,4 +126,30 @@ M.hide = function() vim.api.nvim_command("stopinsert") end +M.showDupes = function() + local ui = vim.api.nvim_list_uis()[1] + local width = 100 + local height = 30 + ResultBuf = vim.api.nvim_create_buf(false, true) + ResultWin = vim.api.nvim_open_win(ResultBuf, true, { + relative = "editor", + width = width, + height = height, + col = (ui.width / 2) - (width / 2), + row = (ui.height / 2) - (height / 2), + anchor = "NW", + footer = "Duplicate Keybindings", + footer_pos = "center", + border = "single", + noautocmd = true, + }) + vim.api.nvim_buf_set_lines( + ResultBuf, + 0, + -1, + false, + showDuplicates.show_duplicates() + ) +end + return M diff --git a/lua/hawtkeys/utils.lua b/lua/hawtkeys/utils.lua index 9697682..e2f96f3 100644 --- a/lua/hawtkeys/utils.lua +++ b/lua/hawtkeys/utils.lua @@ -38,27 +38,43 @@ end function M.mergeTables(t1, t2) local t3 = {} - for _, v in pairs(t1) do + --[[ for _, v in pairs(t1) do table.insert(t3, v) end for _, v in pairs(t2) do table.insert(t3, v) end +]] - --[[ for _, subtable in pairs(t2) do - for _, v in pairs(t1) do - if v.lhs == subtable.lhs then - print("skipping insert") - goto continue + for _, v in pairs(t1) do + table.insert(t3, v) + end + + for _, v in pairs(t2) do + local found = false + for _, v2 in pairs(t3) do + if v2.lhs == v.lhs and v.from_file == "Vim Defaults" then + found = true end - table.insert(t3, subtable) - ::continue:: end - end ]] - -- TODO: Merge better, so that t1 is prioritised - + if not found then + table.insert(t3, v) + end + end return t3 end +function M.findDuplicates(keymaps) + local duplicates = {} + for _, v in pairs(keymaps) do + for _, v2 in pairs(keymaps) do + if v.lhs == v2.lhs and v.mode == v2.mode and v.rhs ~= v2.rhs then + table.insert(duplicates, { v.lhs, { v }, { v2 } }) + end + end + end + return duplicates +end + return M From 4e5af1d8e087196abffe0e791ad0256323962200 Mon Sep 17 00:00:00 2001 From: tris203 Date: Sun, 17 Dec 2023 21:54:16 +0000 Subject: [PATCH 2/2] docs: update README --- README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/README.md b/README.md index bac9bed..0082bec 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,15 @@ This will allow you to search keybinds as below: This will launch a window showing all existing keymaps collected from Neovim bindings and from anlysis of your config file. +### Showing Duplicate Keymaps + +``` +HawtkeysDupes +``` + +Will show potential duplicate keymaps, where you have accidently set the same key for two different things. This can be useful for tracking down issues with plugins not functioning correctly + + ## Current Issues * Currently on large configs, the search can take a while to iterate through your config. @@ -75,3 +84,9 @@ Outstanding items are currently in the TODO.md file. 3. Commit your Changes (`git commit -m 'Add some AmazingFeature'`) 4. Push to the Branch (`git push origin feature/AmazingFeature`) 5. Open a Pull Request + +### Pre-Push Hook + +There is a pre-push hook present in ```.githooks/pre-push.sh```. This can be symlinked to ```.git/hooks/pre-push```. + +This will ensure that the same checks that will be run in CI are run when you push.