vim-patch:9.1.2016: cindent wrong indentation after do-while loop (#37087)

Problem:  At "if(0) do if(0); while(0); else", else should be aligned
          with outer if, but is aligned with inner if.
Solution: In function find_match, ignore "if" and "else" inside a
          do-while loop, when looking for "if". (Anttoni Erkkilä)

closes: vim/vim#19004

9d661b057e

Co-authored-by: Anttoni Erkkilä <anttoni.erkkila@protonmail.com>
This commit is contained in:
zeertzjq
2025-12-24 09:43:42 +08:00
committed by GitHub
parent 855fda03aa
commit 2a2c366a3e
2 changed files with 63 additions and 19 deletions

View File

@@ -3712,10 +3712,12 @@ static int find_match(int lookfor, linenr_T ourscope)
continue;
}
look = cin_skipcomment(get_cursor_line_ptr());
// When looking for if, we ignore "if" and "else" in a deeper do-while loop.
if (!(lookfor == LOOKFOR_IF && whilelevel)) {
// if it was an "else" (that's not an "else if")
// then we need to go back to another if, so
// increment elselevel
look = cin_skipcomment(get_cursor_line_ptr());
if (cin_iselse(look)) {
mightbeif = cin_skipcomment(look + 4);
if (!cin_isif(mightbeif)) {
@@ -3724,15 +3726,7 @@ static int find_match(int lookfor, linenr_T ourscope)
continue;
}
// if it was a "while" then we need to go back to
// another "do", so increment whilelevel. XXX
if (cin_iswhileofdo(look, curwin->w_cursor.lnum)) {
whilelevel++;
continue;
}
// If it's an "if" decrement elselevel
look = cin_skipcomment(get_cursor_line_ptr());
if (cin_isif(look)) {
elselevel--;
// When looking for an "if" ignore "while"s that
@@ -3741,6 +3735,14 @@ static int find_match(int lookfor, linenr_T ourscope)
whilelevel = 0;
}
}
}
// if it was a "while" then we need to go back to
// another "do", so increment whilelevel. XXX
if (cin_iswhileofdo(look, curwin->w_cursor.lnum)) {
whilelevel++;
continue;
}
// If it's a "do" decrement whilelevel
if (cin_isdo(look)) {

View File

@@ -1106,6 +1106,27 @@ func Test_cindent_1()
b;
}
void func() {
if (0)
do
if (0);
while (0);
else;
}
void func() {
if (0)
do
if (0)
do
if (0)
a();
while (0);
while (0);
else
a();
}
/* end of AUTO */
[CODE]
@@ -2088,6 +2109,27 @@ func Test_cindent_1()
b;
}
void func() {
if (0)
do
if (0);
while (0);
else;
}
void func() {
if (0)
do
if (0)
do
if (0)
a();
while (0);
while (0);
else
a();
}
/* end of AUTO */
[CODE]