Skip to content

Commit

Permalink
fix(nvcheatsheet): fix unicode highlighting and spacing of nvcheatsheet
Browse files Browse the repository at this point in the history
As the nvim_buf_add_highlight and other functions use byte indexing instead
of character indexing, I have fixed the whitespace length to not include the
extra bytes of unicode and also for highlighting I had to get the correct byte
index for both text and space lines of cheatsheet using builtin functions.

Additional testing is required to see all cases are handled.
I have checked for emojis and glyphs working as of now.

fixes NvChad#74
  • Loading branch information
kingavatar committed Apr 7, 2023
1 parent 1b6ac5a commit bd7a7cd
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions lua/nvchad_ui/cheatsheet/grid.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ return function()
if type(mappings) == "table" then
for keybind, mappingInfo in pairs(mappings) do
if mappingInfo[2] then
column_width = column_width > #mappingInfo[2] + #prettify_Str(keybind) and column_width
or #mappingInfo[2] + #prettify_Str(keybind)
column_width = column_width > vim.fn.strdisplaywidth(mappingInfo[2] .. prettify_Str(keybind))
and column_width
or vim.fn.strdisplaywidth(mappingInfo[2] .. prettify_Str(keybind))
end
end
end
Expand All @@ -78,12 +79,12 @@ return function()
for mode, modeMappings in pairs(section) do
local mode_suffix = (mode == "n" or mode == "plugin") and "" or string.format(" (%s) ", mode)
local card_name = name .. mode_suffix
local padding_left = math.floor((column_width - #card_name) / 2)
local padding_left = math.floor((column_width - vim.fn.strdisplaywidth(card_name)) / 2)

-- center the heading
card_name = string.rep(" ", padding_left)
.. card_name
.. string.rep(" ", column_width - #card_name - padding_left)
.. string.rep(" ", column_width - vim.fn.strdisplaywidth(card_name) - padding_left)

card_headings[#card_headings + 1] = card_name

Expand All @@ -93,7 +94,7 @@ return function()
if type(modeMappings) == "table" then
for keystroke, mapping_info in pairs(modeMappings) do
if mapping_info[2] then
local whitespace_len = column_width - 4 - #(prettify_Str(keystroke) .. mapping_info[2])
local whitespace_len = column_width - 4 - vim.fn.strdisplaywidth(prettify_Str(keystroke) .. mapping_info[2])
local pretty_mapping = mapping_info[2] .. string.rep(" ", whitespace_len) .. prettify_Str(keystroke)

cards[card_name][#cards[card_name] + 1] = " " .. pretty_mapping .. " "
Expand Down Expand Up @@ -242,8 +243,23 @@ return function()

-- highlight mappings & one line after it
elseif string.match(columns[column_i][i], "%s+") ~= columns[column_i][i] then
vim.api.nvim_buf_add_highlight(buf, nvcheatsheet, "NvChSection", i + #ascii_header - 1, col_start, col_end)
vim.api.nvim_buf_add_highlight(buf, nvcheatsheet, "NvChSection", i + #ascii_header, col_start, col_end)
local line = vim.api.nvim_buf_get_lines(buf, i + #ascii_header - 1, i + #ascii_header + 1, false)
vim.api.nvim_buf_add_highlight(
buf,
nvcheatsheet,
"NvChSection",
i + #ascii_header - 1,
vim.fn.stridx(line[1], columns[column_i][i], col_start),
vim.fn.stridx(line[1], columns[column_i][i], col_start) + vim.fn.strlen(columns[column_i][i])
)
vim.api.nvim_buf_add_highlight(
buf,
nvcheatsheet,
"NvChSection",
i + #ascii_header,
vim.fn.byteidx(line[2], col_start),
vim.fn.byteidx(line[2], col_start) + column_width
)
end
end
end
Expand Down

0 comments on commit bd7a7cd

Please sign in to comment.