[PATCH] Added --fix-nested-comments option.

Andrew Shadura <[email protected]> Thu, 26 Jan 2012 09:41:25 +0100
Newsgroups gmane.comp.gnu.indent.bugs
Message-ID <20120126094125.4c916b63@ileemo>
Hello,

Here's the patch that implements reformatting of nested comments.
Simple but very needed feature sometimes. I'd be glad if you included
it in the next release :)

P.S. Please also consider other patches published here:
http://patch-tracker.debian.org/package/indent/2.2.11-1

-- 
WBR, Andrew

_______________________________________________
bug-indent mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/bug-indent
fix-nested-comments.patch (text/x-patch, 4.9 KB)
Description: add an option to fix nested comments
 Added -fnc/--fix-nested comment option which tries to fix nested comments
 by replacing '/*' inside comments by a space followed by '*'. By default,
 this option is turned off.
Author: Andrew O. Shadura <[email protected]>
Last-Update: 2012-01-26

--- a/src/comments.c
+++ b/src/comments.c
@@ -59,6 +59,9 @@
  * `format_col1_comments'           ("fc1"):  Format comments which
  *     begin in column 1.
  *
+ * `fix_nested_comments'            ("fnc"):  Fix comments which
+ *     have nested opening sequences
+ *
  * `unindent_displace'              ("d"):  The hanging indentation for
  *     comments which do not appear to the right of code.
  *
@@ -190,6 +193,15 @@
         /* We have reached the end of the comment, and it's all on
          * this line. */
 
+         if (settings.fix_nested_comments)
+         {
+            if ((*buf_ptr == '*') && (*(buf_ptr - 1) == '/'))
+            {
+               *(e_com - 1) = ' ';
+               *e_com = '*';
+            }
+         }
+
          if ((*buf_ptr == '*') && (*(buf_ptr + 1) == '/'))
          {
             if (buf_ptr == buf_end)
--- a/src/args.c
+++ b/src/args.c
@@ -158,6 +158,7 @@
 static int exp_eei  = 0;
 static int exp_fc1  = 0;
 static int exp_fca  = 0;
+static int exp_fnc  = 0;
 static int exp_gnu  = 0;
 static int exp_hnl  = 0;
 static int exp_i    = 0;
@@ -314,6 +315,7 @@
     {"hnl",     PRO_BOOL,                            true,       ON, &settings.honour_newlines,                  &exp_hnl},
     {"h",       PRO_FUNCTION,                           0, ONOFF_NA, (int *) usage,                              &exp_version},
     {"gnu",     PRO_SETTINGS,                           0, ONOFF_NA, GNU_SETTINGS_STRING,                        &exp_gnu},
+    {"fnc",     PRO_BOOL,                           false,      OFF, &settings.fix_nested_comments,              &exp_fnc},
     {"fca",     PRO_BOOL,                            true,       ON, &settings.format_comments,                  &exp_fca},
     {"fc1",     PRO_BOOL,                            true,       ON, &settings.format_col1_comments,             &exp_fc1},
     {"eei",     PRO_BOOL,                           false,       ON, &settings.extra_expression_indent,          &exp_eei},
@@ -433,6 +435,7 @@
     {"h",       PRO_FUNCTION,                           0, ONOFF_NA, (int *) usage,                              &exp_version},
     /* This is now the default. */
     {"gnu",     PRO_SETTINGS,                           0, ONOFF_NA, GNU_SETTINGS_STRING,                        &exp_gnu},
+    {"fnc",     PRO_BOOL,                           false,      OFF, &settings.fix_nested_comments,              &exp_fnc},
     {"fca",     PRO_BOOL,                           false,       ON, &settings.format_comments,                  &exp_fca},
     {"fc1",     PRO_BOOL,                           false,       ON, &settings.format_col1_comments,             &exp_fc1},
     {"eei",     PRO_BOOL,                           false,       ON, &settings.extra_expression_indent,          &exp_eei},
@@ -548,6 +551,7 @@
     {"honour-newlines",                             "hnl"},
     {"help",                                        "h"},
     {"gnu-style",                                   "gnu"},
+    {"fix-nested-comments",                         "fnc"},
     {"format-first-column-comments",                "fc1"},
     {"format-all-comments",                         "fca"},
     {"extra-expression-indentation",                "eei"},
--- a/src/indent.h
+++ b/src/indent.h
@@ -268,6 +268,7 @@
     int lineup_to_parens;    /*!<  if true, continued code within parens will be lined up to the open paren */
     int honour_newlines;     /*!<  True when positions at which we read a newline in the input file, should get
                               * a high priority to break long lines at. */
+    int fix_nested_comments; /*!<  If nested comments are to be fixed */
     int format_comments; /*!<  If any comments are to be reformatted */
     int format_col1_comments; /*!<  If comments which start in column 1 are to be magically reformatted */
     int extra_expression_indent;     /*!<  True if continuation lines from the expression part of "if(e)",
--- a/doc/indent.texinfo
+++ b/doc/indent.texinfo
@@ -653,6 +653,12 @@
 duration of that comment.  The margin is not respected if the comment is
 not being formatted.
 
+@kindex -fnc
+@kindex --fix-nested-comments
+If the @option{-fnc} option is specified, all comments with @samp{/*}
+embedded will have that character sequence replaced by a space followed
+by the character @samp{*} thus eliminating nesting.
+
 @kindex -d@var{n}
 @kindex --line-comments-indentation@var{n}
 If the comment begins a line (i.e., there is no program text to its
@@ -1752,6 +1758,11 @@
 Do not disable all formatting of comments.@*
 @xref{Comments}.
 
+@item -fnc
+@itemx --fix-nested-comments
+Fix nested comments.@*
+@xref{Comments}.
+
 @item -gnu
 @itemx --gnu-style
 Use GNU coding style.  This is the default.@*
signature.asc (application/pgp-signature, 835 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iQIcBAEBCAAGBQJPIRG1AAoJEG6k0jEaLSaNAwEP/1Lzjh8ubWlyMFlMXdPF1jD/
79JYdX0JHEOBur/dFgU5YCzvMwZzep9Dii8b+cGNPcIw7BaJjwuXfVlnZgUvSQGS
r6MJGksWd4aOYYIpsxRnwbid2UDb3vvdgvDQVp4JL0ZzlOM4cn9byUvWQ3/xvzGt
LIv7n2preIMQwAAn9/NeCXTUKW3oZekGvW9Arj1mz2n9L0sPjNdjhCuJ2OCDfbaP
anqR8ACiv35ezB7/zpyfMARXTSxnNCWF1oTp0oGdCNGwAIJoozmbIkLKqqiEWHJm
5Blk566XzzXP4hgPR89coQhd2sT6xkc4EysK3G1dNSW43rXssmJcGvAewOqwiZoB
ST6Z4TkxzmijfdJ6Wf4qnzI4pci5KL2EwMKXQv5DGMw5mLkfMQOO+hcSYj26sbiH
U7/s+WVM/0Oci5GBsMKn+jrW/N4B3hY/9G4hLpDrol8C/KBaimRB/JFXVQ6pF57F
QzIcyEprp1OFaM0hNBeILg7lOcEXHlLM4ljvUmp/QqEMwj75o2lcgyHTCLQn/u+g
wliH1MzByPjAa4NbIjeiXQ7RcVcP5JCUzhkkEN2/Tys0SJYagn5in14tbpQR5vZE
JSWD/1AJM55uY3y8+E+i31d8OKdwjmECANHFiZe8Kb2fjxNViT8pIpnzD1t0aqEP
f6m7rSMrxB/fRZRltJkU
=HQOw
-----END PGP SIGNATURE-----