Skip to content

Commit 6211f93

Browse files
committed
feat: adjust indent when incorrect indent
1 parent bc6b808 commit 6211f93

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

lua/indentmini/init.lua

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,16 @@ local function make_snapshot(lnum)
9898
end
9999

100100
local indent = is_empty and 0 or get_indent_lnum(lnum)
101+
-- adjust indent
102+
if not is_empty and not context.mixup and (indent % context.step) ~= 0 then
103+
local col = api.nvim_win_get_cursor(0)[2]
104+
if col > 0 and not api.nvim_get_current_line():sub(1, col):find('%w') then
105+
indent = indent
106+
local n = math.floor((indent - context.step) / context.step)
107+
indent = context.step + n * context.step
108+
end
109+
end
110+
101111
if is_empty then
102112
local prev_lnum = lnum - 1
103113
while prev_lnum >= 1 do
@@ -174,7 +184,7 @@ local function out_current_range(row)
174184
and (row < context.range_srow or row > context.range_erow)
175185
end
176186

177-
local function find_current_range(currow_indent)
187+
local function find_current_range(currow_indent, currow)
178188
local curlevel = math.ceil(currow_indent / context.tabstop) -- for mixup
179189
local range_fn = function(indent, empty, row)
180190
local level = math.ceil(indent / context.tabstop)
@@ -256,8 +266,7 @@ local function on_win(_, winid, bufnr, toprow, botrow)
256266
context.softtabstop = vim.bo[bufnr].softtabstop
257267
context.win_width = api.nvim_win_get_width(winid)
258268
context.mixup = context.is_tab and context.tabstop > context.softtabstop
259-
local ok = pcall(treesitter.get_parser, bufnr)
260-
context.has_ts = ok
269+
context.has_ts = pcall(treesitter.get_parser, bufnr)
261270
for i = toprow, botrow do
262271
make_snapshot(i + 1)
263272
end
@@ -267,7 +276,7 @@ local function on_win(_, winid, bufnr, toprow, botrow)
267276
local pos = api.nvim_win_get_cursor(winid)
268277
context.currow = pos[1] - 1
269278
context.curcol = pos[2]
270-
find_current_range(find_in_snapshot(context.currow + 1).indent)
279+
find_current_range(find_in_snapshot(context.currow + 1).indent, context.currow)
271280
end
272281

273282
return {

0 commit comments

Comments
 (0)