From d541e0d6e00a0d89f21e1b0e6fe92c736ac0f83b Mon Sep 17 00:00:00 2001 From: ADoyle Date: Mon, 7 Nov 2022 00:42:29 +0800 Subject: [PATCH] feat: the parameter "map" of attach_mappings can be list of modes to create mapping on multiple modes (#2220) --- doc/telescope.txt | 5 ++++ lua/telescope/builtin/__git.lua | 35 ++++++++-------------------- lua/telescope/builtin/__internal.lua | 17 +++++--------- lua/telescope/mappings.lua | 21 +++++++++++++---- 4 files changed, 37 insertions(+), 41 deletions(-) diff --git a/doc/telescope.txt b/doc/telescope.txt index b573d65..23f87bf 100644 --- a/doc/telescope.txt +++ b/doc/telescope.txt @@ -1775,6 +1775,11 @@ ordered from the lowest priority to the highest priority. map("i", "asdf", function(_prompt_bufnr) print "You typed asdf" end) + + map({"i", "n"}, "", function(_prompt_bufnr) + print "You typed " + end) + -- needs to return true if you want to map default_mappings and -- false if not return true diff --git a/lua/telescope/builtin/__git.lua b/lua/telescope/builtin/__git.lua index 7c4f28b..cc40503 100644 --- a/lua/telescope/builtin/__git.lua +++ b/lua/telescope/builtin/__git.lua @@ -71,12 +71,9 @@ git.commits = function(opts) sorter = conf.file_sorter(opts), attach_mappings = function(_, map) actions.select_default:replace(actions.git_checkout) - map("i", "m", actions.git_reset_mixed) - map("n", "m", actions.git_reset_mixed) - map("i", "s", actions.git_reset_soft) - map("n", "s", actions.git_reset_soft) - map("i", "h", actions.git_reset_hard) - map("n", "h", actions.git_reset_hard) + map({ "i", "n" }, "m", actions.git_reset_mixed) + map({ "i", "n" }, "s", actions.git_reset_soft) + map({ "i", "n" }, "h", actions.git_reset_hard) return true end, }) @@ -293,23 +290,12 @@ git.branches = function(opts) sorter = conf.file_sorter(opts), attach_mappings = function(_, map) actions.select_default:replace(actions.git_checkout) - map("i", "", actions.git_track_branch) - map("n", "", actions.git_track_branch) - - map("i", "", actions.git_rebase_branch) - map("n", "", actions.git_rebase_branch) - - map("i", "", actions.git_create_branch) - map("n", "", actions.git_create_branch) - - map("i", "", actions.git_switch_branch) - map("n", "", actions.git_switch_branch) - - map("i", "", actions.git_delete_branch) - map("n", "", actions.git_delete_branch) - - map("i", "", actions.git_merge_branch) - map("n", "", actions.git_merge_branch) + map({ "i", "n" }, "", actions.git_track_branch) + map({ "i", "n" }, "", actions.git_rebase_branch) + map({ "i", "n" }, "", actions.git_create_branch) + map({ "i", "n" }, "", actions.git_switch_branch) + map({ "i", "n" }, "", actions.git_delete_branch) + map({ "i", "n" }, "", actions.git_merge_branch) return true end, }) @@ -368,8 +354,7 @@ git.status = function(opts) end, } - map("i", "", actions.git_staging_toggle) - map("n", "", actions.git_staging_toggle) + map({ "i", "n" }, "", actions.git_staging_toggle) return true end, }) diff --git a/lua/telescope/builtin/__internal.lua b/lua/telescope/builtin/__internal.lua index 3684518..9b6931d 100644 --- a/lua/telescope/builtin/__internal.lua +++ b/lua/telescope/builtin/__internal.lua @@ -198,8 +198,7 @@ internal.pickers = function(opts) opts["initial_mode"] = cached_pickers[selection_index].initial_mode internal.resume(opts) end) - map("i", "", actions.remove_selected_picker) - map("n", "", actions.remove_selected_picker) + map({ "i", "n" }, "", actions.remove_selected_picker) return true end, }) @@ -556,10 +555,8 @@ internal.command_history = function(opts) sorter = conf.generic_sorter(opts), attach_mappings = function(_, map) - map("i", "", actions.set_command_line) - map("n", "", actions.set_command_line) - map("n", "", actions.edit_command_line) - map("i", "", actions.edit_command_line) + map({ "i", "n" }, "", actions.set_command_line) + map({ "i", "n" }, "", actions.edit_command_line) -- TODO: Find a way to insert the text... it seems hard. -- map('i', '', actions.insert_value, { expr = true }) @@ -588,10 +585,8 @@ internal.search_history = function(opts) sorter = conf.generic_sorter(opts), attach_mappings = function(_, map) - map("i", "", actions.set_search_line) - map("n", "", actions.set_search_line) - map("n", "", actions.edit_search_line) - map("i", "", actions.edit_search_line) + map({ "i", "n" }, "", actions.set_search_line) + map({ "i", "n" }, "", actions.edit_search_line) -- TODO: Find a way to insert the text... it seems hard. -- map('i', '', actions.insert_value, { expr = true }) @@ -1091,7 +1086,7 @@ internal.registers = function(opts) sorter = conf.generic_sorter(opts), attach_mappings = function(_, map) actions.select_default:replace(actions.paste_register) - map("i", "", actions.edit_register) + map({ "i", "n" }, "", actions.edit_register) return true end, diff --git a/lua/telescope/mappings.lua b/lua/telescope/mappings.lua index e5b23bc..45fad81 100644 --- a/lua/telescope/mappings.lua +++ b/lua/telescope/mappings.lua @@ -112,6 +112,11 @@ --- map("i", "asdf", function(_prompt_bufnr) --- print "You typed asdf" --- end) +--- +--- map({"i", "n"}, "", function(_prompt_bufnr) +--- print "You typed " +--- end) +--- --- -- needs to return true if you want to map default_mappings and --- -- false if not --- return true @@ -284,12 +289,18 @@ end mappings.apply_keymap = function(prompt_bufnr, attach_mappings, buffer_keymap) local applied_mappings = { n = {}, i = {} } - local map = function(mode, key_bind, key_func, opts) - mode = string.lower(mode) - local key_bind_internal = a.nvim_replace_termcodes(key_bind, true, true, true) - applied_mappings[mode][key_bind_internal] = true + local map = function(modes, key_bind, key_func, opts) + if type(modes) == "string" then + modes = { modes } + end - telescope_map(prompt_bufnr, mode, key_bind, key_func, opts) + for _, mode in pairs(modes) do + mode = string.lower(mode) + local key_bind_internal = a.nvim_replace_termcodes(key_bind, true, true, true) + applied_mappings[mode][key_bind_internal] = true + + telescope_map(prompt_bufnr, mode, key_bind, key_func, opts) + end end if attach_mappings then