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

autoindent during typing un-indents "fi" too far #15

Closed
aswild opened this issue Apr 18, 2019 · 2 comments
Closed

autoindent during typing un-indents "fi" too far #15

aswild opened this issue Apr 18, 2019 · 2 comments

Comments

@aswild
Copy link
Contributor

aswild commented Apr 18, 2019

Regression from ef2e050 which changed to using the searchpair function.

This seems to happen whenever I type fi to end an if statement that's indented.

1 if a; then
2     b
3     if c; then
4         d
5     fi
6 fi

When I type the fi on line 5, it's un-indented back to column 1 (matching the if on line 1) rather than to column 5 (matching the if on line 3).

When typing the fi on line 5, the cursor is in the column with the i character and the backwards searchpair finds the if on line 1 rather than line 3. This is because since the cursor is after the fi, it's found and assumed to be a nested if/fi pair.

The searchpair documentation suggests When searching backwards and {end} is more than one character, it may be useful to put "\zs" at the end of the pattern...
In my tests, it looks like this works, though I'm not totally clear what the \zs does. Somehow it's moving where the start of the pattern is so that the searchpair is able to consider the fi right before cursor not included in the first match.

Here's a patch which I believe fixes the issue.

diff --git a/indent/sh.vim b/indent/sh.vim
index 520d3ee..de88663 100644
--- a/indent/sh.vim
+++ b/indent/sh.vim
@@ -127,7 +127,7 @@ function! GetShIndent()
   " Current line is a endif line, so get indent from start of "if condition" line
   " TODO: should we do the same for other "end" lines?
   if curline =~ '^\s*\%(fi\);\?\s*\%(#.*\)\=$'
-    let previous_line = searchpair('\<if\>', '', '\<fi\>', 'bnW')
+    let previous_line = searchpair('\<if\>', '', '\<fi\>\zs', 'bnW')
     if previous_line > 0
       let ind = indent(previous_line)
     endif
@chrisbra
Copy link
Owner

ah, sounds reasonable. Can you please create a PR? I'll try to come up with a test case then. Thanks!

aswild added a commit to aswild/vim-sh-indent that referenced this issue Apr 27, 2019
Regression from ef2e050 "Better indenting for nested if sections" which
changed to using searchpair() instead of search().

When typing "fi", the cursor is after the start of the \<fi\> match, so
vim un-indents back to the next outer if/fi block rather than the
expected block.

The help for searchpair() suggests "When searching backwards and {end} is
more than one character, it may be useful to put "\zs" at the end of the
pattern, so that when the cursor is inside a match with the end it finds
the matching start." Follow this suggestion to restore the expected
behavior.

Fixes: chrisbra#15
@aswild
Copy link
Contributor Author

aswild commented Apr 27, 2019

PR is up. I'm not sure how to write a test case for insert-mode stuff

aswild added a commit to aswild/linuxfiles that referenced this issue Jul 1, 2019
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 a pull request may close this issue.

2 participants