nvimdev / indentmini.nvim

A minimal and blazing fast indentline plugin
MIT License
171 stars 12 forks source link

support for tab indentation #19

Closed AyushRawal closed 3 months ago

AyushRawal commented 3 months ago

issue #15

glepnir commented 3 months ago

give it a test seems like works well thanks :)

glepnir commented 3 months ago

for noexpand file it works well but like vim file it has tab and space both then this patch not works well

image
AyushRawal commented 3 months ago

I am unable to reproduce this. Can you share some code for me test on?

glepnir commented 3 months ago

because of modeline . eg vim source code when i develop the feature of vim i notice that. just git clone https://github.com/vim/vim

in C file the header is set tabstop to 8 shiftwidth to 4 noexpandtab (modeline)

AyushRawal commented 3 months ago

this somewhat fixes it:

@@ -96,6 +96,7 @@ local function on_line(_, _, bufnr, row)
     local bot_indent = bot_row >= 0 and find_in_snapshot(bot_row + 1) or 0
     indent = math.max(top_indent, bot_indent)
   end
+  if not vim.o.expandtab then cache.shiftwidth = vim.o.tabstop end
   for i = 1, indent - 1, cache.shiftwidth do
     local col = i - 1
     local level = math.floor(col / cache.shiftwidth) + 1

for doing this properly instead of saving shiftwidth, it should be step, whose value depends on expandtab

AyushRawal commented 3 months ago

this should be correct solution I think:

@@ -72,7 +72,7 @@ local function find_row(row, curindent, direction, render)
   return INVALID
 end

-local function current_line_range(winid, shiftw)
+local function current_line_range(winid, step)
   local row = api.nvim_win_get_cursor(winid)[1] - 1
   local indent, _ = find_in_snapshot(row + 1)
   if indent == 0 then
@@ -80,7 +80,7 @@ local function current_line_range(winid, shiftw)
   end
   local top_row = find_row(row, indent, UP, false)
   local bot_row = find_row(row, indent, DOWN, false)
-  return top_row, bot_row, math.floor(indent / shiftw)
+  return top_row, bot_row, math.floor(indent / step)
 end

 local function on_line(_, _, bufnr, row)
@@ -96,9 +96,9 @@ local function on_line(_, _, bufnr, row)
     local bot_indent = bot_row >= 0 and find_in_snapshot(bot_row + 1) or 0
     indent = math.max(top_indent, bot_indent)
   end
-  for i = 1, indent - 1, cache.shiftwidth do
+  for i = 1, indent - 1, cache.step do
     local col = i - 1
-    local level = math.floor(col / cache.shiftwidth) + 1
+    local level = math.floor(col / cache.step) + 1
     local higroup = 'IndentLine'
     if row > cache.reg_srow and row < cache.reg_erow and level == cache.cur_inlevel then
       higroup = 'IndentLineCurrent'
@@ -126,9 +126,10 @@ local function on_win(_, winid, bufnr, toprow, botrow)
   end
   api.nvim_win_set_hl_ns(winid, ns)
   cache.leftcol = vim.fn.winsaveview().leftcol
-  cache.shiftwidth = get_shiftw_value(bufnr)
+  cache.step = get_shiftw_value(bufnr)
+  if not vim.o.expandtab then cache.step = vim.o.tabstop end
   cache.count = api.nvim_buf_line_count(bufnr)
-  cache.reg_srow, cache.reg_erow, cache.cur_inlevel = current_line_range(winid, cache.shiftwidth)
+  cache.reg_srow, cache.reg_erow, cache.cur_inlevel = current_line_range(winid, cache.step)
   for i = toprow, botrow do
     cache.snapshot[i + 1] = { get_indent_lnum(i + 1), line_is_empty(i + 1) }
   end

you would need the C function to get tabstop value.
Also, I don't think the case of mixed tabs and spaces can be fully addressed without ballooning up this plugin and using treesitter.

glepnir commented 3 months ago

now should work thanks