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

refactor(perf): move rendering to single hot-loop in ui #24

Merged
merged 1 commit into from
Dec 18, 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
61 changes: 2 additions & 59 deletions lua/hawtkeys/score.lua
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ end

---@param str string
---@return table
local function process_string(str)
local function find_matches(str)
local combinations = generate_combos(str)
local scores = {}

Expand Down Expand Up @@ -150,68 +150,11 @@ local function process_string(str)
return sortedScores
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

---@param str string
---@return table
local function scoreTable(str)
-- local results = utils.top5(process_string(str))
local results = process_string(str)
local resultTable = {}
for _, data in ipairs(results) do
table.insert(
resultTable,
"Key: "
.. highlight_desc(str, data.combo)
.. "<leader>"
.. data.combo
.. " - Hawt Score: "
.. data.score
)
if
data.already_mapped ~= nil
and data.already_mapped.rhs ~= nil
and data.already_mapped.from_file ~= nil
then
table.insert(
resultTable,
"Already mapped: " .. tostring(data.already_mapped.rhs)
)
table.insert(
resultTable,
"In File" .. data.already_mapped.from_file
)
end
end
return resultTable
end

local function reset_already_used_keys()
already_used_keys = nil
end

return {
ScoreTable = scoreTable,
ScoreTable = find_matches,
ResetAlreadyUsedKeys = reset_already_used_keys,
}
88 changes: 77 additions & 11 deletions lua/hawtkeys/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,93 @@ local function create_win(enter, opts)
return win, buf
end

M.search = function(text)
local returnText = Hawtkeys.ScoreTable(text)
vim.api.nvim_buf_set_lines(ResultBuf, 0, -1, false, returnText)
---@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

--loop lines and hilight if already mapped:
for i, line in ipairs(returnText) do
if string.match(line, "^Already mapped:.*") then
M.search = function(text)
local results = Hawtkeys.ScoreTable(text)

-- track line count separately because we insert 1-3 lines
-- per iteration
local line_count = 0
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
)
line_count = line_count + 1
local already_mapped = false
if
data.already_mapped ~= nil
and data.already_mapped.rhs ~= nil
and data.already_mapped.from_file ~= nil
then
already_mapped = true
line_count = line_count + 2
table.insert(
lines,
"Already mapped: " .. tostring(data.already_mapped.rhs)
)
table.insert(lines, "In File" .. data.already_mapped.from_file)
end
vim.api.nvim_buf_set_lines(
ResultBuf,
i == 1 and 0 or -1,
-1,
true,
lines
)

if already_mapped then
local hl = "ErrorMsg"
vim.api.nvim_buf_add_highlight(
ResultBuf,
-1,
hl,
line_count - 3,
0,
-1
)
vim.api.nvim_buf_add_highlight(
ResultBuf,
-1,
"ErrorMsg",
i - 1,
hl,
line_count - 2,
0,
-1
)
vim.api.nvim_buf_add_highlight(ResultBuf, -1, "ErrorMsg", i, 0, -1)
vim.api.nvim_buf_add_highlight(
ResultBuf,
-1,
"ErrorMsg",
i - 2,
hl,
line_count - 1,
0,
-1
)
Expand Down
Loading