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: replace compute_code_column with comp...



details:   https://anonhg.NetBSD.org/src/rev/27c61721382e
branches:  trunk
changeset: 960255:27c61721382e
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sat Mar 13 10:06:47 2021 +0000

description:
indent: replace compute_code_column with compute_code_indent

The goal is to only ever be concerned about the _indentation_ of a
token, never the _column_ it appears in.  Having only one of these
avoids off-by-one errors.

No functional change.

diffstat:

 usr.bin/indent/indent.c     |   6 +++---
 usr.bin/indent/indent.h     |   6 +++---
 usr.bin/indent/io.c         |  33 +++++++++++++++++++--------------
 usr.bin/indent/pr_comment.c |   8 ++++----
 4 files changed, 29 insertions(+), 24 deletions(-)

diffs (161 lines):

diff -r e0791395c6bd -r 27c61721382e usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Sat Mar 13 09:54:11 2021 +0000
+++ b/usr.bin/indent/indent.c   Sat Mar 13 10:06:47 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.c,v 1.48 2021/03/13 09:21:57 rillig Exp $       */
+/*     $NetBSD: indent.c,v 1.49 2021/03/13 10:06:47 rillig Exp $       */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -46,7 +46,7 @@
 #include <sys/cdefs.h>
 #ifndef lint
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: indent.c,v 1.48 2021/03/13 09:21:57 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.49 2021/03/13 10:06:47 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
 #endif
@@ -1150,7 +1150,7 @@
                if (ps.block_init_level <= 0)
                    ps.block_init = 0;
                if (break_comma && (!opt.leave_comma ||
-                   count_spaces_until(compute_code_column(), s_code, e_code) >
+                   count_spaces_until(1 + compute_code_indent(), s_code, e_code) >
                    opt.max_col - opt.tabsize))
                    force_nl = true;
            }
diff -r e0791395c6bd -r 27c61721382e usr.bin/indent/indent.h
--- a/usr.bin/indent/indent.h   Sat Mar 13 09:54:11 2021 +0000
+++ b/usr.bin/indent/indent.h   Sat Mar 13 10:06:47 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.h,v 1.10 2021/03/13 09:54:11 rillig Exp $       */
+/*     $NetBSD: indent.h,v 1.11 2021/03/13 10:06:47 rillig Exp $       */
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -30,7 +30,7 @@
 
 #if 0
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: indent.h,v 1.10 2021/03/13 09:54:11 rillig Exp $");
+__RCSID("$NetBSD: indent.h,v 1.11 2021/03/13 10:06:47 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/indent.h 336333 2018-07-16 05:46:50Z pstef $");
 #endif
@@ -45,7 +45,7 @@
 
 void   add_typename(const char *);
 void   alloc_typenames(void);
-int    compute_code_column(void);
+int    compute_code_indent(void);
 int    compute_label_indent(void);
 int    count_spaces(int, const char *);
 int    count_spaces_until(int, const char *, const char *);
diff -r e0791395c6bd -r 27c61721382e usr.bin/indent/io.c
--- a/usr.bin/indent/io.c       Sat Mar 13 09:54:11 2021 +0000
+++ b/usr.bin/indent/io.c       Sat Mar 13 10:06:47 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: io.c,v 1.38 2021/03/13 09:54:11 rillig Exp $   */
+/*     $NetBSD: io.c,v 1.39 2021/03/13 10:06:47 rillig Exp $   */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -46,7 +46,7 @@
 #include <sys/cdefs.h>
 #ifndef lint
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: io.c,v 1.38 2021/03/13 09:54:11 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.39 2021/03/13 10:06:47 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/io.c 334927 2018-06-10 16:44:18Z pstef $");
 #endif
@@ -195,7 +195,7 @@
                comment_open = 0;
                output_string(".*/\n");
            }
-           target_col = compute_code_column();
+           target_col = 1 + compute_code_indent();
            {
                int i;
 
@@ -287,31 +287,36 @@
 }
 
 int
-compute_code_column(void)
+compute_code_indent(void)
 {
-    int target_col = opt.ind_size * ps.ind_level + 1;
+    int target_ind = opt.ind_size * ps.ind_level;
 
-    if (ps.paren_level) {
+    if (ps.paren_level != 0) {
        if (!opt.lineup_to_parens)
-           target_col += opt.continuation_indent *
+           target_ind += opt.continuation_indent *
                (2 * opt.continuation_indent == opt.ind_size ? 1 : ps.paren_level);
        else if (opt.lineup_to_parens_always)
-           target_col = paren_indent;
+           /*
+            * XXX: where does this '- 1' come from?  It looks strange but is
+            * nevertheless needed for proper indentation, as demonstrated in
+            * the test opt-lpl.0.
+            */
+           target_ind = paren_indent - 1;
        else {
            int w;
            int t = paren_indent;
 
            if ((w = count_spaces(t, s_code) - opt.max_col) > 0
-                   && count_spaces(target_col, s_code) <= opt.max_col) {
+                   && count_spaces(target_ind + 1, s_code) <= opt.max_col) {
                t -= w + 1;
-               if (t > target_col)
-                   target_col = t;
+               if (t > target_ind + 1)
+                   target_ind = t - 1;
            } else
-               target_col = t;
+               target_ind = t - 1;
        }
     } else if (ps.ind_stmt)
-       target_col += opt.continuation_indent;
-    return target_col;
+       target_ind += opt.continuation_indent;
+    return target_ind;
 }
 
 int
diff -r e0791395c6bd -r 27c61721382e usr.bin/indent/pr_comment.c
--- a/usr.bin/indent/pr_comment.c       Sat Mar 13 09:54:11 2021 +0000
+++ b/usr.bin/indent/pr_comment.c       Sat Mar 13 10:06:47 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pr_comment.c,v 1.22 2021/03/13 09:54:11 rillig Exp $   */
+/*     $NetBSD: pr_comment.c,v 1.23 2021/03/13 10:06:47 rillig Exp $   */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -46,7 +46,7 @@
 #include <sys/cdefs.h>
 #ifndef lint
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: pr_comment.c,v 1.22 2021/03/13 09:54:11 rillig Exp $");
+__RCSID("$NetBSD: pr_comment.c,v 1.23 2021/03/13 10:06:47 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/pr_comment.c 334927 2018-06-10 16:44:18Z pstef $");
 #endif
@@ -162,11 +162,11 @@
            int target_col;
            break_delim = false;
            if (s_code != e_code)
-               target_col = count_spaces(compute_code_column(), s_code);
+               target_col = count_spaces(1 + compute_code_indent(), s_code);
            else {
                target_col = 1;
                if (s_lab != e_lab)
-                   target_col = count_spaces(compute_label_indent() + 1, s_lab);
+                   target_col = count_spaces(1 + compute_label_indent(), s_lab);
            }
            ps.com_col = ps.decl_on_line || ps.ind_level == 0 ? opt.decl_com_ind : opt.com_ind;
            if (ps.com_col <= target_col)



Home | Main Index | Thread Index | Old Index