diff --git a/lua/hawtkeys/duplicates.lua b/lua/hawtkeys/duplicates.lua index 9ce5d7f..1b36941 100644 --- a/lua/hawtkeys/duplicates.lua +++ b/lua/hawtkeys/duplicates.lua @@ -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() @@ -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 diff --git a/lua/hawtkeys/ui.lua b/lua/hawtkeys/ui.lua index 221522b..20a9396 100644 --- a/lua/hawtkeys/ui.lua +++ b/lua/hawtkeys/ui.lua @@ -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 diff --git a/tests/hawtkeys/dupes_spec.lua b/tests/hawtkeys/dupes_spec.lua deleted file mode 100644 index 4141da9..0000000 --- a/tests/hawtkeys/dupes_spec.lua +++ /dev/null @@ -1,111 +0,0 @@ -local hawtkeys = require("hawtkeys") -local util = require("hawtkeys.utils") -local ts = require("hawtkeys.ts") ----@diagnostic disable-next-line: undefined-field -local eq = assert.are.same - -describe("HawtkeysDupes", 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("t", keymap[1].lhs) - - eq("n", keymap[2].mode) - eq("t", keymap[2].lhs) - - eq("x", keymap[3].mode) - eq("f", keymap[3].lhs) - - eq("x", keymap[4].mode) - eq("f", keymap[4].lhs) - - local dupes = util.find_duplicates(keymap) - - eq("n", dupes["t"][1].mode) - eq("n", dupes["t"][2].mode) - - eq("x", dupes["f"][1].mode) - eq("x", dupes["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("t", keymap[1].lhs) - - eq("n", keymap[2].mode) - eq("t", keymap[2].lhs) - - eq("n", keymap[3].mode) - eq("", keymap[3].lhs) - - eq("n", keymap[4].mode) - eq("", keymap[4].lhs) - - local dupes = util.find_duplicates(keymap) - - eq("n", dupes["t"][1].mode) - eq("n", dupes["t"][2].mode) - - eq("n", dupes[""][1].mode) - eq("n", dupes[""][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 = util.find_duplicates(keymap) - - eq("n", dupes["gd"][1].mode) - eq("n", dupes["gd"][2].mode) - 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("2", keymap[1].lhs) - eq(':echo "hello"', keymap[1].rhs) - - eq({ "x", "v" }, keymap[2].mode) - eq("2", keymap[2].lhs) - eq(':echo "hello2"', keymap[2].rhs) - - eq("v", keymap[3].mode) - eq("2", keymap[3].lhs) - eq(':echo "hello3"', keymap[3].rhs) - - local dupes = util.find_duplicates(keymap) - - eq({ "n", "x" }, dupes["2"][1].mode) - eq({ "x", "v" }, dupes["2"][2].mode) - eq("v", dupes["2"][3].mode) - end - ) -end) diff --git a/tests/hawtkeys/example_configs/duplicates2.lua b/tests/hawtkeys/example_configs/duplicates2.lua new file mode 100644 index 0000000..f59569b --- /dev/null +++ b/tests/hawtkeys/example_configs/duplicates2.lua @@ -0,0 +1 @@ +vim.keymap.set("x", "f", ':echo "test2"', {}) diff --git a/tests/hawtkeys/utils_spec.lua b/tests/hawtkeys/utils_spec.lua index 281e396..f577cae 100644 --- a/tests/hawtkeys/utils_spec.lua +++ b/tests/hawtkeys/utils_spec.lua @@ -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" }, @@ -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("t", keymap[1].lhs) + + eq("n", keymap[2].mode) + eq("t", keymap[2].lhs) + + eq("x", keymap[3].mode) + eq("f", keymap[3].lhs) + + eq("x", keymap[4].mode) + eq("f", keymap[4].lhs) + + local dupes = utils.find_duplicates(keymap) + + eq("n", dupes["t"][1].mode) + eq("n", dupes["t"][2].mode) + + eq("x", dupes["f"][1].mode) + eq("x", dupes["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("t", keymap[1].lhs) + + eq("n", keymap[2].mode) + eq("t", keymap[2].lhs) + + eq("n", keymap[3].mode) + eq("", keymap[3].lhs) + + eq("n", keymap[4].mode) + eq("", keymap[4].lhs) + + local dupes = utils.find_duplicates(keymap) + + eq("n", dupes["t"][1].mode) + eq("n", dupes["t"][2].mode) + + eq("n", dupes[""][1].mode) + eq("n", dupes[""][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["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["f"][1].mode) + eq(file2, dupes["f"][1].from_file) + eq("x", dupes["f"][2].mode) + eq(file1, dupes["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("2", keymap[1].lhs) + eq(':echo "hello"', keymap[1].rhs) + + eq({ "x", "v" }, keymap[2].mode) + eq("2", keymap[2].lhs) + eq(':echo "hello2"', keymap[2].rhs) + + eq("v", keymap[3].mode) + eq("2", keymap[3].lhs) + eq(':echo "hello3"', keymap[3].rhs) + + local dupes = utils.find_duplicates(keymap) + + eq({ "n", "x" }, dupes["2"][1].mode) + eq({ "x", "v" }, dupes["2"][2].mode) + eq("v", dupes["2"][3].mode) + end + ) +end)