Module Name: src Committed By: rillig Date: Mon May 15 10:13:40 UTC 2023
Modified Files: src/tests/usr.bin/indent: lsym_question.c src/usr.bin/indent: indent.c indent.h Log Message: indent: fix indentation of multi-line '?:' expressions in functions To generate a diff of this commit: cvs rdiff -u -r1.4 -r1.5 src/tests/usr.bin/indent/lsym_question.c cvs rdiff -u -r1.273 -r1.274 src/usr.bin/indent/indent.c cvs rdiff -u -r1.136 -r1.137 src/usr.bin/indent/indent.h 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/lsym_question.c diff -u src/tests/usr.bin/indent/lsym_question.c:1.4 src/tests/usr.bin/indent/lsym_question.c:1.5 --- src/tests/usr.bin/indent/lsym_question.c:1.4 Sun Apr 24 09:04:12 2022 +++ src/tests/usr.bin/indent/lsym_question.c Mon May 15 10:13:40 2023 @@ -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 Index: src/usr.bin/indent/indent.c diff -u src/usr.bin/indent/indent.c:1.273 src/usr.bin/indent/indent.c:1.274 --- src/usr.bin/indent/indent.c:1.273 Mon May 15 09:22:53 2023 +++ src/usr.bin/indent/indent.c Mon May 15 10:13:40 2023 @@ -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, ':'); Index: src/usr.bin/indent/indent.h diff -u src/usr.bin/indent/indent.h:1.136 src/usr.bin/indent/indent.h:1.137 --- src/usr.bin/indent/indent.h:1.136 Mon May 15 09:53:32 2023 +++ src/usr.bin/indent/indent.h Mon May 15 10:13:40 2023 @@ -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