Skip to content

Commit

Permalink
feat: highlight search matches and search result score
Browse files Browse the repository at this point in the history
  • Loading branch information
willothy committed Dec 21, 2023
1 parent dedcf53 commit 4e58870
Showing 1 changed file with 67 additions and 32 deletions.
99 changes: 67 additions & 32 deletions lua/hawtkeys/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ local ResultWin
local ResultBuf
local SearchWin

local prompt_extmark

local function create_win(enter, opts)
opts = opts or {}
local wo = opts.win_options or {}
Expand Down Expand Up @@ -45,30 +47,12 @@ local function create_win(enter, opts)
return win, buf
end

---@param str string
---@param combo string
---@return string
local function highlight_desc(str, combo)
-- returns str with the first unmarked occurrence of each letter of combo surrounded by []
local newStr = str:lower()
local marked = {} -- Keep track of characters already marked
for i = 1, #combo do
local char = combo:sub(i, i)
local pos = marked[char] or 1 -- Start searching from the last marked position or from the beginning
pos = newStr:find(char, pos, true) or 0
if pos then
newStr = newStr:sub(1, pos - 1)
.. "["
.. char
.. "]"
.. newStr:sub(pos + 1)
marked[char] = pos + 2 -- Mark this character's position
end
end
return newStr
end

local prompt_extmark
local search_threshold = {
GREAT = 6,
GOOD = 3,
OK = 1,
BAD = 0,
}

M.search = function(text)
text = text or ""
Expand All @@ -80,15 +64,14 @@ M.search = function(text)
for i = 1, #results do
local data = results[i]
local lines = {}
table.insert(
lines,
"Key: "
.. highlight_desc(text, data.combo)
.. "<leader>"
.. data.combo
.. " - Hawt Score: "
.. data.score
local line = string.format(
"Key: %s <leader>%s - Hawt Score: %d",
text,
data.combo,
data.score
)
table.insert(lines, line)

line_count = line_count + 1
local already_mapped = false
if
Expand Down Expand Up @@ -138,6 +121,52 @@ M.search = function(text)
0,
-1
)
else
local newStr = text:lower()
local marked = {} -- Keep track of characters already marked
for idx = 1, #data.combo do
local char = data.combo:sub(idx, idx)
local pos = marked[char] or 1 -- Start searching from the last marked position or from the beginning
pos = newStr:find(char, pos, true) or 0
if marked[char] and marked[char] == pos then
pos = newStr:find(char, pos + 1, true) or 0
end
if pos then
newStr = newStr:sub(1, pos - 1)
.. char
.. newStr:sub(pos + 1)
local hl_col = pos + 5
vim.api.nvim_buf_add_highlight(
ResultBuf,
-1,
"Function",
line_count - (already_mapped and 3 or 1),
hl_col - 1,
hl_col
)
marked[char] = pos -- Mark this character's position
end
end

local score_offset = #line - #tostring(data.score)
local score_hl
if data.score >= search_threshold.GREAT then
score_hl = "HawtkeysMatchGreat"
elseif data.score >= search_threshold.GOOD then
score_hl = "MoreMsg"
elseif data.score >= search_threshold.OK then
score_hl = "WarningMsg"
else
score_hl = "ErrorMsg"
end
vim.api.nvim_buf_add_highlight(
ResultBuf,
-1,
score_hl,
line_count - (already_mapped and 3 or 1),
score_offset,
#line
)
end
end
end
Expand Down Expand Up @@ -210,6 +239,11 @@ M.show = function()
end),
})

-- create highlight for best keymaps
local more_msg = vim.api.nvim_get_hl(0, { name = "MoreMsg" })
more_msg.underline = true
vim.api.nvim_set_hl(0, "HawtkeysMatchGreat", more_msg)

update_search_hint("")
vim.api.nvim_command("startinsert")
end
Expand Down Expand Up @@ -284,6 +318,7 @@ M.hide = function()
if SearchWin and vim.api.nvim_win_is_valid(SearchWin) then
vim.api.nvim_win_close(SearchWin, true)
end
vim.api.nvim_set_hl(0, "HawtkeysMatchGreat", {})
Hawtkeys.ResetAlreadyUsedKeys()
SearchWin = nil
ResultWin = nil
Expand Down

0 comments on commit 4e58870

Please sign in to comment.