From 6067d114030c7c4a92f3b2566b7976bbe8401dc7 Mon Sep 17 00:00:00 2001 From: Will Hopkins Date: Mon, 18 Dec 2023 16:07:45 -0800 Subject: [PATCH] feat: cleaner 'all' menu with extmarks and nice mode lists --- lua/hawtkeys/show_all.lua | 16 +--------------- lua/hawtkeys/ts.lua | 23 +++++++++++++++++++++-- lua/hawtkeys/ui.lua | 37 ++++++++++++++++++++++++++++++++++++- 3 files changed, 58 insertions(+), 18 deletions(-) diff --git a/lua/hawtkeys/show_all.lua b/lua/hawtkeys/show_all.lua index bee6bed..321e9c8 100644 --- a/lua/hawtkeys/show_all.lua +++ b/lua/hawtkeys/show_all.lua @@ -3,21 +3,7 @@ local tsSearch = require("hawtkeys.ts") ---@return table function M.show_all() - local allKeys = tsSearch.get_all_keymaps() - local resultTable = {} - for _, data in ipairs(allKeys) do - table.insert( - resultTable, - tostring(data.lhs) - .. " " - .. tostring(data.rhs) - .. " (" - .. tostring(data.mode) - .. ") - " - .. tostring(data.from_file) - ) - end - return resultTable + return tsSearch.get_all_keymaps() end return M diff --git a/lua/hawtkeys/ts.lua b/lua/hawtkeys/ts.lua index ee83510..c8d9686 100644 --- a/lua/hawtkeys/ts.lua +++ b/lua/hawtkeys/ts.lua @@ -61,7 +61,7 @@ local function find_maps_in_file(file_path) end if not buf_local then - table.insert(tsKemaps, { + local map = { mode = vim.treesitter .get_node_text(node.node:child(1), file_content) :gsub("^%s*(['\"])(.*)%1%s*$", "%2") @@ -75,7 +75,26 @@ local function find_maps_in_file(file_path) :gsub("^%s*(['\"])(.*)%1%s*$", "%2") :gsub("[\n\r]", ""), from_file = file_path, - }) + } + + if map.mode:match("^%s*{.*},?.*$") then + local mode = {} + for i, child in + vim.iter(node.node:child(1):iter_children()) + :enumerate() + do + if i % 2 == 0 then + local ty = vim.treesitter + .get_node_text(child, file_content) + :gsub("['\"]", "") + :gsub("[\n\r]", "") + vim.print("type: " .. vim.inspect(ty)) + table.insert(mode, ty) + end + end + map.mode = table.concat(mode, ", ") + end + table.insert(tsKemaps, map) end end end diff --git a/lua/hawtkeys/ui.lua b/lua/hawtkeys/ui.lua index c92d677..964d516 100644 --- a/lua/hawtkeys/ui.lua +++ b/lua/hawtkeys/ui.lua @@ -3,6 +3,8 @@ local Hawtkeys = require("hawtkeys.score") local ShowAll = require("hawtkeys.show_all") local showDuplicates = require("hawtkeys.duplicates") +local ns = vim.api.nvim_create_namespace("hawtkeys") + local ResultWin local ResultBuf local SearchWin @@ -192,7 +194,40 @@ M.show_all = function() height = height, footer = "Current Keybindings", }) - vim.api.nvim_buf_set_lines(ResultBuf, 0, -1, false, ShowAll.show_all()) + local all = ShowAll.show_all() + local pattern = "%s (%s) - %s" + for i, data in ipairs(all) do + local filename = data.from_file:gsub(vim.env.HOME, "~") + local line = pattern:format(data.lhs, data.mode, filename) + + local offset_mode = #data.lhs + 2 + local offset_file = offset_mode + #data.mode + 2 + + local l2 = data.rhs + if l2 == nil or l2 == "" then + l2 = "" + end + vim.api.nvim_buf_set_lines( + ResultBuf, + i == 1 and 0 or -1, + -1, + false, + { line } + ) + -- highlight the filename + vim.api.nvim_buf_add_highlight( + ResultBuf, + -1, + "Comment", + i - 1, + offset_file, + -1 + ) + -- mapping rhs as extmark so the cursor skips over it + vim.api.nvim_buf_set_extmark(ResultBuf, ns, i - 1, 0, { + virt_lines = { { { l2, "Function" } } }, + }) + end end M.show_dupes = function()