Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/indent indent: fix line wrapping of comments to the ...



details:   https://anonhg.NetBSD.org/src/rev/ef0f914997be
branches:  trunk
changeset: 375367:ef0f914997be
user:      rillig <rillig%NetBSD.org@localhost>
date:      Mon May 15 19:55:51 2023 +0000

description:
indent: fix line wrapping of comments to the right of code

diffstat:

 tests/usr.bin/indent/lsym_comment.c |  29 ++++++++++++++++++++---------
 usr.bin/indent/pr_comment.c         |  11 +++++++++--
 2 files changed, 29 insertions(+), 11 deletions(-)

diffs (92 lines):

diff -r 42c5ccdb56d5 -r ef0f914997be tests/usr.bin/indent/lsym_comment.c
--- a/tests/usr.bin/indent/lsym_comment.c       Mon May 15 19:03:23 2023 +0000
+++ b/tests/usr.bin/indent/lsym_comment.c       Mon May 15 19:55:51 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: lsym_comment.c,v 1.10 2023/05/15 07:28:45 rillig Exp $ */
+/* $NetBSD: lsym_comment.c,v 1.11 2023/05/15 19:55:51 rillig Exp $ */
 
 /*
  * Tests for the token lsym_comment, which starts a comment.
@@ -584,7 +584,12 @@ function(void)
                        {
                                int decl;       /* indented declaration */
                                {
-                                       int decl;       /* indented declaration */
+// $ This comment is indented so far to the right that it may overshoot the
+// $ right margin.  The allowed line length is increased to the starting
+// $ indentation of 56 plus a fixed amount of 25 columns, resulting in 81.
+// $ The trailing '*' would fit, but the trailing '/' is too much.
+                                       int decl;       /* indented declaration
+                                                        */
                                }
                        }
                }
@@ -641,23 +646,29 @@ function(void)
 }
 //indent end
 
-//indent run
+//indent run -l78
 void
 function(void)
 {
        code();                 /* code comment */
        code();                 /* code comment _________ to line length 78 */
-       code();                 /* code comment __________ to line length 79 */
-       code();                 /* code comment ___________ to line length 80 */
-       code();                 /* code comment ____________ to line length 81 */
+       code();                 /* code comment __________ to line length 79
+                                */
+       code();                 /* code comment ___________ to line length 80
+                                */
+       code();                 /* code comment ____________ to line length 81
+                                */
        code();                 /* code comment _____________ to line length
                                 * 82 */
 
 /* $ In the following comments, the line length is measured after formatting. */
        code();                 /* code comment _________ to line length 78 */
-       code();                 /* code comment __________ to line length 79 */
-       code();                 /* code comment ___________ to line length 80 */
-       code();                 /* code comment ____________ to line length 81 */
+       code();                 /* code comment __________ to line length 79
+                                */
+       code();                 /* code comment ___________ to line length 80
+                                */
+       code();                 /* code comment ____________ to line length 81
+                                */
        code();                 /* code comment _____________ to line length
                                 * 82 */
 
diff -r 42c5ccdb56d5 -r ef0f914997be usr.bin/indent/pr_comment.c
--- a/usr.bin/indent/pr_comment.c       Mon May 15 19:03:23 2023 +0000
+++ b/usr.bin/indent/pr_comment.c       Mon May 15 19:55:51 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pr_comment.c,v 1.141 2023/05/15 09:22:53 rillig Exp $  */
+/*     $NetBSD: pr_comment.c,v 1.142 2023/05/15 19:55:51 rillig Exp $  */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: pr_comment.c,v 1.141 2023/05/15 09:22:53 rillig Exp $");
+__RCSID("$NetBSD: pr_comment.c,v 1.142 2023/05/15 19:55:51 rillig Exp $");
 
 #include <string.h>
 
@@ -225,6 +225,13 @@ copy_comment_wrap(int line_length, bool 
                    else
                        com.len = 0;
                    com_add_char(' ');
+               } else {
+                   size_t trimmed_len = com.len;
+                   while (ch_isblank(com.mem[trimmed_len - 1]))
+                       trimmed_len--;
+                   int now_len = ind_add(ps.com_ind, com.st, trimmed_len);
+                   if (now_len + 3 /* ' ' '*' '/' */ > line_length)
+                       output_line();
                }
 
                if (!(com.len > 0 && ch_isblank(com.mem[com.len - 1])))



Home | Main Index | Thread Index | Old Index