From 3a3c9a3c8ba3fb14121ef5c50a87173dcb969416 Mon Sep 17 00:00:00 2001 From: daangoossens22 <62295482+daangoossens22@users.noreply.github.com> Date: Fri, 28 Jan 2022 16:36:42 +0100 Subject: [PATCH] feat (keymaps): add configuration options (#1703) --- doc/telescope.txt | 6 ++++++ lua/telescope/builtin/init.lua | 2 ++ lua/telescope/builtin/internal.lua | 24 +++++++++++++++++------- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/doc/telescope.txt b/doc/telescope.txt index 835e66a..ba864e7 100644 --- a/doc/telescope.txt +++ b/doc/telescope.txt @@ -1240,6 +1240,12 @@ builtin.keymaps({opts}) *builtin.keymaps()* Parameters: ~ {opts} (table) options to pass to the picker + Options: ~ + {modes} (table) a list of short-named keymap modes to search + (default: { "n", "i", "c", "x" }) + {show_plug} (boolean) if true, the keymaps for which the lhs contains + "" are also shown (default: true) + builtin.filetypes({opts}) *builtin.filetypes()* Lists all available filetypes, sets currently open buffer's filetype to diff --git a/lua/telescope/builtin/init.lua b/lua/telescope/builtin/init.lua index de5a689..dc094b5 100644 --- a/lua/telescope/builtin/init.lua +++ b/lua/telescope/builtin/init.lua @@ -314,6 +314,8 @@ builtin.registers = require_on_exported_call("telescope.builtin.internal").regis --- Lists normal mode keymappings, runs the selected keymap on `` ---@param opts table: options to pass to the picker +---@field modes table: a list of short-named keymap modes to search (default: { "n", "i", "c", "x" }) +---@field show_plug boolean: if true, the keymaps for which the lhs contains "" are also shown (default: true) builtin.keymaps = require_on_exported_call("telescope.builtin.internal").keymaps --- Lists all available filetypes, sets currently open buffer's filetype to selected filetype in Telescope on `` diff --git a/lua/telescope/builtin/internal.lua b/lua/telescope/builtin/internal.lua index 4765e60..8e1c9c4 100644 --- a/lua/telescope/builtin/internal.lua +++ b/lua/telescope/builtin/internal.lua @@ -952,18 +952,28 @@ end -- TODO: make filtering include the mapping and the action internal.keymaps = function(opts) - local modes = { "n", "i", "c" } - local keymaps_table = {} + opts.modes = vim.F.if_nil(opts.modes, { "n", "i", "c", "x" }) + opts.show_plug = vim.F.if_nil(opts.show_plug, true) + local keymap_encountered = {} -- used to make sure no duplicates are inserted into keymaps_table + local keymaps_table = {} local max_len_lhs = 0 - for _, mode in pairs(modes) do - local function extract_keymaps(keymaps) - for _, keymap in pairs(keymaps) do - table.insert(keymaps_table, keymap) - max_len_lhs = math.max(max_len_lhs, string.len(keymap.lhs or "")) + + -- helper function to populate keymaps_table and determine max_len_lhs + local function extract_keymaps(keymaps) + for _, keymap in pairs(keymaps) do + local keymap_key = keymap.buffer .. keymap.mode .. keymap.lhs -- should be distinct for every keymap + if not keymap_encountered[keymap_key] then + keymap_encountered[keymap_key] = true + if opts.show_plug or not string.find(keymap.lhs, "") then + table.insert(keymaps_table, keymap) + max_len_lhs = math.max(max_len_lhs, #utils.display_termcodes(keymap.lhs)) + end end end + end + for _, mode in pairs(opts.modes) do local global = vim.api.nvim_get_keymap(mode) local buf_local = vim.api.nvim_buf_get_keymap(0, mode) extract_keymaps(global)