Skip to content

Commit 2103a71

Browse files
committed
Allow to use different chars to render the tab character
Signed-off-by: yuguorui <[email protected]>
1 parent 882d1bc commit 2103a71

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

docs/en/blank.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ blank have four configurable items
77
1. enable
88
2. notify
99
3. chars
10-
4. style
11-
5. exclude_filetype
10+
4. tab_chars
11+
5. style
12+
6. exclude_filetype
1213

1314
`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
1415

@@ -27,6 +28,8 @@ chars = {
2728
},
2829
```
2930

31+
`tab_chars` is used to configure what char to render the `<Tab>`, it is a table likes the `chars` table.
32+
3033
`style` is a RGB string or RGB string list, if it is a table, it will choice different color to render different blank (indent)
3134

3235
`exclude_filetype` is opposite of support_filetypes, it is a lua table like this, same as chunk mod

docs/zh_CN/blank.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ blank mod 有四个配置项
77
1. enable
88
2. notify
99
3. chars
10-
4. style
11-
5. exclude_filetype
10+
4. tab_chars
11+
5. style
12+
6. exclude_filetype
1213

1314
`enable` 是用来控制该 mod 是否启动的,如果设置为 false,其所携带的 usercmd 和 autocmd 均不会产生,此时该 mod 关闭
1415

@@ -27,6 +28,8 @@ chars = {
2728
},
2829
```
2930

31+
`tab_chars` 是一个 lua 表,其中的字符用来指示如何渲染 `<Tab>` 字符,它和`chars`类似。
32+
3033
`style` 是一个 RGB 字符串或者一个表,如果是表,他将会使用不同颜色来渲染 blank
3134

3235
`exclude_filetype` 是 support_filetype 的反面,用来控制在哪些文件类型不渲染 blank,默认的 exclude_filetypes 可以在 [default config](../../lua/hlchunk/utils/filetype.lua) 中找到

lua/hlchunk/mods/blank.lua

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ local blank_mod = BaseMod:new({
2121
chars = {
2222
"",
2323
},
24+
tab_chars = {},
2425
style = {
2526
api.nvim_get_hl(0, { name = "Whitespace" }),
2627
},
@@ -38,8 +39,16 @@ function blank_mod:render_line(index, indent)
3839
local render_char_num = math.floor(indent / shiftwidth)
3940
local win_info = fn.winsaveview()
4041
local text = ""
41-
for _ = 1, render_char_num do
42-
text = text .. "." .. (" "):rep(shiftwidth - 1)
42+
43+
local space_tab = ("\t"):rep(vim.o.tabstop)
44+
local line_val = fn.getline(index):gsub("\t", space_tab)
45+
for i = 1, render_char_num do
46+
local char = line_val:sub(shiftwidth * i - 1, shiftwidth * i - 1)
47+
if char == "\t" then
48+
text = text .. ("t") .. (" "):rep(shiftwidth - 1)
49+
else
50+
text = text .. "." .. (" "):rep(shiftwidth - 1)
51+
end
4352
end
4453
text = text:sub(win_info.leftcol + 1)
4554

@@ -50,7 +59,14 @@ function blank_mod:render_line(index, indent)
5059
count = count + 1
5160
local Blank_chars_num = Array:from(self.options.chars):size()
5261
local Blank_style_num = Array:from(self.options.style):size()
53-
local char = self.options.chars[(count - 1) % Blank_chars_num + 1]:rep(shiftwidth)
62+
63+
local char
64+
if self.options.tab_chars and c:match("t") then
65+
char = self.options.tab_chars[(count - 1) % Blank_chars_num + 1]:rep(shiftwidth)
66+
else
67+
char = self.options.chars[(count - 1) % Blank_chars_num + 1]:rep(shiftwidth)
68+
end
69+
5470
local style = "HLBlank" .. tostring((count - 1) % Blank_style_num + 1)
5571
row_opts.virt_text = { { char, style } }
5672
row_opts.virt_text_win_col = i - 1

0 commit comments

Comments
 (0)