From 2103a711d9410c0414831b875a0ca9192f9e7e3a Mon Sep 17 00:00:00 2001 From: yuguorui Date: Wed, 17 Apr 2024 13:39:49 +0800 Subject: [PATCH 1/2] Allow to use different chars to render the tab character Signed-off-by: yuguorui --- docs/en/blank.md | 7 +++++-- docs/zh_CN/blank.md | 7 +++++-- lua/hlchunk/mods/blank.lua | 22 +++++++++++++++++++--- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/docs/en/blank.md b/docs/en/blank.md index cf75eee..bbabc93 100644 --- a/docs/en/blank.md +++ b/docs/en/blank.md @@ -7,8 +7,9 @@ blank have four configurable items 1. enable 2. notify 3. chars -4. style -5. exclude_filetype +4. tab_chars +5. style +6. exclude_filetype `enable` is used to control whether enable hl_blank, if set it to false, its usercmd and autocmd will not set, so it will not work @@ -27,6 +28,8 @@ chars = { }, ``` +`tab_chars` is used to configure what char to render the ``, it is a table likes the `chars` table. + `style` is a RGB string or RGB string list, if it is a table, it will choice different color to render different blank (indent) `exclude_filetype` is opposite of support_filetypes, it is a lua table like this, same as chunk mod diff --git a/docs/zh_CN/blank.md b/docs/zh_CN/blank.md index ccbb5e4..44a5212 100644 --- a/docs/zh_CN/blank.md +++ b/docs/zh_CN/blank.md @@ -7,8 +7,9 @@ blank mod 有四个配置项 1. enable 2. notify 3. chars -4. style -5. exclude_filetype +4. tab_chars +5. style +6. exclude_filetype `enable` 是用来控制该 mod 是否启动的,如果设置为 false,其所携带的 usercmd 和 autocmd 均不会产生,此时该 mod 关闭 @@ -27,6 +28,8 @@ chars = { }, ``` +`tab_chars` 是一个 lua 表,其中的字符用来指示如何渲染 `` 字符,它和`chars`类似。 + `style` 是一个 RGB 字符串或者一个表,如果是表,他将会使用不同颜色来渲染 blank `exclude_filetype` 是 support_filetype 的反面,用来控制在哪些文件类型不渲染 blank,默认的 exclude_filetypes 可以在 [default config](../../lua/hlchunk/utils/filetype.lua) 中找到 diff --git a/lua/hlchunk/mods/blank.lua b/lua/hlchunk/mods/blank.lua index 887a722..41bd085 100644 --- a/lua/hlchunk/mods/blank.lua +++ b/lua/hlchunk/mods/blank.lua @@ -21,6 +21,7 @@ local blank_mod = BaseMod:new({ chars = { "․", }, + tab_chars = {}, style = { api.nvim_get_hl(0, { name = "Whitespace" }), }, @@ -38,8 +39,16 @@ function blank_mod:render_line(index, indent) local render_char_num = math.floor(indent / shiftwidth) local win_info = fn.winsaveview() local text = "" - for _ = 1, render_char_num do - text = text .. "." .. (" "):rep(shiftwidth - 1) + + local space_tab = ("\t"):rep(vim.o.tabstop) + local line_val = fn.getline(index):gsub("\t", space_tab) + for i = 1, render_char_num do + local char = line_val:sub(shiftwidth * i - 1, shiftwidth * i - 1) + if char == "\t" then + text = text .. ("t") .. (" "):rep(shiftwidth - 1) + else + text = text .. "." .. (" "):rep(shiftwidth - 1) + end end text = text:sub(win_info.leftcol + 1) @@ -50,7 +59,14 @@ function blank_mod:render_line(index, indent) count = count + 1 local Blank_chars_num = Array:from(self.options.chars):size() local Blank_style_num = Array:from(self.options.style):size() - local char = self.options.chars[(count - 1) % Blank_chars_num + 1]:rep(shiftwidth) + + local char + if self.options.tab_chars and c:match("t") then + char = self.options.tab_chars[(count - 1) % Blank_chars_num + 1]:rep(shiftwidth) + else + char = self.options.chars[(count - 1) % Blank_chars_num + 1]:rep(shiftwidth) + end + local style = "HLBlank" .. tostring((count - 1) % Blank_style_num + 1) row_opts.virt_text = { { char, style } } row_opts.virt_text_win_col = i - 1 From 0cdb7bade5b462660d32d4128d50f812c02e98c1 Mon Sep 17 00:00:00 2001 From: yuguorui Date: Wed, 17 Apr 2024 13:42:33 +0800 Subject: [PATCH 2/2] skip render empty lines with blank Users may want to know whether the corresponding line has spaces, but the current implementation unconditionally renders blanks based on indentation. Signed-off-by: yuguorui --- lua/hlchunk/mods/blank.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/hlchunk/mods/blank.lua b/lua/hlchunk/mods/blank.lua index 41bd085..b9a41a6 100644 --- a/lua/hlchunk/mods/blank.lua +++ b/lua/hlchunk/mods/blank.lua @@ -46,7 +46,7 @@ function blank_mod:render_line(index, indent) local char = line_val:sub(shiftwidth * i - 1, shiftwidth * i - 1) if char == "\t" then text = text .. ("t") .. (" "):rep(shiftwidth - 1) - else + elseif char ~= "" then text = text .. "." .. (" "):rep(shiftwidth - 1) end end