Currently Vim fails to indent correctly code where "else", "{" and an
expression occur on a single line. E.g. this fragment of code:
#v+
void func(void)
{
if(x==y)
if(y==z)
foo=1;
else { bar=1;
baz = 2;
}
printf("Foo!\n");
}
#v-
will have printf() indented too much.
The attached patch fixes the issue.
--
Cheers,
Lech
--
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
diff --git a/src/misc1.c b/src/misc1.c
index 76c9525..020117e 100644
--- a/src/misc1.c
+++ b/src/misc1.c
@@ -4983,7 +4983,7 @@ cin_skipcomment(s)
}
/*
- * Return TRUE if there there is no code at *s. White space and comments are
+ * Return TRUE if there is no code at *s. White space and comments are
* not considered code.
*/
static int
@@ -5458,8 +5458,11 @@ cin_islinecomment(p)
}
/*
- * Recognize a line that starts with '{' or '}', or ends with ';', '{' or '}'.
+ * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
+ * '}'.
* Don't consider "} else" a terminated line.
+ * Don't consider a line where there are unmatched opening braces before '}',
+ * ';' or ',' a terminated line.
* Return the character terminating the line (ending char's have precedence if
* both apply in order to determine initializations).
*/
@@ -5470,6 +5473,7 @@ cin_isterminated(s, incl_open, incl_comma)
int incl_comma; /* recognize a trailing comma */
{
char_u found_start = 0;
+ unsigned n_open = 0;
s = cin_skipcomment(s);
@@ -5480,10 +5484,19 @@ cin_isterminated(s, incl_open, incl_comma)
{
/* skip over comments, "" strings and 'c'haracters */
s = skip_string(cin_skipcomment(s));
- if ((*s == ';' || (incl_open && *s == '{') || *s == '}'
- || (incl_comma && *s == ','))
+ if (*s == '}' && n_open > 0)
+ --n_open;
+ if (n_open == 0
+ && (*s == ';' || *s == '}' || (incl_comma && *s == ','))
&& cin_nocode(s + 1))
return *s;
+ else if (*s == '{')
+ {
+ if (incl_open && cin_nocode(s + 1))
+ return *s;
+ else
+ ++n_open;
+ }
if (*s)
s++;
diff --git a/src/testdir/test3.in b/src/testdir/test3.in
index 62402fb..8576e27 100644
--- a/src/testdir/test3.in
+++ b/src/testdir/test3.in
@@ -1344,6 +1344,22 @@ func(int a
}
STARTTEST
+:set cino&
+2kdd=][
+ENDTEST
+
+void func(void)
+{
+ if(x==y)
+ if(y==z)
+ foo=1;
+ else { bar=1;
+ baz=2;
+ }
+ printf("Foo!\n");
+}
+
+STARTTEST
:g/^STARTTEST/.,/^ENDTEST/d
:1;/start of AUTO/,$wq! test.out
ENDTEST
diff --git a/src/testdir/test3.ok b/src/testdir/test3.ok
index 4e2a648..3764453 100644
--- a/src/testdir/test3.ok
+++ b/src/testdir/test3.ok
@@ -1204,3 +1204,15 @@ func(int a
{
}
+
+void func(void)
+{
+ if(x==y)
+ if(y==z)
+ foo=1;
+ else { bar=1;
+ baz=2;
+ }
+ printf("Foo!\n");
+}
+