Skip to content

Conversation

AyushRawal
Copy link
Contributor

@AyushRawal AyushRawal commented May 29, 2024

issue #15

@glepnir
Copy link
Member

glepnir commented May 30, 2024

give it a test seems like works well thanks :)

@glepnir glepnir merged commit e488a19 into nvimdev:main May 30, 2024
@glepnir
Copy link
Member

glepnir commented May 30, 2024

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

image

@AyushRawal
Copy link
Contributor Author

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

@glepnir
Copy link
Member

glepnir commented May 31, 2024

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
Copy link
Contributor Author

AyushRawal commented May 31, 2024

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
Copy link
Contributor Author

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
Copy link
Member

glepnir commented Jun 1, 2024

now should work thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants