Skip to content

Commit 1754bb9

Browse files
committed
fix: fix #106 get rows_indent by treesitter logic bug
1 parent 31ddd8c commit 1754bb9

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

lua/hlchunk/mods/indent/init.lua

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,18 @@ function IndentMod:render(range)
8282
}
8383
local char_num = #self.conf.chars
8484
local style_num = #self.meta.hl_name_list
85+
local leftcol = self.meta.leftcol
86+
local sw = self.meta.shiftwidth
8587
local render_info = {}
8688
for lnum = range.start, range.finish do
8789
local blankLen = self.meta.cache:get(range.bufnr, lnum)
88-
local render_char_num, offset, shadow_char_num =
89-
indentHelper.calc(blankLen, self.meta.leftcol, self.meta.shiftwidth)
90+
local render_char_num, offset, shadow_char_num = indentHelper.calc(blankLen, leftcol, sw)
9091
for i = 1, render_char_num do
9192
local char = self.conf.chars[(i - 1 + shadow_char_num) % char_num + 1]
9293
local style = self.meta.hl_name_list[(i - 1 + shadow_char_num) % style_num + 1]
9394
table.insert(render_info, {
9495
lnum = lnum,
95-
virt_text_win_col = offset + self.meta.leftcol + (i - 1) * self.meta.shiftwidth,
96+
virt_text_win_col = offset + leftcol + (i - 1) * sw,
9697
virt_text = { { char, style } },
9798
})
9899
end

lua/hlchunk/utils/indentHelper.lua

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
local fn = vim.fn
21
local cFunc = require("hlchunk.utils.cFunc")
32

43
-- get the virtual indent of the given line
@@ -24,6 +23,7 @@ local indentHelper = {}
2423
---@return number render_num, number offset, number shadowed_num return the render char number and the start index of the
2524
-- first render char, the last is shadowed char number
2625
function indentHelper.calc(blank, leftcol, sw)
26+
blank = blank or ""
2727
local blankLen = type(blank) == "string" and #blank or blank --[[@as number]]
2828
if blankLen - leftcol <= 0 or sw <= 0 then
2929
return 0, 0, 0
@@ -68,18 +68,17 @@ local function get_rows_indent_by_treesitter(range)
6868
end
6969

7070
local bufnr = range.bufnr
71-
for i = range.start, range.finish, -1 do
72-
rows_indent[i] = vim.api.nvim_buf_call(bufnr, function()
73-
local indent = ts_indent.get_indent(i + 1)
74-
if indent == -1 then
75-
indent = fn.indent(i + 1)
76-
if indent == 0 and cFunc.get_line_len(bufnr, i) == 0 then
77-
indent = get_virt_indent(bufnr, i)
78-
end
79-
end
80-
---@diagnostic disable-next-line: redundant-return-value
81-
return indent
71+
for i = range.start, range.finish, 1 do
72+
local t1 = vim.api.nvim_buf_call(bufnr, function()
73+
return ts_indent.get_indent(i + 1)
8274
end)
75+
local t2 = cFunc.get_indent(bufnr, i)
76+
local line_len = cFunc.get_line_len(bufnr, i)
77+
local indent = math.min(t1, t2)
78+
if indent == 0 and line_len == 0 then
79+
indent = get_virt_indent(bufnr, i)
80+
end
81+
rows_indent[i] = indent
8382
end
8483

8584
return indentHelper.ROWS_INDENT_RETCODE.OK, rows_indent

0 commit comments

Comments
 (0)