@@ -5,13 +5,16 @@ local ffi, treesitter = require('ffi'), vim.treesitter
55local opt = {
66 only_current = false ,
77 exclude = { ' dashboard' , ' lazy' , ' help' , ' nofile' , ' terminal' , ' prompt' , ' qf' },
8+ exclude_nodetype = { ' string' , ' comment' },
89 config = {
910 virt_text_pos = ' overlay' ,
1011 hl_mode = ' combine' ,
1112 ephemeral = true ,
1213 },
1314}
1415
16+ local enabled = true
17+
1518ffi .cdef ([[
1619 typedef struct {} Error;
1720 typedef int colnr_T;
@@ -229,6 +232,15 @@ local function on_line(_, _, bufnr, row)
229232 if context .is_tab and not context .mixup then
230233 col = level - 1
231234 end
235+ if context .has_ts then
236+ local node = treesitter .get_node ({ bufnr = bufnr , pos = { row , col }, ignore_injections = true })
237+ while node do
238+ if vim .tbl_contains (opt .exclude_nodetype , node :type ()) then
239+ goto continue
240+ end
241+ node = node :parent ()
242+ end
243+ end
232244 if
233245 col >= context .leftcol
234246 and level >= opt .minlevel
@@ -248,10 +260,14 @@ local function on_line(_, _, bufnr, row)
248260 buf_set_extmark (bufnr , ns , row , col , opt .config )
249261 opt .config .virt_text_win_col = nil
250262 end
263+ :: continue::
251264 end
252265end
253266
254267local function on_win (_ , winid , bufnr , toprow , botrow )
268+ if not enabled then
269+ return false
270+ end
255271 if
256272 bufnr ~= api .nvim_get_current_buf ()
257273 or vim .iter (opt .exclude ):find (function (v )
@@ -279,16 +295,67 @@ local function on_win(_, winid, bufnr, toprow, botrow)
279295 local pos = api .nvim_win_get_cursor (winid )
280296 context .currow = pos [1 ] - 1
281297 context .curcol = pos [2 ]
282- find_current_range (find_in_snapshot (context .currow + 1 ).indent , context .currow )
298+ local cur_indent = find_in_snapshot (context .currow + 1 ).indent
299+ local next_indent = (context .currow + 1 < context .count ) and find_in_snapshot (context .currow + 2 ).indent or 0
300+ -- We only want to look backwards if we are closing a block
301+ local line_text = api .nvim_get_current_line ()
302+ local is_closer = line_text :find (' ^%s*[})%]]' ) or line_text :find (' ^%s*end' )
303+ local target_indent = cur_indent
304+ if next_indent > cur_indent then
305+ target_indent = next_indent
306+ elseif is_closer then
307+ local prev_indent = context .currow > 0 and find_in_snapshot (context .currow ).indent or 0
308+ if prev_indent > cur_indent then
309+ target_indent = prev_indent
310+ end
311+ end
312+ find_current_range (target_indent , context .currow )
283313end
284314
285- return {
286- setup = function (conf )
287- conf = conf or {}
288- opt .only_current = conf .only_current or false
289- opt .exclude = vim .list_extend (opt .exclude , conf .exclude or {})
290- opt .config .virt_text = { { conf .char or ' │' } }
291- opt .minlevel = conf .minlevel or 1
292- set_provider (ns , { on_win = on_win , on_line = on_line })
293- end ,
294- }
315+ local M = {}
316+
317+ function M .toggle ()
318+ enabled = not enabled
319+ vim .api .nvim__redraw ({
320+ buf = vim .api .nvim_get_current_buf (),
321+ valid = false
322+ })
323+ end
324+
325+ function M .enable ()
326+ enabled = true
327+ vim .api .nvim__redraw ({
328+ buf = vim .api .nvim_get_current_buf (),
329+ valid = false
330+ })
331+ end
332+
333+ function M .disable ()
334+ enabled = false
335+ vim .api .nvim__redraw ({
336+ buf = vim .api .nvim_get_current_buf (),
337+ valid = false
338+ })
339+ end
340+
341+ --- @param conf table | nil IndentMini config
342+ function M .setup (conf )
343+ conf = conf or {}
344+ enabled = conf .enabled ~= false
345+ opt .only_current = conf .only_current or false
346+ opt .exclude = vim .list_extend (opt .exclude , conf .exclude or {})
347+ opt .exclude_nodetype = vim .list_extend (opt .exclude_nodetype , conf .exclude_nodetype or {})
348+ opt .config .virt_text = { { conf .char or ' │' } }
349+ opt .minlevel = conf .minlevel or 1
350+ set_provider (ns , { on_win = on_win , on_line = on_line })
351+
352+ if conf .key then
353+ vim .keymap .set (' n' , conf .key , ' <Cmd>IndentToggle<CR>' , {
354+ desc = ' Toggle indent guides' ,
355+ noremap = true ,
356+ silent = true ,
357+ })
358+ end
359+ end
360+
361+ return M
0 commit comments