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: fix indentation of multi-line '?:' ex...



details:   https://anonhg.NetBSD.org/src/rev/7042d1c659f4
branches:  trunk
changeset: 375337:7042d1c659f4
user:      rillig <rillig%NetBSD.org@localhost>
date:      Mon May 15 10:13:40 2023 +0000

description:
indent: fix indentation of multi-line '?:' expressions in functions

diffstat:

 tests/usr.bin/indent/lsym_question.c |  19 +++++++++++++------
 usr.bin/indent/indent.c              |  14 ++++++++++++--
 usr.bin/indent/indent.h              |   8 ++++----
 3 files changed, 29 insertions(+), 12 deletions(-)

diffs (97 lines):

diff -r 02902bd3cd67 -r 7042d1c659f4 tests/usr.bin/indent/lsym_question.c
--- a/tests/usr.bin/indent/lsym_question.c      Mon May 15 09:53:32 2023 +0000
+++ b/tests/usr.bin/indent/lsym_question.c      Mon May 15 10:13:40 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: lsym_question.c,v 1.4 2022/04/24 09:04:12 rillig Exp $ */
+/* $NetBSD: lsym_question.c,v 1.5 2023/05/15 10:13:40 rillig Exp $ */
 
 /*
  * Tests for the token lsym_question, which represents the '?' in a '?:'
@@ -56,11 +56,18 @@ void
 function(void)
 {
        const char *branch = cond
-       // $ TODO: Indent these continuation lines as they are part of the
-       // $ TODO: initializer expression, not of the declarator part to the
-       // $ TODO: left of the '='.
-       ? "then"
-       : "else";
+               ? "then"
+               : "else";
+
+       const char *multiple_branches = cond1
+               ? "then 1"
+               : cond2
+               ? "then 2"
+               : "else";
+
+       const char *condensed = cond1 ? "condensed 1"
+               : cond2 ? "condensed 2"
+               : "condensed else";
 }
 //indent end
 
diff -r 02902bd3cd67 -r 7042d1c659f4 usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Mon May 15 09:53:32 2023 +0000
+++ b/usr.bin/indent/indent.c   Mon May 15 10:13:40 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.c,v 1.273 2023/05/15 09:22:53 rillig Exp $      */
+/*     $NetBSD: indent.c,v 1.274 2023/05/15 10:13:40 rillig Exp $      */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: indent.c,v 1.273 2023/05/15 09:22:53 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.274 2023/05/15 10:13:40 rillig Exp $");
 
 #include <sys/param.h>
 #include <err.h>
@@ -532,6 +532,11 @@ static void
 process_question(void)
 {
     ps.quest_level++;
+    if (code.len == 0) {
+       ps.in_stmt_cont = true;
+       ps.in_stmt_or_decl = true;
+       ps.in_decl = false;
+    }
     if (ps.want_blank)
        buf_add_char(&code, ' ');
     buf_add_char(&code, '?');
@@ -543,6 +548,11 @@ process_colon(void)
 {
     if (ps.quest_level > 0) {  /* part of a '?:' operator */
        ps.quest_level--;
+       if (code.len == 0) {
+           ps.in_stmt_cont = true;
+           ps.in_stmt_or_decl = true;
+           ps.in_decl = false;
+       }
        if (ps.want_blank)
            buf_add_char(&code, ' ');
        buf_add_char(&code, ':');
diff -r 02902bd3cd67 -r 7042d1c659f4 usr.bin/indent/indent.h
--- a/usr.bin/indent/indent.h   Mon May 15 09:53:32 2023 +0000
+++ b/usr.bin/indent/indent.h   Mon May 15 10:13:40 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.h,v 1.136 2023/05/15 09:53:32 rillig Exp $      */
+/*     $NetBSD: indent.h,v 1.137 2023/05/15 10:13:40 rillig Exp $      */
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -297,9 +297,9 @@ extern struct parser_state {
                                 * currently prepared for output */
     int ind_level_follow;      /* the level to which ind_level should be set
                                 * after the current line is printed */
-    bool in_stmt_cont;         /* whether the next line should have an extra
-                                * indentation level because we are in the
-                                * middle of a statement */
+    bool in_stmt_cont;         /* whether the current line should have an
+                                * extra indentation level because we are in
+                                * the middle of a statement */
     int decl_level;            /* current nesting level for a structure
                                 * declaration or an initializer */
     bool decl_indent_done;     /* whether the indentation for a declaration



Home | Main Index | Thread Index | Old Index