From b2622096610b7fe601e3e5a7cf11894c34586e29 Mon Sep 17 00:00:00 2001 From: Sir AppSec Date: Fri, 6 Sep 2024 05:18:17 +0300 Subject: [PATCH] shotcut:ToggleTerm which-key --- lua/hacker-helper.lua | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/lua/hacker-helper.lua b/lua/hacker-helper.lua index 2f8f083..18a9b76 100644 --- a/lua/hacker-helper.lua +++ b/lua/hacker-helper.lua @@ -5,8 +5,9 @@ local module = require("hacker-helper.module") ---@field opt string Your config option ---@field keys table Key mappings local config = { + prefix = "r", -- Default prefix for Hacker Helper keys = { - run_exec = "re", -- Default mapping for executing in terminal + run_exec = "e", -- Default mapping for executing in terminal }, opt = "Hello!", } @@ -24,16 +25,35 @@ M.setup = function(args) -- Merge user configuration with defaults M.config = vim.tbl_deep_extend("force", M.config, args or {}) - -- Set up the key mappings, respecting user configuration - vim.api.nvim_set_keymap('n', M.config.keys.run_exec, - ':lua require("hacker-helper.module").exec_line_or_selection_in_term()', { noremap = true, silent = true }) - vim.api.nvim_set_keymap('v', M.config.keys.run_exec, - ':lua require("hacker-helper.module").exec_line_or_selection_in_term()', { noremap = true, silent = true }) + -- Set the key mappings dynamically based on the prefix + local full_run_exec_mapping = M.config.prefix .. M.config.keys.run_exec + vim.api.nvim_set_keymap( + "n", + full_run_exec_mapping, + ':lua require("hacker-helper.module").exec_line_or_selection_in_term()', + { noremap = true, silent = true } + ) + vim.api.nvim_set_keymap( + "v", + full_run_exec_mapping, + ':lua require("hacker-helper.module").exec_line_or_selection_in_term()', + { noremap = true, silent = true } + ) + + -- Register a description for the r prefix with which-key (if available) + if package.loaded["which-key"] then + require("which-key").register({ + ["x"] = { name = "Hacker Helper" }, + ["xe"] = { + ":lua require('hacker-helper.module').exec_line_or_selection_in_term()", + "Run Command in Terminal", + }, + }) + end end M.hello = function() return module.my_first_function(M.config.opt) end - return M