Skip to content

Commit 3a3c9a3

Browse files
feat (keymaps): add configuration options (#1703)
1 parent 6c275e2 commit 3a3c9a3

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

doc/telescope.txt

+6
Original file line numberDiff line numberDiff line change
@@ -1240,6 +1240,12 @@ builtin.keymaps({opts}) *builtin.keymaps()*
12401240
Parameters: ~
12411241
{opts} (table) options to pass to the picker
12421242

1243+
Options: ~
1244+
{modes} (table) a list of short-named keymap modes to search
1245+
(default: { "n", "i", "c", "x" })
1246+
{show_plug} (boolean) if true, the keymaps for which the lhs contains
1247+
"<Plug>" are also shown (default: true)
1248+
12431249

12441250
builtin.filetypes({opts}) *builtin.filetypes()*
12451251
Lists all available filetypes, sets currently open buffer's filetype to

lua/telescope/builtin/init.lua

+2
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,8 @@ builtin.registers = require_on_exported_call("telescope.builtin.internal").regis
314314

315315
--- Lists normal mode keymappings, runs the selected keymap on `<cr>`
316316
---@param opts table: options to pass to the picker
317+
---@field modes table: a list of short-named keymap modes to search (default: { "n", "i", "c", "x" })
318+
---@field show_plug boolean: if true, the keymaps for which the lhs contains "<Plug>" are also shown (default: true)
317319
builtin.keymaps = require_on_exported_call("telescope.builtin.internal").keymaps
318320

319321
--- Lists all available filetypes, sets currently open buffer's filetype to selected filetype in Telescope on `<cr>`

lua/telescope/builtin/internal.lua

+17-7
Original file line numberDiff line numberDiff line change
@@ -952,18 +952,28 @@ end
952952

953953
-- TODO: make filtering include the mapping and the action
954954
internal.keymaps = function(opts)
955-
local modes = { "n", "i", "c" }
956-
local keymaps_table = {}
955+
opts.modes = vim.F.if_nil(opts.modes, { "n", "i", "c", "x" })
956+
opts.show_plug = vim.F.if_nil(opts.show_plug, true)
957957

958+
local keymap_encountered = {} -- used to make sure no duplicates are inserted into keymaps_table
959+
local keymaps_table = {}
958960
local max_len_lhs = 0
959-
for _, mode in pairs(modes) do
960-
local function extract_keymaps(keymaps)
961-
for _, keymap in pairs(keymaps) do
962-
table.insert(keymaps_table, keymap)
963-
max_len_lhs = math.max(max_len_lhs, string.len(keymap.lhs or ""))
961+
962+
-- helper function to populate keymaps_table and determine max_len_lhs
963+
local function extract_keymaps(keymaps)
964+
for _, keymap in pairs(keymaps) do
965+
local keymap_key = keymap.buffer .. keymap.mode .. keymap.lhs -- should be distinct for every keymap
966+
if not keymap_encountered[keymap_key] then
967+
keymap_encountered[keymap_key] = true
968+
if opts.show_plug or not string.find(keymap.lhs, "<Plug>") then
969+
table.insert(keymaps_table, keymap)
970+
max_len_lhs = math.max(max_len_lhs, #utils.display_termcodes(keymap.lhs))
971+
end
964972
end
965973
end
974+
end
966975

976+
for _, mode in pairs(opts.modes) do
967977
local global = vim.api.nvim_get_keymap(mode)
968978
local buf_local = vim.api.nvim_buf_get_keymap(0, mode)
969979
extract_keymaps(global)

0 commit comments

Comments
 (0)