Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FindFirstUnbalancedParen can cause incorrect indention if using noet #2

Open
suokko opened this issue Sep 5, 2017 · 1 comment
Open
Assignees

Comments

@suokko
Copy link

suokko commented Sep 5, 2017

If noet is enabled and I try to use multiline function parameters indention counts characters without expanding tabs to shiftwidth. I made an quick local fix but I'm not exactly sure if that is completely correct.

diff --git a/after/indent/lua.vim b/after/indent/lua.vim
index 5f18744..4c32d8e 100755
--- a/after/indent/lua.vim
+++ b/after/indent/lua.vim
@@ -216,6 +216,18 @@ function! LuaGetPrevLines()
   return s:GetPrevLines()
 endfunction
 
+function s:GetIndentWidth(line, end_idx)
+  let result = 0
+  for pos in range(a:end_idx)
+    if a:line[pos] == "\t"
+      let result += &shiftwidth
+    else
+      let result += 1
+    endif
+  endfor
+  return result
+endfunction
+
 " Tries the best effort to the find the opening '(' which marks a multi line
 " expression. However, sometimes it well balanced, meaning there is not such
 " opening locally, or such an opening would give too much indent (immediate
@@ -255,7 +267,7 @@ function! s:FindFirstUnbalancedParen(lines)
           if match(line, '\v^.+\(.*<function>' ) > -1
             return s:GetStringIndent(line) + &shiftwidth
           else
-            return i + 1
+            return s:GetIndentWidth(line, i) + 1
           endif
         endif
       endif
@raymond-w-ko raymond-w-ko self-assigned this Sep 13, 2017
@idbrii
Copy link

idbrii commented Mar 13, 2022

Here's a test case:

Previous indent:

f = function(a,
	     b)
	print(a)
	g = function(a,
	      b)
	end
end

This patch now indents to:

f = function(a,
	     b)
	print(a)
	g = function(a,
		     b)
	end
end

(noet sw=8 ts=8)

idbrii added a commit to idbrii/vim-lua-indent that referenced this issue Mar 13, 2022
Cleaned up version of suokko's fix from:
raymond-w-ko#2
(Renaming and making the new func more like GetStringIndent.)

We must \t as &shiftwidth instead of a single char. Can't use
GetStringIndent because it stops when it hits non whitespace and making
it support both cases is awkward.

With `noet sw=8 ts=8` old behaviour:

f = function(a,
	     b)
	print(a)
	g = function(a,
	      b)
	end
end

New behaviour fixes indent for g:

f = function(a,
	     b)
	print(a)
	g = function(a,
		     b)
	end
end
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

No branches or pull requests

3 participants