diff --git a/lua/hawtkeys/tests/set_maps.lua b/lua/hawtkeys/tests/set_maps.lua index b766215..6077737 100644 --- a/lua/hawtkeys/tests/set_maps.lua +++ b/lua/hawtkeys/tests/set_maps.lua @@ -1,30 +1,48 @@ -vim.api.nvim_set_keymap('n', '1', ':lua print("1 pressed")', { noremap = true, silent = true }) -vim.keymap.set('n', '2', ':lua print("2 pressed")', { noremap = true, silent = true }) +vim.api.nvim_set_keymap( + "n", + "1", + ':lua print("1 pressed")', + { noremap = true, silent = true } +) +vim.keymap.set( + "n", + "2", + ':lua print("2 pressed")', + { noremap = true, silent = true } +) local normalMap = function(lhs, rhs) - vim.api.nvim_set_keymap('n', lhs, rhs, { noremap = true, silent = true }) + vim.api.nvim_set_keymap("n", lhs, rhs, { noremap = true, silent = true }) end -normalMap('3', ':lua print("3 pressed")') +normalMap("3", ':lua print("3 pressed")') local kmap = vim.api -kmap.nvim_set_keymap('n', '4', ':lua print("4 pressed")', { noremap = true, silent = true }) +kmap.nvim_set_keymap( + "n", + "4", + ':lua print("4 pressed")', + { noremap = true, silent = true } +) local nmap = vim.api.nvim_set_keymap -nmap('n', '5', ':lua print("5 pressed")', { noremap = true, silent = true }) +nmap( + "n", + "5", + ':lua print("5 pressed")', + { noremap = true, silent = true } +) -local whichkey = require('which-key') +local whichkey = require("which-key") whichkey.register({ - [''] = { - ['6'] = { ':lua print("6 pressed")', 'Print 6' }, - }, + [""] = { + ["6"] = { ':lua print("6 pressed")', "Print 6" }, + }, +})({ + "plugin/example", + keys = { + { "7", ":lua print('7 pressed" }, + }, }) - -{ "plugin/example", - keys = { -{ "7", ":lua print('7 pressed" }, - } -} - diff --git a/lua/hawtkeys/ts.lua b/lua/hawtkeys/ts.lua index 9525dd0..45a9264 100644 --- a/lua/hawtkeys/ts.lua +++ b/lua/hawtkeys/ts.lua @@ -6,7 +6,6 @@ local config = require("hawtkeys") local ts = require("nvim-treesitter.compat") local tsQuery = require("nvim-treesitter.query") - ---@alias vimModes 'n' | 'x' | 'v' | 'i' ---@alias setMethods 'dot_index_expression' | 'index_expression' | 'function_call' @@ -18,14 +17,40 @@ local tsQuery = require("nvim-treesitter.query") ---@field optsIndex number|nil ---@field method setMethods - ---@type { [string] : keyMapArgs } local keyMapSet = { - ["vim.keymap.set"] = { modeIndex = 1, lhsIndex = 2, rhsIndex = 3, optsIndex = 4, method = "dot_index_expression" }, --method 1 - ["vim.api.nvim_set_keymap"] = { modeIndex = 1, lhsIndex = 2, rhsIndex = 3, optsIndex = 4, method = 'dot_index_expression' }, --method 2 - ["normalMap"] = { modeIndex = 'n', lhsIndex = 1, rhsIndex = 2, method = 'function_call' }, --method 3 - ["kmap.nvim_set_keymap"] = { modeIndex = 1, lhsIndex = 2, rhsIndex = 3, method = 'dot_index_expression' }, --method 4 - ["nmap"] = { modeIndex = 'n', lhsIndex = 1, rhsIndex = 2, method = 'function_call' } -- method 5 + ["vim.keymap.set"] = { + modeIndex = 1, + lhsIndex = 2, + rhsIndex = 3, + optsIndex = 4, + method = "dot_index_expression", + }, --method 1 + ["vim.api.nvim_set_keymap"] = { + modeIndex = 1, + lhsIndex = 2, + rhsIndex = 3, + optsIndex = 4, + method = "dot_index_expression", + }, --method 2 + ["normalMap"] = { + modeIndex = "n", + lhsIndex = 1, + rhsIndex = 2, + method = "function_call", + }, --method 3 + ["kmap.nvim_set_keymap"] = { + modeIndex = 1, + lhsIndex = 2, + rhsIndex = 3, + method = "dot_index_expression", + }, --method 4 + ["nmap"] = { + modeIndex = "n", + lhsIndex = 1, + rhsIndex = 2, + method = "function_call", + }, -- method 5 } ---@type table @@ -84,7 +109,7 @@ local function return_field_data(node, indexData, targetData, file_content) if success then result = result:gsub("[\n\r]", "") --remove surrounding quotes - result = result:gsub("^\"(.*)\"$", "%1") + result = result:gsub('^"(.*)"$', "%1") --remove single quotes result = result:gsub("^'(.*)'$", "%1") return result @@ -102,7 +127,6 @@ local function find_files(dir) return files end - ---@param file_path string ---@return table local function find_maps_in_file(file_path) @@ -121,23 +145,47 @@ local function find_maps_in_file(file_path) local tsKemaps = {} -- TODO: This currently doesnt always work, as the options for helper functions are different, -- need to use TS to resolve it back to a native keymap - local dot_index_expression_query = ts.parse_query( - "lua", - build_dot_index_expression_query(keyMapSet) - ) - for match in tsQuery.iter_prepared_matches(dot_index_expression_query, tree, file_content, 0, -1) do + local dot_index_expression_query = + ts.parse_query("lua", build_dot_index_expression_query(keyMapSet)) + for match in + tsQuery.iter_prepared_matches( + dot_index_expression_query, + tree, + file_content, + 0, + -1 + ) + do for type, node in pairs(match) do if type == "args" then - local parent = vim.treesitter.get_node_text(node.node:parent():child(0), file_content) + local parent = vim.treesitter.get_node_text( + node.node:parent():child(0), + file_content + ) local mapDef = keyMapSet[parent] ---@type string - local mode = return_field_data(node.node, mapDef, "modeIndex", file_content) + local mode = return_field_data( + node.node, + mapDef, + "modeIndex", + file_content + ) ---@type string - local lhs = return_field_data(node.node, mapDef, "lhsIndex", file_content) + local lhs = return_field_data( + node.node, + mapDef, + "lhsIndex", + file_content + ) ---@type string - local rhs = return_field_data(node.node, mapDef, "rhsIndex", file_content) + local rhs = return_field_data( + node.node, + mapDef, + "rhsIndex", + file_content + ) local buf_local = false local opts_arg = node.node:child(mapDef.optsIndex) -- the opts table arg of `vim.keymap.set` is optional, only @@ -163,24 +211,48 @@ local function find_maps_in_file(file_path) end end - local function_call_query = ts.parse_query( - "lua", - build_function_call_query(keyMapSet) - ) + local function_call_query = + ts.parse_query("lua", build_function_call_query(keyMapSet)) - for match in tsQuery.iter_prepared_matches(function_call_query, tree, file_content, 0, -1) do + for match in + tsQuery.iter_prepared_matches( + function_call_query, + tree, + file_content, + 0, + -1 + ) + do for expCap, node in pairs(match) do if expCap == "args" then - local parent = vim.treesitter.get_node_text(node.node:parent():child(0), file_content) + local parent = vim.treesitter.get_node_text( + node.node:parent():child(0), + file_content + ) local mapDef = keyMapSet[parent] ---@type string - local mode = return_field_data(node.node, mapDef, "modeIndex", file_content) + local mode = return_field_data( + node.node, + mapDef, + "modeIndex", + file_content + ) ---@type string - local lhs = return_field_data(node.node, mapDef, "lhsIndex", file_content) + local lhs = return_field_data( + node.node, + mapDef, + "lhsIndex", + file_content + ) ---@type string - local rhs = return_field_data(node.node, mapDef, "rhsIndex", file_content) + local rhs = return_field_data( + node.node, + mapDef, + "rhsIndex", + file_content + ) local buf_local = false local opts_arg = node.node:child(mapDef.optsIndex) -- the opts table arg of `vim.keymap.set` is optional, only