Module Name:    src
Committed By:   rillig
Date:           Sat Mar 13 10:06:47 UTC 2021

Modified Files:
        src/usr.bin/indent: indent.c indent.h io.c pr_comment.c

Log Message:
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.


To generate a diff of this commit:
cvs rdiff -u -r1.48 -r1.49 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.10 -r1.11 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.38 -r1.39 src/usr.bin/indent/io.c
cvs rdiff -u -r1.22 -r1.23 src/usr.bin/indent/pr_comment.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/indent/indent.c
diff -u src/usr.bin/indent/indent.c:1.48 src/usr.bin/indent/indent.c:1.49
--- src/usr.bin/indent/indent.c:1.48	Sat Mar 13 09:21:57 2021
+++ src/usr.bin/indent/indent.c	Sat Mar 13 10:06:47 2021
@@ -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 @@ static char sccsid[] = "@(#)indent.c	5.1
 #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 @@ main(int argc, char **argv)
 		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;
 	    }

Index: src/usr.bin/indent/indent.h
diff -u src/usr.bin/indent/indent.h:1.10 src/usr.bin/indent/indent.h:1.11
--- src/usr.bin/indent/indent.h:1.10	Sat Mar 13 09:54:11 2021
+++ src/usr.bin/indent/indent.h	Sat Mar 13 10:06:47 2021
@@ -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 @@ __FBSDID("$FreeBSD: head/usr.bin/indent/
 
 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 *);

Index: src/usr.bin/indent/io.c
diff -u src/usr.bin/indent/io.c:1.38 src/usr.bin/indent/io.c:1.39
--- src/usr.bin/indent/io.c:1.38	Sat Mar 13 09:54:11 2021
+++ src/usr.bin/indent/io.c	Sat Mar 13 10:06:47 2021
@@ -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 @@ static char sccsid[] = "@(#)io.c	8.1 (Be
 #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 @@ dump_line(void)
 		comment_open = 0;
 		output_string(".*/\n");
 	    }
-	    target_col = compute_code_column();
+	    target_col = 1 + compute_code_indent();
 	    {
 		int i;
 
@@ -287,31 +287,36 @@ dump_line(void)
 }
 
 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

Index: src/usr.bin/indent/pr_comment.c
diff -u src/usr.bin/indent/pr_comment.c:1.22 src/usr.bin/indent/pr_comment.c:1.23
--- src/usr.bin/indent/pr_comment.c:1.22	Sat Mar 13 09:54:11 2021
+++ src/usr.bin/indent/pr_comment.c	Sat Mar 13 10:06:47 2021
@@ -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 @@ static char sccsid[] = "@(#)pr_comment.c
 #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 @@ pr_comment(void)
 	    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)

Reply via email to