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: when an indentation is ambiguous, ind...



details:   https://anonhg.NetBSD.org/src/rev/6dfbedf630de
branches:  trunk
changeset: 376286:6dfbedf630de
user:      rillig <rillig%NetBSD.org@localhost>
date:      Fri Jun 09 08:10:58 2023 +0000

description:
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.

diffstat:

 tests/usr.bin/indent/opt_eei.c |  27 ++++++++++-----------------
 usr.bin/indent/debug.c         |   6 +++---
 usr.bin/indent/indent.c        |  15 +++++----------
 usr.bin/indent/indent.h        |   4 ++--
 usr.bin/indent/io.c            |  26 +++++++++++++-------------
 5 files changed, 33 insertions(+), 45 deletions(-)

diffs (219 lines):

diff -r d607887c7e8b -r 6dfbedf630de tests/usr.bin/indent/opt_eei.c
--- a/tests/usr.bin/indent/opt_eei.c    Fri Jun 09 07:54:05 2023 +0000
+++ b/tests/usr.bin/indent/opt_eei.c    Fri Jun 09 08:10:58 2023 +0000
@@ -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;
 }
diff -r d607887c7e8b -r 6dfbedf630de usr.bin/indent/debug.c
--- a/usr.bin/indent/debug.c    Fri Jun 09 07:54:05 2023 +0000
+++ b/usr.bin/indent/debug.c    Fri Jun 09 08:10:58 2023 +0000
@@ -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",
 };
 
diff -r d607887c7e8b -r 6dfbedf630de usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Fri Jun 09 07:54:05 2023 +0000
+++ b/usr.bin/indent/indent.c   Fri Jun 09 08:10:58 2023 +0000
@@ -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;
diff -r d607887c7e8b -r 6dfbedf630de usr.bin/indent/indent.h
--- a/usr.bin/indent/indent.h   Fri Jun 09 07:54:05 2023 +0000
+++ b/usr.bin/indent/indent.h   Fri Jun 09 08:10:58 2023 +0000
@@ -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;
 
diff -r d607887c7e8b -r 6dfbedf630de usr.bin/indent/io.c
--- a/usr.bin/indent/io.c       Fri Jun 09 07:54:05 2023 +0000
+++ b/usr.bin/indent/io.c       Fri Jun 09 08:10:58 2023 +0000
@@ -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



Home | Main Index | Thread Index | Old Index