Wednesday, February 24, 2010

PATCH: 'cindent' is too forgiving for function return type decls

diff -r 903fcd726d90 src/misc1.c
--- a/src/misc1.c Thu Feb 11 18:54:43 2010 +0100
+++ b/src/misc1.c Thu Feb 25 01:21:16 2010 -0500
@@ -7729,9 +7729,12 @@
* line needs to be indented as a function type spec.
* Don't do this if the current line looks like a comment
* or if the current line is terminated, ie. ends in ';'.
+ * It also cannot contain a '{' or a '}'.
*/
else if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
&& !cin_nocode(theline)
+ && !vim_strchr(theline, '{')
+ && !vim_strchr(theline, '}')
&& !cin_ends_in(theline, (char_u *)":", NULL)
&& !cin_ends_in(theline, (char_u *)",", NULL)
&& cin_isfuncdecl(NULL, cur_curpos.lnum + 1)
In #vim, ShingRay pointed out a bug in cindent, which can be reproduced with:

echo $'void f() {\nif (1)\n;\n}' \
| vim - -u NONE -N -c 'set ft=c' -c 'norm gg=G'

The above adds an extra 'shiftwidth' to every line. It's caused by
vim mistakenly deciding that "void f() {" is specifying the function
return type for the function "if". Since I can't think of a C-like
language that would allow { or } anywhere in the return type of a
function, here's a patch that fixes the problem by never considering a
line containing braces to be return type declaration.

~Matt

diff -r 903fcd726d90 src/misc1.c
--- a/src/misc1.c Thu Feb 11 18:54:43 2010 +0100
+++ b/src/misc1.c Thu Feb 25 01:21:16 2010 -0500
@@ -7729,9 +7729,12 @@
* line needs to be indented as a function type spec.
* Don't do this if the current line looks like a comment
* or if the current line is terminated, ie. ends in ';'.
+ * It also cannot contain a '{' or a '}'.
*/
else if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
&& !cin_nocode(theline)
+ && !vim_strchr(theline, '{')
+ && !vim_strchr(theline, '}')
&& !cin_ends_in(theline, (char_u *)":", NULL)
&& !cin_ends_in(theline, (char_u *)",", NULL)
&& cin_isfuncdecl(NULL, cur_curpos.lnum + 1)

--
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php

No comments:

Post a Comment