Skip to content

Commit

Permalink
feat: cleaner 'all' menu with extmarks and nice mode lists
Browse files Browse the repository at this point in the history
  • Loading branch information
willothy committed Dec 19, 2023
1 parent 9a68b37 commit feca905
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 18 deletions.
16 changes: 1 addition & 15 deletions lua/hawtkeys/show_all.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
23 changes: 21 additions & 2 deletions lua/hawtkeys/ts.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand Down
23 changes: 22 additions & 1 deletion lua/hawtkeys/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -192,7 +194,26 @@ 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()
for i, data in ipairs(all) do
local l1 = tostring(data.lhs)
.. " ("
.. (data.mode or "n")
.. ") - "
.. tostring(data.from_file):gsub(vim.env.HOME, "~")
local l2 = tostring(data.rhs or "<unknown>")
vim.api.nvim_buf_set_lines(
ResultBuf,
i == 1 and 0 or -1,
-1,
false,
{ l1 }
)
-- mapping rhs as extmark so the cursor skips over it
vim.api.nvim_buf_set_extmark(ResultBuf, ns, i - 1, 0, {
virt_lines = { { { l2, "Comment" } } },
})
end
end

M.show_dupes = function()
Expand Down

0 comments on commit feca905

Please sign in to comment.