Skip to content

Commit 9d661b0

Browse files
aerkkilachrisbra
authored andcommitted
patch 9.1.2016: cindent wrong indentation after do-while loop
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: #19004 Signed-off-by: Anttoni Erkkilä <anttoni.erkkila@protonmail.com> Signed-off-by: Christian Brabandt <cb@256bit.org>
1 parent 60c8705 commit 9d661b0

3 files changed

Lines changed: 66 additions & 19 deletions

File tree

src/cindent.c

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2076,16 +2076,30 @@ find_match(int lookfor, linenr_T ourscope)
20762076
if (theirscope->lnum > ourscope)
20772077
continue;
20782078

2079-
// if it was an "else" (that's not an "else if")
2080-
// then we need to go back to another if, so
2081-
// increment elselevel
20822079
look = cin_skipcomment(ml_get_curline());
2083-
if (cin_iselse(look))
2080+
// When looking for if, we ignore "if" and "else" in a deeper do-while loop.
2081+
if (!(lookfor == LOOKFOR_IF && whilelevel))
20842082
{
2085-
mightbeif = cin_skipcomment(look + 4);
2086-
if (!cin_isif(mightbeif))
2087-
++elselevel;
2088-
continue;
2083+
// if it was an "else" (that's not an "else if")
2084+
// then we need to go back to another if, so
2085+
// increment elselevel
2086+
if (cin_iselse(look))
2087+
{
2088+
mightbeif = cin_skipcomment(look + 4);
2089+
if (!cin_isif(mightbeif))
2090+
++elselevel;
2091+
continue;
2092+
}
2093+
2094+
// If it's an "if" decrement elselevel
2095+
if (cin_isif(look))
2096+
{
2097+
elselevel--;
2098+
// When looking for an "if" ignore "while"s that
2099+
// get in the way.
2100+
if (elselevel == 0 && lookfor == LOOKFOR_IF)
2101+
whilelevel = 0;
2102+
}
20892103
}
20902104

20912105
// if it was a "while" then we need to go back to
@@ -2096,17 +2110,6 @@ find_match(int lookfor, linenr_T ourscope)
20962110
continue;
20972111
}
20982112

2099-
// If it's an "if" decrement elselevel
2100-
look = cin_skipcomment(ml_get_curline());
2101-
if (cin_isif(look))
2102-
{
2103-
elselevel--;
2104-
// When looking for an "if" ignore "while"s that
2105-
// get in the way.
2106-
if (elselevel == 0 && lookfor == LOOKFOR_IF)
2107-
whilelevel = 0;
2108-
}
2109-
21102113
// If it's a "do" decrement whilelevel
21112114
if (cin_isdo(look))
21122115
whilelevel--;

src/testdir/test_cindent.vim

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,6 +1111,27 @@ def Test_cindent_1()
11111111
b;
11121112
}
11131113

1114+
void func() {
1115+
if (0)
1116+
do
1117+
if (0);
1118+
while (0);
1119+
else;
1120+
}
1121+
1122+
void func() {
1123+
if (0)
1124+
do
1125+
if (0)
1126+
do
1127+
if (0)
1128+
a();
1129+
while (0);
1130+
while (0);
1131+
else
1132+
a();
1133+
}
1134+
11141135
/* end of AUTO */
11151136
[CODE]
11161137

@@ -2093,6 +2114,27 @@ def Test_cindent_1()
20932114
b;
20942115
}
20952116

2117+
void func() {
2118+
if (0)
2119+
do
2120+
if (0);
2121+
while (0);
2122+
else;
2123+
}
2124+
2125+
void func() {
2126+
if (0)
2127+
do
2128+
if (0)
2129+
do
2130+
if (0)
2131+
a();
2132+
while (0);
2133+
while (0);
2134+
else
2135+
a();
2136+
}
2137+
20962138
/* end of AUTO */
20972139

20982140
[CODE]

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,8 @@ static char *(features[]) =
734734

735735
static int included_patches[] =
736736
{ /* Add new patch number below this line */
737+
/**/
738+
2016,
737739
/**/
738740
2015,
739741
/**/

0 commit comments

Comments
 (0)