From d173740bb49654d755ea1b0018c9a6c6f8689f98 Mon Sep 17 00:00:00 2001 From: daangoossens22 <62295482+daangoossens22@users.noreply.github.com> Date: Fri, 14 Jan 2022 21:13:32 +0100 Subject: [PATCH] feat: make results pane of builtin `keymaps` more readable (#1684) --- lua/telescope/builtin/internal.lua | 27 ++++++++++----------- lua/telescope/make_entry.lua | 38 ++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 15 deletions(-) diff --git a/lua/telescope/builtin/internal.lua b/lua/telescope/builtin/internal.lua index 2120bb6..e0c5f5e 100644 --- a/lua/telescope/builtin/internal.lua +++ b/lua/telescope/builtin/internal.lua @@ -955,30 +955,27 @@ internal.keymaps = function(opts) local modes = { "n", "i", "c" } 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 "")) + end + end + local global = vim.api.nvim_get_keymap(mode) - for _, keymap in pairs(global) do - table.insert(keymaps_table, keymap) - end local buf_local = vim.api.nvim_buf_get_keymap(0, mode) - for _, keymap in pairs(buf_local) do - table.insert(keymaps_table, keymap) - end + extract_keymaps(global) + extract_keymaps(buf_local) end + opts.width_lhs = max_len_lhs + 1 pickers.new(opts, { prompt_title = "Key Maps", finder = finders.new_table { results = keymaps_table, - entry_maker = function(line) - local desc = line.desc or line.rhs or "Lua function" - return { - valid = line ~= "", - value = line, - ordinal = utils.display_termcodes(line.lhs) .. desc, - display = line.mode .. " " .. utils.display_termcodes(line.lhs) .. " " .. desc, - } - end, + entry_maker = opts.entry_maker or make_entry.gen_from_keymaps(opts), }, sorter = conf.generic_sorter(opts), attach_mappings = function(prompt_bufnr) diff --git a/lua/telescope/make_entry.lua b/lua/telescope/make_entry.lua index 2d53248..98129d7 100644 --- a/lua/telescope/make_entry.lua +++ b/lua/telescope/make_entry.lua @@ -693,6 +693,44 @@ function make_entry.gen_from_registers(_) end end +function make_entry.gen_from_keymaps(opts) + local function get_desc(entry) + return entry.desc or entry.rhs or "Lua function" + end + local function get_lhs(entry) + return utils.display_termcodes(entry.lhs) + end + + local displayer = require("telescope.pickers.entry_display").create { + separator = "▏", + items = { + { width = 2 }, + { width = opts.width_lhs }, + { remaining = true }, + }, + } + local make_display = function(entry) + return displayer { + entry.mode, + get_lhs(entry), + get_desc(entry), + } + end + + return function(entry) + return { + mode = entry.mode, + lhs = get_lhs(entry), + desc = get_desc(entry), + -- + valid = entry ~= "", + value = entry, + ordinal = entry.mode .. " " .. get_lhs(entry) .. " " .. get_desc(entry), + display = make_display, + } + end +end + function make_entry.gen_from_highlights() local make_display = function(entry) local display = entry.value