Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: cleaner 'all' menu with extmarks and nice mode lists #25

Merged
merged 1 commit into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
37 changes: 36 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,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 = "<unknown>"
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()
Expand Down
Loading