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: reduce indentation in parse, extract ...



details:   https://anonhg.NetBSD.org/src/rev/967296f2e668
branches:  trunk
changeset: 990532:967296f2e668
user:      rillig <rillig%NetBSD.org@localhost>
date:      Fri Oct 29 22:52:50 2021 +0000

description:
indent: reduce indentation in parse, extract decl_level

No functional change.

diffstat:

 usr.bin/indent/parse.c |  36 ++++++++++++++++++++----------------
 1 files changed, 20 insertions(+), 16 deletions(-)

diffs (58 lines):

diff -r 4530f9582f6c -r 967296f2e668 usr.bin/indent/parse.c
--- a/usr.bin/indent/parse.c    Fri Oct 29 22:37:25 2021 +0000
+++ b/usr.bin/indent/parse.c    Fri Oct 29 22:52:50 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: parse.c,v 1.44 2021/10/28 22:20:08 rillig Exp $        */
+/*     $NetBSD: parse.c,v 1.45 2021/10/29 22:52:50 rillig Exp $        */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -84,6 +84,16 @@
 }
 #endif
 
+static int
+decl_level(void)
+{
+    int level = 0;
+    for (int i = ps.tos - 1; i > 0; i--)
+       if (ps.s_sym[i] == psym_decl)
+           level++;
+    return level;
+}
+
 /*
  * Shift the token onto the parser stack, or reduce it by combining it with
  * previous tokens.
@@ -106,22 +116,16 @@
        ps.search_stmt = opt.brace_same_line;
        /* indicate that following brace should be on same line */
 
-       if (ps.s_sym[ps.tos] != psym_decl) {    /* only put one declaration
-                                                * onto stack */
-           break_comma = true; /* while in declaration, newline should be
-                                * forced after comma */
-           ps.s_sym[++ps.tos] = psym_decl;
-           ps.s_ind_level[ps.tos] = ps.ind_level_follow;
+       if (ps.s_sym[ps.tos] == psym_decl)
+           break;              /* only put one declaration onto stack */
 
-           if (opt.ljust_decl) {
-               ps.ind_level = 0;
-               for (int i = ps.tos - 1; i > 0; --i)
-                   if (ps.s_sym[i] == psym_decl)
-                       ++ps.ind_level; /* indentation is number of
-                                        * declaration levels deep we are */
-               ps.ind_level_follow = ps.ind_level;
-           }
-       }
+       break_comma = true;     /* while in a declaration, force a newline
+                                * after comma */
+       ps.s_sym[++ps.tos] = psym_decl;
+       ps.s_ind_level[ps.tos] = ps.ind_level_follow;
+
+       if (opt.ljust_decl)
+           ps.ind_level_follow = ps.ind_level = decl_level();
        break;
 
     case psym_if_expr:         /* 'if' '(' <expr> ')' */



Home | Main Index | Thread Index | Old Index