Skip to content

Commit e867862

Browse files
committed
improve blank line check by using treesitter
1 parent 59c2be5 commit e867862

File tree

1 file changed

+11
-59
lines changed

1 file changed

+11
-59
lines changed

lua/indentmini/init.lua

Lines changed: 11 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -52,57 +52,6 @@ local function get_shiftw_value(bufnr)
5252
return get_sw_value(handle)
5353
end
5454

55-
---@paren node TSNode
56-
---@return TSNode?
57-
local function ts_find_indentation_node(node)
58-
local cur_row = node:range()
59-
local current = node
60-
61-
while current do
62-
current = current:parent()
63-
if current then
64-
local row = current:range()
65-
if row < cur_row then
66-
return current
67-
end
68-
end
69-
end
70-
return nil
71-
end
72-
73-
---@paren lnum integer
74-
---@return integer?
75-
local function ts_get_indent(lnum)
76-
local node = treesitter.get_node({ pos = { lnum - 1, 0 } })
77-
if not node then
78-
return
79-
end
80-
local srow = node:range()
81-
if lnum - 1 > srow then
82-
local count = node:named_child_count()
83-
local last_valid_child = nil
84-
for i = 0, count - 1 do
85-
local child = node:named_child(i)
86-
if child then
87-
local child_row = child:range()
88-
if child_row < lnum - 1 then
89-
last_valid_child = child
90-
else
91-
break
92-
end
93-
end
94-
end
95-
if last_valid_child then
96-
return get_indent_lnum(last_valid_child:range() + 1)
97-
end
98-
end
99-
local parent = ts_find_indentation_node(node)
100-
if not parent then
101-
return
102-
end
103-
return get_indent_lnum(parent:range() + 1)
104-
end
105-
10655
-- Bit operations for snapshot packing/unpacking
10756
-- empty(1) | indent(6) | indent_cols(9)
10857
local function pack_snapshot(empty, indent, indent_cols)
@@ -130,11 +79,14 @@ local function make_snapshot(lnum)
13079
local line_text = ffi.string(ml_get(lnum))
13180
local is_empty = #line_text == 0 or only_spaces_or_tabs(line_text)
13281
if is_empty and context.has_ts then
133-
local indent = ts_get_indent(lnum)
134-
if indent then
135-
local packed = pack_snapshot(true, indent, indent)
136-
context.snapshot[lnum] = packed
137-
return unpack_snapshot(packed)
82+
local node = treesitter.get_node({ pos = { lnum - 1, 0 } })
83+
if node then
84+
local rtype = node:tree():root():type()
85+
if node:type() == rtype then
86+
local packed = pack_snapshot(true, 0, 0)
87+
context.snapshot[lnum] = packed
88+
return unpack_snapshot(packed)
89+
end
13890
end
13991
end
14092

@@ -265,7 +217,7 @@ local function on_line(_, _, bufnr, row)
265217
and (not currow_insert or col ~= context.curcol)
266218
then
267219
local row_in_curblock = context.range_srow
268-
and (row > context.range_srow and row < context.range_erow)
220+
and (row > context.range_srow and row <= context.range_erow)
269221
local higroup = row_in_curblock and level == context.cur_inlevel and 'IndentLineCurrent'
270222
or 'IndentLine'
271223
opt.config.virt_text[1][2] = higroup
@@ -297,6 +249,8 @@ local function on_win(_, winid, bufnr, toprow, botrow)
297249
context.softtabstop = vim.bo[bufnr].softtabstop
298250
context.win_width = api.nvim_win_get_width(winid)
299251
context.mixup = context.is_tab and context.tabstop > context.softtabstop
252+
local ok = pcall(treesitter.get_parser, bufnr)
253+
context.has_ts = ok
300254
for i = toprow, botrow do
301255
make_snapshot(i + 1)
302256
end
@@ -306,8 +260,6 @@ local function on_win(_, winid, bufnr, toprow, botrow)
306260
local pos = api.nvim_win_get_cursor(winid)
307261
context.currow = pos[1] - 1
308262
context.curcol = pos[2]
309-
local ok = pcall(treesitter.get_paser, bufnr)
310-
context.has_ts = ok
311263
find_current_range(find_in_snapshot(context.currow + 1).indent)
312264
end
313265

0 commit comments

Comments
 (0)