cohama / lexima.vim

Auto close parentheses and repeat by dot dot dot...
995 stars 46 forks source link

Endwise rules behave unexpectedly #119

Closed lyokha closed 2 years ago

lyokha commented 2 years ago

Say, I edit some Vim script which contain

if 1 && 1
    echo 'Ok'
endif

Then I decide to edit the second condition in this contrived if by replacing this by a very log condition.

if 1 && some_var == 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
    echo 'Ok'
endif

Now I see that the if-line is too long and decide to break it, say, before &&. After that I am getting

if 1

endif\ && some_var == 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
    echo 'Ok'
endif

because the endwise rule for Vim filetype has triggered and put unexpected endif before the second part of the if condition. Notice that the endwise rule worked not-endwisely as the cursor was in the middle of the if condition before pressing the <Enter>.

The solution is simple: let the endwise rule trigger only when the cursor is at the end of line, here is my patch that makes the endwise rule work nicely.

diff --git a/autoload/lexima/endwise_rule.vim b/autoload/lexima/endwise_rule.vim
index 4f11f91..d52d71e 100644
--- a/autoload/lexima/endwise_rule.vim
+++ b/autoload/lexima/endwise_rule.vim
@@ -7,7 +7,7 @@ function! lexima#endwise_rule#make()
   let rules = []
   " vim
   for at in ['fu', 'fun', 'func', 'funct', 'functi', 'functio', 'function', 'if', 'wh', 'whi', 'whil', 'while', 'for', 'try']
-    call add(rules, s:make_rule('^\s*' . at . '\>.*\%#', 'end' . at, 'vim', []))
+    call add(rules, s:make_rule('^\s*' . at . '\>.*\%#$', 'end' . at, 'vim', []))
   endfor

   for at in ['aug', 'augroup']

It seems that other endwise rules require this treatment too.

cohama commented 2 years ago

Thank you for your contribution. I have just fixed.