Module Name:    src
Committed By:   rillig
Date:           Fri Jun  9 08:10:58 UTC 2023

Modified Files:
        src/tests/usr.bin/indent: opt_eei.c
        src/usr.bin/indent: debug.c indent.c indent.h io.c

Log Message:
indent: when an indentation is ambiguous, indent one level further

The '-eei' mode now applies whenever the indentation from a multi-line
expression could be confused with a following statement.


To generate a diff of this commit:
cvs rdiff -u -r1.13 -r1.14 src/tests/usr.bin/indent/opt_eei.c
cvs rdiff -u -r1.40 -r1.41 src/usr.bin/indent/debug.c
cvs rdiff -u -r1.342 -r1.343 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.179 -r1.180 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.206 -r1.207 src/usr.bin/indent/io.c

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

Modified files:

Index: src/tests/usr.bin/indent/opt_eei.c
diff -u src/tests/usr.bin/indent/opt_eei.c:1.13 src/tests/usr.bin/indent/opt_eei.c:1.14
--- src/tests/usr.bin/indent/opt_eei.c:1.13	Fri Jun  9 07:54:05 2023
+++ src/tests/usr.bin/indent/opt_eei.c	Fri Jun  9 08:10:58 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: opt_eei.c,v 1.13 2023/06/09 07:54:05 rillig Exp $ */
+/* $NetBSD: opt_eei.c,v 1.14 2023/06/09 08:10:58 rillig Exp $ */
 
 /*
  * Tests for the options '-eei' and '-neei'.
@@ -62,18 +62,14 @@ b)
 //indent run -eei
 {
 	if (a <
-/* $ XXX: No extra indentation necessary. */
-			b)
+	    b)
 		stmt();
 	if (a
-/* $ XXX: No extra indentation necessary. */
-			<
-/* $ XXX: No extra indentation necessary. */
-			b)
+	    <
+	    b)
 		stmt();
 	while (a
-/* $ XXX: No extra indentation necessary. */
-			< b)
+	       < b)
 		stmt();
 	switch (
 			a)
@@ -110,8 +106,7 @@ b)
 	    b)
 	stmt();
     while (a
-/* $ XXX: No extra indentation necessary. */
-	    < b)
+	   < b)
 	stmt();
     switch (
 /* $ XXX: No extra indentation necessary. */
@@ -208,10 +203,9 @@ b)
 //indent run -eei
 {
 	if (fun(
-// $ TODO: Indent one level further.
-		1,
-		2,
-		3))
+			1,
+			2,
+			3))
 		stmt;
 }
 //indent end
@@ -239,9 +233,8 @@ b)
 	      3
 	    )))
 		stmt;
-// $ XXX: The indentation of '4' may be considered ambiguous.
 	if ((((
-		4
+		  4
 	      ))))
 		stmt;
 }

Index: src/usr.bin/indent/debug.c
diff -u src/usr.bin/indent/debug.c:1.40 src/usr.bin/indent/debug.c:1.41
--- src/usr.bin/indent/debug.c:1.40	Thu Jun  8 21:18:54 2023
+++ src/usr.bin/indent/debug.c	Fri Jun  9 08:10:58 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: debug.c,v 1.40 2023/06/08 21:18:54 rillig Exp $	*/
+/*	$NetBSD: debug.c,v 1.41 2023/06/09 08:10:58 rillig Exp $	*/
 
 /*-
  * Copyright (c) 2023 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: debug.c,v 1.40 2023/06/08 21:18:54 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.41 2023/06/09 08:10:58 rillig Exp $");
 
 #include <stdarg.h>
 
@@ -131,7 +131,7 @@ const char *const line_kind_name[] = {
 
 static const char *const extra_expr_indent_name[] = {
 	"no",
-	"yes",
+	"maybe",
 	"last",
 };
 

Index: src/usr.bin/indent/indent.c
diff -u src/usr.bin/indent/indent.c:1.342 src/usr.bin/indent/indent.c:1.343
--- src/usr.bin/indent/indent.c:1.342	Fri Jun  9 07:20:30 2023
+++ src/usr.bin/indent/indent.c	Fri Jun  9 08:10:58 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.c,v 1.342 2023/06/09 07:20:30 rillig Exp $	*/
+/*	$NetBSD: indent.c,v 1.343 2023/06/09 08:10:58 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: indent.c,v 1.342 2023/06/09 07:20:30 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.343 2023/06/09 08:10:58 rillig Exp $");
 
 #include <sys/param.h>
 #include <err.h>
@@ -453,10 +453,8 @@ process_lparen(void)
 	ps.want_blank = false;
 	buf_add_char(&code, token.s[0]);
 
-	if (opt.extra_expr_indent && !opt.lineup_to_parens
-	    && ps.spaced_expr_psym != psym_0 && ps.nparen == 1
-	    && opt.continuation_indent == opt.indent_size)
-		ps.extra_expr_indent = eei_yes;
+	if (opt.extra_expr_indent && ps.spaced_expr_psym != psym_0)
+		ps.extra_expr_indent = eei_maybe;
 
 	if (ps.init_or_struct && ps.psyms.top <= 2) {
 		/* A kludge to correctly align function definitions. */
@@ -465,9 +463,6 @@ process_lparen(void)
 	}
 
 	int indent = ind_add(0, code.s, code.len);
-	if (opt.extra_expr_indent && ps.spaced_expr_psym != psym_0
-	    && ps.nparen == 1 && indent < 2 * opt.indent_size)
-		indent = 2 * opt.indent_size;
 
 	enum paren_level_cast cast = cast_unknown;
 	if (ps.prev_lsym == lsym_offsetof
@@ -532,7 +527,7 @@ unbalanced:
 	buf_add_char(&code, token.s[0]);
 
 	if (ps.spaced_expr_psym != psym_0 && ps.nparen == 0) {
-		if (ps.extra_expr_indent == eei_yes)
+		if (ps.extra_expr_indent == eei_maybe)
 			ps.extra_expr_indent = eei_last;
 		ps.force_nl = true;
 		ps.next_unary = true;

Index: src/usr.bin/indent/indent.h
diff -u src/usr.bin/indent/indent.h:1.179 src/usr.bin/indent/indent.h:1.180
--- src/usr.bin/indent/indent.h:1.179	Thu Jun  8 21:18:54 2023
+++ src/usr.bin/indent/indent.h	Fri Jun  9 08:10:58 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.h,v 1.179 2023/06/08 21:18:54 rillig Exp $	*/
+/*	$NetBSD: indent.h,v 1.180 2023/06/09 08:10:58 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -351,7 +351,7 @@ extern struct parser_state {
 
 	enum {
 		eei_no,
-		eei_yes,
+		eei_maybe,
 		eei_last
 	} extra_expr_indent;
 

Index: src/usr.bin/indent/io.c
diff -u src/usr.bin/indent/io.c:1.206 src/usr.bin/indent/io.c:1.207
--- src/usr.bin/indent/io.c:1.206	Fri Jun  9 07:20:30 2023
+++ src/usr.bin/indent/io.c	Fri Jun  9 08:10:58 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: io.c,v 1.206 2023/06/09 07:20:30 rillig Exp $	*/
+/*	$NetBSD: io.c,v 1.207 2023/06/09 08:10:58 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: io.c,v 1.206 2023/06/09 07:20:30 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.207 2023/06/09 08:10:58 rillig Exp $");
 
 #include <stdio.h>
 
@@ -216,16 +216,16 @@ compute_code_indent_lineup(int base_ind)
 {
 	int ind = paren_indent;
 	int overflow = ind_add(ind, code.s, code.len) - opt.max_line_length;
-	if (overflow < 0)
-		return ind;
-
-	if (ind_add(base_ind, code.s, code.len) < opt.max_line_length) {
+	if (overflow >= 0
+	    && ind_add(base_ind, code.s, code.len) < opt.max_line_length) {
 		ind -= overflow + 2;
-		if (ind > base_ind)
-			return ind;
-		return base_ind;
+		if (ind < base_ind)
+			ind = base_ind;
 	}
 
+	if (ps.extra_expr_indent != eei_no
+	    && ind == base_ind + opt.indent_size)
+		ind += opt.continuation_indent;
 	return ind;
 }
 
@@ -249,10 +249,10 @@ compute_code_indent(void)
 		return compute_code_indent_lineup(base_ind);
 	}
 
-	if (ps.extra_expr_indent != eei_no)
-		return base_ind + 2 * opt.continuation_indent;
-
-	return base_ind + opt.continuation_indent * ps.line_start_nparen;
+	int rel_ind = opt.continuation_indent * ps.line_start_nparen;
+	if (ps.extra_expr_indent != eei_no && rel_ind == opt.indent_size)
+		rel_ind += opt.continuation_indent;
+	return base_ind + rel_ind;
 }
 
 static void

Reply via email to