From 8d02f4e526c6bf761a215426e189afc10523e8c6 Mon Sep 17 00:00:00 2001 From: Saikiran Reddy <31726036+kingavatar@users.noreply.github.com> Date: Sat, 8 Apr 2023 15:45:50 +0530 Subject: [PATCH] fix(nvcheatsheet): fix unicode highlighting and spacing of nvcheatsheet (#86) * fix(nvcheatsheet): fix unicode highlighting and spacing of nvcheatsheet 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 #74 * fix(nvcheatsheet): handle title highlights as well For Now this will handle spacing and highlights of title and space below as well. But emoji in title is bit tricky to solve for now emojis or glyphs in title should not be allowed. * fix(nvcheatsheet): fix heading nerd font as well I have fixed nerd font icon for heading title as well. I have tried to include emojis but yet they are causing the space highlighting to offset right side. So emoji support yet. --- lua/nvchad_ui/cheatsheet/grid.lua | 70 +++++++++++++++++++++---------- 1 file changed, 47 insertions(+), 23 deletions(-) diff --git a/lua/nvchad_ui/cheatsheet/grid.lua b/lua/nvchad_ui/cheatsheet/grid.lua index b3237701..db50e256 100644 --- a/lua/nvchad_ui/cheatsheet/grid.lua +++ b/lua/nvchad_ui/cheatsheet/grid.lua @@ -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 @@ -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 @@ -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 .. " " @@ -214,36 +215,59 @@ return function() if columns[column_i][i] then -- highlight headings & one line after it if vim.tbl_contains(card_headings, columns[column_i][i]) then - local heading_index = 0 - local heading_end = 0 - - -- find index of starting/ending letter - for char_i = 1, #columns[column_i][i], 1 do - if columns[column_i][i]:sub(char_i, char_i) ~= " " and heading_index == 0 then - heading_index = char_i - elseif columns[column_i][i]:sub(char_i, char_i) ~= " " then - heading_end = char_i - end - end + local lines = vim.api.nvim_buf_get_lines(buf, i + #ascii_header - 1, i + #ascii_header + 1, false) -- highlight area around card heading - 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 - 1, + vim.fn.byteidx(lines[1], col_start), + vim.fn.byteidx(lines[1], col_start) + + column_width + + vim.fn.strlen(columns[column_i][i]) + - vim.fn.strdisplaywidth(columns[column_i][i]) + ) -- highlight card heading & randomize hl groups for colorful colors vim.api.nvim_buf_add_highlight( buf, nvcheatsheet, highlight_groups[math.random(1, #highlight_groups)], i + #ascii_header - 1, - col_start + heading_index - 2, - col_start + heading_end + 1 + vim.fn.stridx(lines[1], vim.trim(columns[column_i][i]), col_start) - 1, + vim.fn.stridx(lines[1], vim.trim(columns[column_i][i]), col_start) + + vim.fn.strlen(vim.trim(columns[column_i][i])) + + 1 + ) + vim.api.nvim_buf_add_highlight( + buf, + nvcheatsheet, + "NvChSection", + i + #ascii_header, + vim.fn.byteidx(lines[2], col_start), + vim.fn.byteidx(lines[2], col_start) + column_width ) - vim.api.nvim_buf_add_highlight(buf, nvcheatsheet, "NvChSection", i + #ascii_header, col_start, col_end) -- 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 lines = 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(lines[1], columns[column_i][i], col_start), + vim.fn.stridx(lines[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(lines[2], col_start), + vim.fn.byteidx(lines[2], col_start) + column_width + ) end end end