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: compute indentation of 'case' labels ...



details:   https://anonhg.NetBSD.org/src/rev/6380d5f91a87
branches:  trunk
changeset: 376245:6380d5f91a87
user:      rillig <rillig%NetBSD.org@localhost>
date:      Tue Jun 06 04:37:26 2023 +0000

description:
indent: compute indentation of 'case' labels on-demand

One less moving part to keep track of.

No functional change.

diffstat:

 tests/usr.bin/indent/opt_cli.c          |  27 ++++++++++++++++++++++++++-
 tests/usr.bin/indent/psym_switch_expr.c |  20 +++++++++++++++-----
 usr.bin/indent/indent.c                 |   5 ++---
 usr.bin/indent/indent.h                 |   5 +----
 usr.bin/indent/io.c                     |  16 +++++++++++++---
 usr.bin/indent/parse.c                  |  10 +++-------
 6 files changed, 60 insertions(+), 23 deletions(-)

diffs (207 lines):

diff -r 1b5014546b5c -r 6380d5f91a87 tests/usr.bin/indent/opt_cli.c
--- a/tests/usr.bin/indent/opt_cli.c    Mon Jun 05 22:36:58 2023 +0000
+++ b/tests/usr.bin/indent/opt_cli.c    Tue Jun 06 04:37:26 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: opt_cli.c,v 1.5 2022/04/24 09:04:12 rillig Exp $ */
+/* $NetBSD: opt_cli.c,v 1.6 2023/06/06 04:37:27 rillig Exp $ */
 
 /*
  * Tests for the option '-cli' ("case label indentation"), which sets the
@@ -72,3 +72,28 @@ classify(int n)
        }
 }
 //indent end
+
+//indent run -cli3.25
+void
+classify(int n)
+{
+       switch (n) {
+                                 case 0:
+                                       print("zero");
+                                       break;
+                                 case 1:
+                                       print("one");
+                                       break;
+                                 case 2:
+                                 case 3:
+                                       print("prime");
+                                       break;
+                                 case 4:
+                                       print("square");
+                                       break;
+                                 default:
+                                       print("large");
+                                       break;
+       }
+}
+//indent end
diff -r 1b5014546b5c -r 6380d5f91a87 tests/usr.bin/indent/psym_switch_expr.c
--- a/tests/usr.bin/indent/psym_switch_expr.c   Mon Jun 05 22:36:58 2023 +0000
+++ b/tests/usr.bin/indent/psym_switch_expr.c   Tue Jun 06 04:37:26 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: psym_switch_expr.c,v 1.4 2022/04/24 09:04:12 rillig Exp $ */
+/* $NetBSD: psym_switch_expr.c,v 1.5 2023/06/06 04:37:27 rillig Exp $ */
 
 /*
  * Tests for the parser symbol psym_switch_expr, which represents the keyword
@@ -6,10 +6,6 @@
  * statement (usually a block) containing the 'case' labels.
  */
 
-//indent input
-// TODO: add input
-//indent end
-
 //indent run-equals-input
 
 
@@ -43,3 +39,17 @@ function(void)
                }
 }
 //indent end
+
+//indent run -cli-0.375
+void
+function(void)
+{
+       switch (expr)
+               if (cond) {
+     case 1:
+                       return;
+     case 2:
+                       break;
+               }
+}
+//indent end
diff -r 1b5014546b5c -r 6380d5f91a87 usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Mon Jun 05 22:36:58 2023 +0000
+++ b/usr.bin/indent/indent.c   Tue Jun 06 04:37:26 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.c,v 1.336 2023/06/05 20:56:18 rillig Exp $      */
+/*     $NetBSD: indent.c,v 1.337 2023/06/06 04:37:26 rillig Exp $      */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: indent.c,v 1.336 2023/06/05 20:56:18 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.337 2023/06/06 04:37:26 rillig Exp $");
 
 #include <sys/param.h>
 #include <err.h>
@@ -81,7 +81,6 @@ struct buffer code;
 struct buffer com;
 
 bool found_err;
-float case_ind;
 bool had_eof;
 int line_no = 1;
 enum indent_enabled indent_enabled;
diff -r 1b5014546b5c -r 6380d5f91a87 usr.bin/indent/indent.h
--- a/usr.bin/indent/indent.h   Mon Jun 05 22:36:58 2023 +0000
+++ b/usr.bin/indent/indent.h   Tue Jun 06 04:37:26 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.h,v 1.175 2023/06/05 14:22:26 rillig Exp $      */
+/*     $NetBSD: indent.h,v 1.176 2023/06/06 04:37:26 rillig Exp $      */
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -247,8 +247,6 @@ extern struct options {
 } opt;
 
 extern bool found_err;
-extern float case_ind;         /* indentation level to be used for a "case n:"
-                                */
 extern bool had_eof;           /* whether input is exhausted */
 extern int line_no;            /* the current line number. */
 extern enum indent_enabled {
@@ -355,7 +353,6 @@ extern struct parser_state {
        int tos;                /* pointer to top of stack */
        parser_symbol s_sym[STACKSIZE];
        int s_ind_level[STACKSIZE];
-       float s_case_ind_level[STACKSIZE];
 
        /* Spacing inside a statement or declaration */
 
diff -r 1b5014546b5c -r 6380d5f91a87 usr.bin/indent/io.c
--- a/usr.bin/indent/io.c       Mon Jun 05 22:36:58 2023 +0000
+++ b/usr.bin/indent/io.c       Tue Jun 06 04:37:26 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: io.c,v 1.198 2023/06/05 12:06:51 rillig Exp $  */
+/*     $NetBSD: io.c,v 1.199 2023/06/06 04:37:26 rillig Exp $  */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: io.c,v 1.198 2023/06/05 12:06:51 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.199 2023/06/06 04:37:26 rillig Exp $");
 
 #include <stdio.h>
 
@@ -362,11 +362,21 @@ compute_code_indent(void)
                    opt.continuation_indent * ps.line_start_nparen;
 }
 
+static int
+compute_case_label_indent(void)
+{
+       int i = ps.tos;
+       while (i > 0 && ps.s_sym[i] != psym_switch_expr)
+               i--;
+       float case_ind = (float)ps.s_ind_level[i] + opt.case_indent;
+       return (int)(case_ind * (float)opt.indent_size);
+}
+
 int
 compute_label_indent(void)
 {
        if (out.line_kind == lk_case_or_default)
-               return (int)(case_ind * (float)opt.indent_size);
+               return compute_case_label_indent();
        if (lab.s[0] == '#')
                return 0;
        return opt.indent_size * (ps.ind_level - 2);
diff -r 1b5014546b5c -r 6380d5f91a87 usr.bin/indent/parse.c
--- a/usr.bin/indent/parse.c    Mon Jun 05 22:36:58 2023 +0000
+++ b/usr.bin/indent/parse.c    Tue Jun 06 04:37:26 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: parse.c,v 1.66 2023/06/05 07:35:05 rillig Exp $        */
+/*     $NetBSD: parse.c,v 1.67 2023/06/06 04:37:26 rillig Exp $        */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: parse.c,v 1.66 2023/06/05 07:35:05 rillig Exp $");
+__RCSID("$NetBSD: parse.c,v 1.67 2023/06/06 04:37:26 rillig Exp $");
 
 #include <err.h>
 
@@ -183,8 +183,6 @@ parse(parser_symbol psym)
 
        case psym_switch_expr:
                ps_push_follow(psym_switch_expr);
-               ps.s_case_ind_level[ps.tos] = case_ind;
-               case_ind = (float)ps.ind_level_follow + opt.case_indent;
                ps.ind_level_follow += (int)opt.case_indent + 1;
                break;
 
@@ -239,9 +237,7 @@ reduce_stmt(void)
                return true;
 
        case psym_switch_expr:
-               case_ind = ps.s_case_ind_level[ps.tos - 1];
-               /* FALLTHROUGH */
-       case psym_decl:         /* finish of a declaration */
+       case psym_decl:
        case psym_if_expr_stmt_else:
        case psym_for_exprs:
        case psym_while_expr:



Home | Main Index | Thread Index | Old Index