Skip to content

Commit

Permalink
refactor(dupes): refactor to return a key object (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
tris203 authored Jan 5, 2024
1 parent 7e75492 commit f9366ac
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 120 deletions.
10 changes: 4 additions & 6 deletions lua/hawtkeys/duplicates.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ local tsSearch = require("hawtkeys.ts")

---@class HawtkeysDuplicatesData
---@field key string
---@field file1 string
---@field file2 string
---
---TODO: Make this return a HawtkeysKeyMapData instead of strings
---@field map1 HawtkeysKeyMapData
---@field map2 HawtkeysKeyMapData

---@return HawtkeysDuplicatesData[]
function M.show_duplicates()
Expand All @@ -18,8 +16,8 @@ function M.show_duplicates()
---@type HawtkeysDuplicatesData
local object = {
key = index,
file1 = data[1].from_file,
file2 = data[2].from_file,
map1 = data[1],
map2 = data[2],
}
table.insert(resultTable, object)
end
Expand Down
4 changes: 2 additions & 2 deletions lua/hawtkeys/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,8 @@ M.show_dupes = function()
local dupes = showDuplicates.show_duplicates()
local pattern = "%s : %s"
for i, data in ipairs(dupes) do
local filename1 = utils.reduceHome(data.file1)
local filename2 = utils.reduceHome(data.file2)
local filename1 = utils.reduceHome(data.map1.from_file)
local filename2 = utils.reduceHome(data.map2.from_file)
local line = pattern:format(filename1, filename2)

local l2 = data.key
Expand Down
111 changes: 0 additions & 111 deletions tests/hawtkeys/dupes_spec.lua

This file was deleted.

1 change: 1 addition & 0 deletions tests/hawtkeys/example_configs/duplicates2.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vim.keymap.set("x", "<leader>f", ':echo "test2"<CR>', {})
134 changes: 133 additions & 1 deletion tests/hawtkeys/utils_spec.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
local hawtkeys = require("hawtkeys")
local ts = require("hawtkeys.ts")
local utils = require("hawtkeys.utils")
---@diagnostic disable-next-line: undefined-field
local eq = assert.are.same

describe("utils functionality", function()
describe("utils functionality raw", function()
it("can find duplicate lhs in table", function()
local keymaps = {
{ lhs = "a", rhs = "1" },
Expand Down Expand Up @@ -46,3 +48,133 @@ describe("utils functionality", function()
eq("gD", utils.sanitise_modifier_keys("gD"))
end)
end)

describe("Duplicates with Treesitter Read Data", function()
before_each(function()
require("plenary.reload").reload_module("hawtkeys")
ts.reset_scanned_files()
hawtkeys.setup({})
end)

it("should detect duplicates", function()
local keymap = ts.find_maps_in_file(
"tests/hawtkeys/example_configs/duplicates.lua"
)

eq("n", keymap[1].mode)
eq("<leader>t", keymap[1].lhs)

eq("n", keymap[2].mode)
eq("<leader>t", keymap[2].lhs)

eq("x", keymap[3].mode)
eq("<leader>f", keymap[3].lhs)

eq("x", keymap[4].mode)
eq("<leader>f", keymap[4].lhs)

local dupes = utils.find_duplicates(keymap)

eq("n", dupes["<leader>t"][1].mode)
eq("n", dupes["<leader>t"][2].mode)

eq("x", dupes["<leader>f"][1].mode)
eq("x", dupes["<leader>f"][2].mode)
end)

it(
"should detect duplicates with differing casing in the modifiers",
function()
local keymap = ts.find_maps_in_file(
"tests/hawtkeys/example_configs/duplicates_with_casing.lua"
)

eq("n", keymap[1].mode)
eq("<leader>t", keymap[1].lhs)

eq("n", keymap[2].mode)
eq("<Leader>t", keymap[2].lhs)

eq("n", keymap[3].mode)
eq("<C-a>", keymap[3].lhs)

eq("n", keymap[4].mode)
eq("<c-a>", keymap[4].lhs)

local dupes = utils.find_duplicates(keymap)

eq("n", dupes["<leader>t"][1].mode)
eq("n", dupes["<leader>t"][2].mode)

eq("n", dupes["<C-a>"][1].mode)
eq("n", dupes["<c-a>"][1].mode)
end
)

it("should detected duplicates in non-modifier based keymaps", function()
local keymap = ts.find_maps_in_file(
"tests/hawtkeys/example_configs/duplicates_no_modififers.lua"
)

eq("n", keymap[1].mode)
eq("gd", keymap[1].lhs)

eq("n", keymap[2].mode)
eq("gd", keymap[2].lhs)

local dupes = utils.find_duplicates(keymap)

eq("n", dupes["gd"][1].mode)
eq("n", dupes["gd"][2].mode)
end)

it("can detect the filename of a duplicate", function()
local filename = "tests/hawtkeys/example_configs/duplicates.lua"
local keymaps = ts.find_maps_in_file(filename)
local dupes = utils.find_duplicates(keymaps)

eq(filename, dupes["<leader>t"][1].from_file)
end)

it("can detect duplicates in seperate files", function()
local file1 = "tests/hawtkeys/example_configs/duplicates.lua"
local file2 = "tests/hawtkeys/example_configs/duplicates2.lua"
local keymaps1 = ts.find_maps_in_file(file1)
local keymaps2 = ts.find_maps_in_file(file2)

local keymaps = vim.tbl_extend("force", keymaps1, keymaps2)

local dupes = utils.find_duplicates(keymaps)

eq("x", dupes["<leader>f"][1].mode)
eq(file2, dupes["<leader>f"][1].from_file)
eq("x", dupes["<leader>f"][2].mode)
eq(file1, dupes["<leader>f"][2].from_file)
end)

it(
"should detect duplicates with partial mode matches for multi-mode maps",
function()
local keymap = ts.find_maps_in_file(
"tests/hawtkeys/example_configs/multi_mode.lua"
)
eq({ "n", "x" }, keymap[1].mode)
eq("<leader>2", keymap[1].lhs)
eq(':echo "hello"<CR>', keymap[1].rhs)

eq({ "x", "v" }, keymap[2].mode)
eq("<leader>2", keymap[2].lhs)
eq(':echo "hello2"<CR>', keymap[2].rhs)

eq("v", keymap[3].mode)
eq("<leader>2", keymap[3].lhs)
eq(':echo "hello3"<CR>', keymap[3].rhs)

local dupes = utils.find_duplicates(keymap)

eq({ "n", "x" }, dupes["<leader>2"][1].mode)
eq({ "x", "v" }, dupes["<leader>2"][2].mode)
eq("v", dupes["<leader>2"][3].mode)
end
)
end)

0 comments on commit f9366ac

Please sign in to comment.