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: distinguish between 'column' and 'ind...



details:   https://anonhg.NetBSD.org/src/rev/bb061e828e1d
branches:  trunk
changeset: 960266:bb061e828e1d
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sat Mar 13 13:51:08 2021 +0000

description:
indent: distinguish between 'column' and 'indentation'

column == 1 + indentation.

In addition, indentation is a relative distance while column is an
absolute position.  Therefore, don't confuse these two concepts, to
prevent off-by-one errors.

No functional change.

diffstat:

 usr.bin/indent/args.c         |  10 +++++-----
 usr.bin/indent/indent.c       |  30 ++++++++++++++++--------------
 usr.bin/indent/indent_globs.h |  30 +++++++++++++++---------------
 usr.bin/indent/io.c           |  12 ++++++------
 usr.bin/indent/pr_comment.c   |  11 ++++++-----
 5 files changed, 48 insertions(+), 45 deletions(-)

diffs (274 lines):

diff -r db87d65b9767 -r bb061e828e1d usr.bin/indent/args.c
--- a/usr.bin/indent/args.c     Sat Mar 13 13:25:23 2021 +0000
+++ b/usr.bin/indent/args.c     Sat Mar 13 13:51:08 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: args.c,v 1.20 2021/03/13 11:19:43 rillig Exp $ */
+/*     $NetBSD: args.c,v 1.21 2021/03/13 13:51:08 rillig Exp $ */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -46,7 +46,7 @@
 #include <sys/cdefs.h>
 #ifndef lint
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: args.c,v 1.20 2021/03/13 11:19:43 rillig Exp $");
+__RCSID("$NetBSD: args.c,v 1.21 2021/03/13 13:51:08 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/args.c 336318 2018-07-15 21:04:21Z pstef $");
 #endif
@@ -119,12 +119,12 @@
     {"br", PRO_BOOL, true, ON, &opt.btype_2},
     {"bs", PRO_BOOL, false, ON, &opt.Bill_Shannon},
     {"cdb", PRO_BOOL, true, ON, &opt.comment_delimiter_on_blankline},
-    {"cd", PRO_INT, 0, 0, &opt.decl_com_ind},
+    {"cd", PRO_INT, 0, 0, &opt.decl_comment_column},
     {"ce", PRO_BOOL, true, ON, &opt.cuddle_else},
     {"ci", PRO_INT, 0, 0, &opt.continuation_indent},
     {"cli", PRO_SPECIAL, 0, CLI, 0},
     {"cs", PRO_BOOL, false, ON, &opt.space_after_cast},
-    {"c", PRO_INT, 33, 0, &opt.com_ind},
+    {"c", PRO_INT, 33, 0, &opt.comment_column},
     {"di", PRO_INT, 16, 0, &opt.decl_indent},
     {"dj", PRO_BOOL, false, ON, &opt.ljust_decl},
     {"d", PRO_INT, 0, 0, &opt.unindent_displace},
@@ -134,7 +134,7 @@
     {"fc1", PRO_BOOL, true, ON, &opt.format_col1_comments},
     {"fcb", PRO_BOOL, true, ON, &opt.format_block_comments},
     {"ip", PRO_BOOL, true, ON, &opt.indent_parameters},
-    {"i", PRO_INT, 8, 0, &opt.ind_size},
+    {"i", PRO_INT, 8, 0, &opt.indent_size},
     {"lc", PRO_INT, 0, 0, &opt.block_comment_max_line_length},
     {"ldi", PRO_INT, -1, 0, &opt.local_decl_indent},
     {"lpl", PRO_BOOL, false, ON, &opt.lineup_to_parens_always},
diff -r db87d65b9767 -r bb061e828e1d usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Sat Mar 13 13:25:23 2021 +0000
+++ b/usr.bin/indent/indent.c   Sat Mar 13 13:51:08 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.c,v 1.56 2021/03/13 13:25:23 rillig Exp $       */
+/*     $NetBSD: indent.c,v 1.57 2021/03/13 13:51:08 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.56 2021/03/13 13:25:23 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.57 2021/03/13 13:51:08 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
 #endif
@@ -507,16 +507,18 @@
        }
     }
 
-    if (opt.com_ind <= 1)
-       opt.com_ind = 2;        /* don't put normal comments before column 2 */
+    if (opt.comment_column <= 1)
+       opt.comment_column = 2; /* don't put normal comments before column 2 */
     if (opt.block_comment_max_line_length <= 0)
        opt.block_comment_max_line_length = opt.max_line_length;
     if (opt.local_decl_indent < 0) /* if not specified by user, set this */
        opt.local_decl_indent = opt.decl_indent;
-    if (opt.decl_com_ind <= 0) /* if not specified by user, set this */
-       opt.decl_com_ind = opt.ljust_decl ? (opt.com_ind <= 10 ? 2 : opt.com_ind - 8) : opt.com_ind;
+    if (opt.decl_comment_column <= 0)  /* if not specified by user, set this */
+       opt.decl_comment_column = opt.ljust_decl
+           ? (opt.comment_column <= 10 ? 2 : opt.comment_column - 8)
+           : opt.comment_column;
     if (opt.continuation_indent == 0)
-       opt.continuation_indent = opt.ind_size;
+       opt.continuation_indent = opt.indent_size;
 }
 
 static void
@@ -538,8 +540,8 @@
            break;
        p++;
     }
-    if (col > opt.ind_size)
-       ps.ind_level = ps.i_l_follow = col / opt.ind_size;
+    if (col > opt.indent_size)
+       ps.ind_level = ps.i_l_follow = col / opt.indent_size;
 }
 
 static void
@@ -641,8 +643,8 @@
     ps.paren_indents[ps.p_l_follow - 1] =
        indentation_after_range(0, s_code, e_code);
     if (sp_sw && ps.p_l_follow == 1 && opt.extra_expression_indent
-           && ps.paren_indents[0] < 2 * opt.ind_size)
-       ps.paren_indents[0] = 2 * opt.ind_size;
+           && ps.paren_indents[0] < 2 * opt.indent_size)
+       ps.paren_indents[0] = 2 * opt.indent_size;
     if (ps.in_or_st && *token == '(' && ps.tos <= 2) {
        /*
         * this is a kluge to make sure that declarations will be
@@ -1521,9 +1523,9 @@
     /*
      * get the tab math right for indentations that are not multiples of tabsize
      */
-    if ((ps.ind_level * opt.ind_size) % opt.tabsize != 0) {
-       pos += (ps.ind_level * opt.ind_size) % opt.tabsize;
-       cur_dec_ind += (ps.ind_level * opt.ind_size) % opt.tabsize;
+    if ((ps.ind_level * opt.indent_size) % opt.tabsize != 0) {
+       pos += (ps.ind_level * opt.indent_size) % opt.tabsize;
+       cur_dec_ind += (ps.ind_level * opt.indent_size) % opt.tabsize;
     }
     if (tabs_to_var) {
        int tpos;
diff -r db87d65b9767 -r bb061e828e1d usr.bin/indent/indent_globs.h
--- a/usr.bin/indent/indent_globs.h     Sat Mar 13 13:25:23 2021 +0000
+++ b/usr.bin/indent/indent_globs.h     Sat Mar 13 13:51:08 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent_globs.h,v 1.19 2021/03/13 13:25:23 rillig Exp $ */
+/*     $NetBSD: indent_globs.h,v 1.20 2021/03/13 13:51:08 rillig Exp $ */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -93,11 +93,11 @@
 extern struct options {
     int         blanklines_around_conditional_compilation;
     int         blanklines_after_declarations_at_proctop; /* this is vaguely
-                                * similar to blanklines_after_decla except
-                                * that in only applies to the first set of
-                                * declarations in a procedure (just after
-                                * the first '{') and it causes a blank line
-                                * to be generated even if there are no
+                                * similar to blanklines_after_declarations
+                                * except that it only applies to the first
+                                * set of declarations in a procedure (just
+                                * after the first '{') and it causes a blank
+                                * line to be generated even if there are no
                                 * declarations */
     int         blanklines_after_declarations;
     int         blanklines_after_procs;
@@ -109,17 +109,17 @@
     int         Bill_Shannon;  /* true iff a blank should always be
                                 * inserted after sizeof */
     int         comment_delimiter_on_blankline;
-    int         decl_com_ind;  /* the column in which comments after
+    int         decl_comment_column; /* the column in which comments after
                                 * declarations should be put */
-    int         cuddle_else;   /* true if else should cuddle up to '}' */
+    int         cuddle_else;   /* true if 'else' should cuddle up to '}' */
     int         continuation_indent; /* set to the indentation between the
                                 * edge of code and continuation lines */
-    float       case_indent;   /* The distance to indent case labels from the
-                                * switch statement */
-    /* XXX: TODO: rename to 'comment_column' since 'ind' is confusing */
-    int         com_ind;       /* the column in which comments to the right
+    float       case_indent;   /* The distance (measured in tabsize) to
+                                * indent case labels from the switch
+                                * statement */
+    int         comment_column;        /* the column in which comments to the right
                                 * of code should start */
-    int         decl_indent;   /* column to indent declared identifiers to */
+    int         decl_indent;   /* indentation of identifier in declaration */
     int         ljust_decl;    /* true if declarations should be left
                                 * justified */
     int         unindent_displace; /* comments not to the right of code
@@ -139,9 +139,9 @@
                                 * are to be magically reformatted (just
                                 * like comments that begin in later columns) */
     int         format_block_comments; /* true if comments beginning with
-                                * `/ * \n' are to be reformatted */
+                                * '/ * \n' are to be reformatted */
     int         indent_parameters;
-    int         ind_size;      /* the size of one indentation level */
+    int         indent_size;   /* the size of one indentation level */
     int         block_comment_max_line_length;
     int         local_decl_indent; /* like decl_indent but for locals */
     int         lineup_to_parens_always; /* if true, do not attempt to keep
diff -r db87d65b9767 -r bb061e828e1d usr.bin/indent/io.c
--- a/usr.bin/indent/io.c       Sat Mar 13 13:25:23 2021 +0000
+++ b/usr.bin/indent/io.c       Sat Mar 13 13:51:08 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: io.c,v 1.42 2021/03/13 11:19:43 rillig Exp $   */
+/*     $NetBSD: io.c,v 1.43 2021/03/13 13:51:08 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.42 2021/03/13 11:19:43 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.43 2021/03/13 13:51:08 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/io.c 334927 2018-06-10 16:44:18Z pstef $");
 #endif
@@ -289,12 +289,12 @@
 int
 compute_code_indent(void)
 {
-    int target_ind = opt.ind_size * ps.ind_level;
+    int target_ind = opt.indent_size * ps.ind_level;
 
     if (ps.paren_level != 0) {
        if (!opt.lineup_to_parens)
            target_ind += opt.continuation_indent *
-               (2 * opt.continuation_indent == opt.ind_size ? 1 : ps.paren_level);
+               (2 * opt.continuation_indent == opt.indent_size ? 1 : ps.paren_level);
        else if (opt.lineup_to_parens_always)
            /*
             * XXX: where does this '- 1' come from?  It looks strange but is
@@ -323,10 +323,10 @@
 compute_label_indent(void)
 {
     if (ps.pcase)
-       return (int) (case_ind * opt.ind_size);
+       return (int) (case_ind * opt.indent_size);
     if (s_lab[0] == '#')
         return 0;
-    return opt.ind_size * (ps.ind_level - label_offset);
+    return opt.indent_size * (ps.ind_level - label_offset);
 }
 
 
diff -r db87d65b9767 -r bb061e828e1d usr.bin/indent/pr_comment.c
--- a/usr.bin/indent/pr_comment.c       Sat Mar 13 13:25:23 2021 +0000
+++ b/usr.bin/indent/pr_comment.c       Sat Mar 13 13:51:08 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pr_comment.c,v 1.28 2021/03/13 13:25:23 rillig Exp $   */
+/*     $NetBSD: pr_comment.c,v 1.29 2021/03/13 13:51:08 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.28 2021/03/13 13:25:23 rillig Exp $");
+__RCSID("$NetBSD: pr_comment.c,v 1.29 2021/03/13 13:51:08 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/pr_comment.c 334927 2018-06-10 16:44:18Z pstef $");
 #endif
@@ -140,7 +140,7 @@
             * If this (*and previous lines are*) blank, dont put comment way
             * out at left
             */
-           ps.com_col = (ps.ind_level - opt.unindent_displace) * opt.ind_size + 1;
+           ps.com_col = (ps.ind_level - opt.unindent_displace) * opt.indent_size + 1;
            adj_max_line_length = opt.block_comment_max_line_length;
            if (ps.com_col <= 1)
                ps.com_col = 1 + !opt.format_col1_comments;
@@ -154,7 +154,8 @@
                if (s_lab != e_lab)
                    target_col = 1 + indentation_after(compute_label_indent(), s_lab);
            }
-           ps.com_col = ps.decl_on_line || ps.ind_level == 0 ? opt.decl_com_ind : opt.com_ind;
+           ps.com_col = ps.decl_on_line || ps.ind_level == 0
+               ? opt.decl_comment_column : opt.comment_column;
            if (ps.com_col <= target_col)
                ps.com_col = opt.tabsize * (1 + (target_col - 1) / opt.tabsize) + 1;
            if (ps.com_col + 24 > adj_max_line_length)
@@ -222,7 +223,7 @@
 
     while (1) {                        /* this loop will go until the comment is
                                 * copied */
-       switch (*buf_ptr) {     /* this checks for various spcl cases */
+       switch (*buf_ptr) {     /* this checks for various special cases */
        case 014:               /* check for a form feed */
            check_size_comment(3, &last_bl);
            if (!ps.box_com) {  /* in a text comment, break the line here */



Home | Main Index | Thread Index | Old Index