Skip to content

Commit

Permalink
feat: duplicate checks
Browse files Browse the repository at this point in the history
  • Loading branch information
tris203 committed Dec 17, 2023
1 parent 5bd3e1a commit 74132a6
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 11 deletions.
22 changes: 22 additions & 0 deletions lua/hawtkeys/duplicates.lua
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions lua/hawtkeys/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions lua/hawtkeys/ts.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {}
Expand Down
28 changes: 28 additions & 0 deletions lua/hawtkeys/ui.lua
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
38 changes: 27 additions & 11 deletions lua/hawtkeys/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 74132a6

Please sign in to comment.