Skip to content

Commit

Permalink
feat: allow filtering keymaps displayed
Browse files Browse the repository at this point in the history
  • Loading branch information
jokajak committed Jul 10, 2023
1 parent 6a18036 commit 1960571
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 40 deletions.
21 changes: 21 additions & 0 deletions lua/keyseer/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ KeySeer.config = {

-- TODO: Represent modifier toggling in highlights
include_modifiers = false,
-- Boolean to include built in keymaps in display
include_builtin_keymaps = false,
-- Boolean to include global keymaps in display
include_global_keymaps = true,
-- Boolean to include buffer keymaps in display
include_buffer_keymaps = true,

-- Configuration for ui:
-- - `height` and `width` are maximum dimensions.
Expand Down Expand Up @@ -103,6 +109,21 @@ KeySeer.config = {

-- KeySeer functionality ========================================
--
---Open the keyseer ui
---@param mode? string the neovim mode to show keymaps for
---@param bufnr? buffer the buffer for buffer specific keymaps
KeySeer.show = function(mode, bufnr)
bufnr = bufnr or vim.api.nvim_get_current_buf()
mode = mode or "n"

local UI = require("keyseer.ui")
UI.show("home", mode, bufnr)
end

KeySeer.close = function()
local UI = require("keyseer.ui")
UI:close()
end

-- Helper data ================================================================
-- Module default config
Expand Down
24 changes: 15 additions & 9 deletions lua/keyseer/keymaps/init.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
--- This file contains the code for parsing keymaps
local D = require("keyseer.util.debug")
local Utils = require("keyseer.utils")
local config = require("keyseer").config
local Config = require("keyseer").config
local BuiltInKeyMaps = require("keyseer.keymaps.builtin_keymaps")
local Keypress = require("keyseer.keymaps.keypress")

Expand Down Expand Up @@ -36,13 +36,19 @@ end
---@param mode string? Optional mode for which to get keymaps
---@returns table
function Keymaps:process_keymaps(bufnr, mode)
mode = mode or config.initial_mode
local buffer_keymaps = bufnr and vim.api.nvim_buf_get_keymap(bufnr, mode) or {}
local global_keymaps = vim.api.nvim_get_keymap(mode)
local preset_keymaps = BuiltInKeyMaps[mode]
self:add_keymaps(preset_keymaps)
self:add_keymaps(global_keymaps)
self:add_keymaps(buffer_keymaps)
mode = mode or Config.initial_mode
if Config.include_builtin_keymaps then
local preset_keymaps = BuiltInKeyMaps[mode]
self:add_keymaps(preset_keymaps)
end
if Config.include_global_keymaps then
local global_keymaps = vim.api.nvim_get_keymap(mode)
self:add_keymaps(global_keymaps)
end
if Config.include_buffer_keymaps then
local buffer_keymaps = bufnr and vim.api.nvim_buf_get_keymap(bufnr, mode) or {}
self:add_keymaps(buffer_keymaps)
end
end

---@class KeySeerKeyMap
Expand Down Expand Up @@ -179,6 +185,6 @@ function Keymaps:push(keypress)
end

function Keymaps:pop()
self.current_node = table.remove(self.stack)
self.current_node = vim.F.if_nil(table.remove(self.stack), self.root)
end
return Keymaps
17 changes: 12 additions & 5 deletions lua/keyseer/ui/colors.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ M.colors = {
Special = "@punctuation.special",
Button = "CursorLine",
ButtonActive = "Visual",
KeycapKeymap = "CursorLine",
KeycapMultipleKeymaps = "Search",
KeycapKeymapAndPrefix = "Visual",
KeycapKeymapsAndPrefix = "IncSearch",
KeycapPrefix = "Bold",
}

---@type table<string,table>
M.keycap_colors = {
KeycapKeymap = { link = "CursorLine", default = true },
KeycapMultipleKeymaps = { link = "Conceal", default = true },
KeycapKeymapAndPrefix = { link = "Visual", default = true },
KeycapKeymapsAndPrefix = { link = "IncSearch", default = true },
KeycapPrefix = { link = "NormalFloat", default = true },
}

M.did_setup = false
Expand All @@ -25,6 +29,9 @@ function M.set_hl()
for hl_group, link in pairs(M.colors) do
vim.api.nvim_set_hl(0, "KeySeer" .. hl_group, { link = link, default = true })
end
for hl_group, opts in pairs(M.keycap_colors) do
vim.api.nvim_set_hl(0, "KeySeer" .. hl_group, opts)
end
end

function M.setup()
Expand Down
23 changes: 8 additions & 15 deletions lua/keyseer/ui/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
local Popup = require("keyseer.ui.popup")
local Render = require("keyseer.ui.render")
local UIConfig = require("keyseer.ui.config")
local Utils = require("keyseer.utils")
local D = require("keyseer.util.debug")
local Config = require("keyseer").config
local Keymaps = require("keyseer.keymaps")

Expand Down Expand Up @@ -48,7 +48,8 @@ end
---@param mode? string The neovim mode for keymaps
---@param bufnr? integer The buffer for keymaps
function M.show(pane, mode, bufnr)
M.ui = M.visible() and M.ui or M.create()
bufnr = vim.F.if_nil(bufnr, vim.api.nvim_get_current_buf())
M.ui = M.visible() and M.ui or M.create(bufnr)

if pane then
M.ui.state.pane = pane
Expand All @@ -58,16 +59,17 @@ function M.show(pane, mode, bufnr)
M.ui.state.mode = mode
end

if bufnr then
M.ui.state.bufnr = bufnr
end
D.log("UI", "Setting bufnr to " .. bufnr)
M.ui.state.bufnr = bufnr

M.ui:update()
end

---@return KeySeerUI
function M.create()
---@param bufnr? buffer The buffer to retrieve keymaps
function M.create(bufnr)
local self = setmetatable({}, { __index = setmetatable(M, { __index = Popup }) })
bufnr = vim.F.if_nil(bufnr, vim.api.nvim_get_current_buf())
---@cast self KeySeerUI
Popup.init(self, {})

Expand Down Expand Up @@ -127,15 +129,6 @@ function M.create()
end
end)

-- go backwards in the key press tree
self:on_key(UIConfig.keys.back, function()
-- only makes sense on the home pane
if self.state.pane == "home" then
Utils.notify("Going back")
self.state.keymaps:pop()
self:update()
end
end)
return self
end

Expand Down
13 changes: 12 additions & 1 deletion lua/keyseer/ui/panes/configuration.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
-- Configuration
--
-- Current mode: Normal
local Config = require("keyseer").config

-- Render configurations
local M = {}
Expand All @@ -12,7 +13,17 @@ local M = {}
function M.render(ui)
ui.render:append("Configuration", "KeySeerH2"):nl():nl()

ui.render:append("Current configurations", "KeySeerH2"):nl()
ui.render:append("Current configurations", "KeySeerH2"):nl():nl()

ui.render:append("Current buffer: ", "KeySeerH2")
ui.render:append(tostring(ui.state.bufnr)):nl()

ui.render:append("Show builtin keymaps: ", "KeySeerH2")
ui.render:append(tostring(Config.include_builtin_keymaps)):nl()
ui.render:append("Show global keymaps: ", "KeySeerH2")
ui.render:append(tostring(Config.include_global_keymaps)):nl()
ui.render:append("Show buffer keymaps: ", "KeySeerH2")
ui.render:append(tostring(Config.include_buffer_keymaps)):nl()
end

return M
26 changes: 16 additions & 10 deletions lua/keyseer/ui/panes/home.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,11 @@
-- │ <CTRL> │ <SUPER> │ <ALT> │ <SPACE> │ <ALT> │ <CTRL> │
-- └────────┴─────────┴───────┴──────────────────┴───────┴────────┘
local D = require("keyseer.util.debug")
local Config = require("keyseer").config
local Keymaps = require("keyseer.keymaps")
local Utils = require("keyseer.utils")
local UIConfig = require("keyseer.ui.config")
-- Render help
local M = {
count = 0,
modifiers = {},
saved_keymaps = {},
}

function M.render(ui)
Expand All @@ -42,19 +39,28 @@ end

---Update keymaps when entering the pane
function M.on_enter(ui)
for _, keypress in pairs(ui.state.current_keymaps) do
vim.keymap.set("n", "g" .. keypress, function()
ui.state.keymaps:push(keypress)
ui:update()
end, { buffer = ui.buf })
end
-- for _, keypress in pairs(ui.state.current_keymaps) do
-- vim.keymap.set("n", "g" .. keypress, function()
-- ui.state.keymaps:push(keypress)
-- ui:update()
-- end, { buffer = ui.buf })
-- end
-- go backwards in the key press tree
vim.keymap.set("n", UIConfig.keys.back, function()
-- only makes sense on the home pane
ui.state.keymaps:pop()
ui:update()
end, { buffer = ui.buf })
end

---Update keymaps when exiting the pane
function M.on_exit(ui)
for _, keypress in pairs(ui.state.current_keymaps) do
D.log("UI", "deleting keymap for %s", keypress)
vim.keymap.del("n", "g" .. keypress, { buffer = ui.buf })
end
ui.state.current_keymaps = {}
vim.keymap.del("n", UIConfig.keys.back, { buffer = ui.buf })
end

return M

0 comments on commit 1960571

Please sign in to comment.