Skip to content

Allow to use different chars to render the tab character #97

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 5 additions & 2 deletions docs/en/blank.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -27,6 +28,8 @@ chars = {
},
```

`tab_chars` is used to configure what char to render the `<Tab>`, 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
Expand Down
7 changes: 5 additions & 2 deletions docs/zh_CN/blank.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 关闭

Expand All @@ -27,6 +28,8 @@ chars = {
},
```

`tab_chars` 是一个 lua 表,其中的字符用来指示如何渲染 `<Tab>` 字符,它和`chars`类似。

`style` 是一个 RGB 字符串或者一个表,如果是表,他将会使用不同颜色来渲染 blank

`exclude_filetype` 是 support_filetype 的反面,用来控制在哪些文件类型不渲染 blank,默认的 exclude_filetypes 可以在 [default config](../../lua/hlchunk/utils/filetype.lua) 中找到
Expand Down
22 changes: 19 additions & 3 deletions lua/hlchunk/mods/blank.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ local blank_mod = BaseMod:new({
chars = {
"․",
},
tab_chars = {},
style = {
api.nvim_get_hl(0, { name = "Whitespace" }),
},
Expand All @@ -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)
elseif char ~= "" then
text = text .. "." .. (" "):rep(shiftwidth - 1)
end
end
text = text:sub(win_info.leftcol + 1)

Expand All @@ -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
Expand Down