]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.1.2016: cindent wrong indentation after do-while loop v9.1.2016
authorAnttoni Erkkilä <anttoni.erkkila@protonmail.com>
Tue, 23 Dec 2025 20:42:57 +0000 (20:42 +0000)
committerChristian Brabandt <cb@256bit.org>
Tue, 23 Dec 2025 20:42:57 +0000 (20:42 +0000)
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>
src/cindent.c
src/testdir/test_cindent.vim
src/version.c

index e23f96d453cf2956b18ac42d1e40888f9b506a35..12d7e694d90d5d65e00ecbd9af6b6418955d2b0b 100644 (file)
@@ -2076,16 +2076,30 @@ find_match(int lookfor, linenr_T ourscope)
            if (theirscope->lnum > ourscope)
                continue;
 
-           // 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(ml_get_curline());
-           if (cin_iselse(look))
+           // When looking for if, we ignore "if" and "else" in a deeper do-while loop.
+           if (!(lookfor == LOOKFOR_IF && whilelevel))
            {
-               mightbeif = cin_skipcomment(look + 4);
-               if (!cin_isif(mightbeif))
-                   ++elselevel;
-               continue;
+               // if it was an "else" (that's not an "else if")
+               // then we need to go back to another if, so
+               // increment elselevel
+               if (cin_iselse(look))
+               {
+                   mightbeif = cin_skipcomment(look + 4);
+                   if (!cin_isif(mightbeif))
+                       ++elselevel;
+                   continue;
+               }
+
+               // If it's an "if" decrement elselevel
+               if (cin_isif(look))
+               {
+                   elselevel--;
+                   // When looking for an "if" ignore "while"s that
+                   // get in the way.
+                   if (elselevel == 0 && lookfor == LOOKFOR_IF)
+                       whilelevel = 0;
+               }
            }
 
            // if it was a "while" then we need to go back to
@@ -2096,17 +2110,6 @@ find_match(int lookfor, linenr_T ourscope)
                continue;
            }
 
-           // If it's an "if" decrement elselevel
-           look = cin_skipcomment(ml_get_curline());
-           if (cin_isif(look))
-           {
-               elselevel--;
-               // When looking for an "if" ignore "while"s that
-               // get in the way.
-               if (elselevel == 0 && lookfor == LOOKFOR_IF)
-                   whilelevel = 0;
-           }
-
            // If it's a "do" decrement whilelevel
            if (cin_isdo(look))
                whilelevel--;
index 589fcdd61f640d4dadefa9640a566d497ecd85f3..4b6fcb8706c02af03eed9be4be718c05c5cdcfb3 100644 (file)
@@ -1111,6 +1111,27 @@ def 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]
 
@@ -2093,6 +2114,27 @@ def 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]
index 75714149c3174b7388b8c7348b60a83ea78a4f17..fdc6ed8f522683ffe0d20dd8e9e52d91accb20d7 100644 (file)
@@ -734,6 +734,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    2016,
 /**/
     2015,
 /**/