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: implement suppressing optional blank ...



details:   https://anonhg.NetBSD.org/src/rev/9c6b063e59aa
branches:  trunk
changeset: 375919:9c6b063e59aa
user:      rillig <rillig%NetBSD.org@localhost>
date:      Mon May 22 10:28:59 2023 +0000

description:
indent: implement suppressing optional blank lines

diffstat:

 tests/usr.bin/indent/opt_sob.c |  14 +++-----------
 usr.bin/indent/debug.c         |   6 ++++--
 usr.bin/indent/indent.c        |   5 +++--
 usr.bin/indent/indent.h        |  16 +++++++++++-----
 usr.bin/indent/io.c            |  27 +++++++++++++++++++++++----
 5 files changed, 44 insertions(+), 24 deletions(-)

diffs (224 lines):

diff -r 0b15adca17b5 -r 9c6b063e59aa tests/usr.bin/indent/opt_sob.c
--- a/tests/usr.bin/indent/opt_sob.c    Mon May 22 06:50:52 2023 +0000
+++ b/tests/usr.bin/indent/opt_sob.c    Mon May 22 10:28:59 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: opt_sob.c,v 1.6 2023/05/11 09:28:53 rillig Exp $ */
+/* $NetBSD: opt_sob.c,v 1.7 2023/05/22 10:28:59 rillig Exp $ */
 
 /*
  * Tests for the options '-sob' and '-nsob'.
@@ -62,7 +62,7 @@ function_with_2_blank_lines(void)
 /* $ The following 2 lines are "optional" and are removed due to '-sob'. */
 
 
-           var--;
+               var--;
 
 
        return var;
@@ -94,7 +94,6 @@ function_with_1_blank_line(void)
        var = value;
 
        if (var > 0)
-
                var--;
 
        return var;
@@ -106,23 +105,16 @@ int
 function_with_2_blank_lines(void)
 {
 
-
        int             var;
 
-
        var = value;
 
-
        if (var > 0)
-
-
                var--;
 
-
        return var;
 
-
 }
 //indent end
 
-//indent run-equals-prev-output -nsob
+//indent run-equals-input -nsob
diff -r 0b15adca17b5 -r 9c6b063e59aa usr.bin/indent/debug.c
--- a/usr.bin/indent/debug.c    Mon May 22 06:50:52 2023 +0000
+++ b/usr.bin/indent/debug.c    Mon May 22 10:28:59 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: debug.c,v 1.19 2023/05/20 11:53:53 rillig Exp $        */
+/*     $NetBSD: debug.c,v 1.20 2023/05/22 10:28:59 rillig Exp $        */
 
 /*-
  * Copyright (c) 2023 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: debug.c,v 1.19 2023/05/20 11:53:53 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.20 2023/05/22 10:28:59 rillig Exp $");
 
 #include <stdarg.h>
 
@@ -119,8 +119,10 @@ const char *const paren_level_cast_name[
 
 const char *const line_kind_name[] = {
        "other",
+       "blank",
        "#if",
        "#endif",
+       "stmt head",
        "}",
        "block comment",
 };
diff -r 0b15adca17b5 -r 9c6b063e59aa usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Mon May 22 06:50:52 2023 +0000
+++ b/usr.bin/indent/indent.c   Mon May 22 10:28:59 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.c,v 1.302 2023/05/21 10:05:20 rillig Exp $      */
+/*     $NetBSD: indent.c,v 1.303 2023/05/22 10:28:59 rillig Exp $      */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: indent.c,v 1.302 2023/05/21 10:05:20 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.303 2023/05/22 10:28:59 rillig Exp $");
 
 #include <sys/param.h>
 #include <err.h>
@@ -515,6 +515,7 @@ unbalanced:
                parse(ps.spaced_expr_psym);
                ps.spaced_expr_psym = psym_0;
                ps.want_blank = true;
+               out.line_kind = lk_stmt_head;
        }
 }
 
diff -r 0b15adca17b5 -r 9c6b063e59aa usr.bin/indent/indent.h
--- a/usr.bin/indent/indent.h   Mon May 22 06:50:52 2023 +0000
+++ b/usr.bin/indent/indent.h   Mon May 22 10:28:59 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.h,v 1.156 2023/05/20 11:53:53 rillig Exp $      */
+/*     $NetBSD: indent.h,v 1.157 2023/05/22 10:28:59 rillig Exp $      */
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -407,14 +407,20 @@ extern struct parser_state {
 extern struct output_state {
        enum line_kind {
                lk_other,
+               lk_blank,
                lk_if,          /* #if, #ifdef, #ifndef */
                lk_endif,       /* #endif */
+               lk_stmt_head,   /* the ')' of an incomplete statement such as
+                                * 'if (expr)' or 'for (expr; expr; expr)' */
                lk_func_end,    /* the last '}' of a function body */
                lk_block_comment,
-       } line_kind;            /* kind of the current output line, is reset to
-                                * lk_other at the beginning of each output
-                                * line; used for inserting blank lines */
-       enum line_kind prev_line_kind;
+       } line_kind;            /* kind of the line that is being prepared for
+                                * output; is reset to lk_other each time after
+                                * trying to send a line to the output, even if
+                                * that line was a suppressed blank line; used
+                                * for inserting or removing blank lines */
+       enum line_kind prev_line_kind;  /* the kind of line that was actually
+                                        * sent to the output */
 
        struct buffer indent_off_text;  /* text from between 'INDENT OFF' and
                                         * 'INDENT ON', both inclusive */
diff -r 0b15adca17b5 -r 9c6b063e59aa usr.bin/indent/io.c
--- a/usr.bin/indent/io.c       Mon May 22 06:50:52 2023 +0000
+++ b/usr.bin/indent/io.c       Mon May 22 10:28:59 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: io.c,v 1.184 2023/05/20 12:05:01 rillig Exp $  */
+/*     $NetBSD: io.c,v 1.185 2023/05/22 10:28:59 rillig Exp $  */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: io.c,v 1.184 2023/05/20 12:05:01 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.185 2023/05/22 10:28:59 rillig Exp $");
 
 #include <stdio.h>
 
@@ -161,6 +161,16 @@ want_blank_line(void)
        return false;
 }
 
+static bool
+is_blank_line_optional(void)
+{
+       if (out.prev_line_kind == lk_stmt_head)
+               return wrote_newlines >= 1;
+       if (ps.tos >= 2)
+               return wrote_newlines >= 2;
+       return wrote_newlines >= 3;
+}
+
 static int
 output_line_label(void)
 {
@@ -252,8 +262,11 @@ output_line(void)
        ps.is_function_definition = false;
 
        if (indent_enabled == indent_on) {
+               if (lab.len == 0 && code.len == 0 && com.len == 0)
+                       out.line_kind = lk_blank;
+
                if (want_blank_line() && wrote_newlines < 2
-                   && (lab.len > 0 || code.len > 0 || com.len > 0))
+                   && out.line_kind != lk_blank)
                        output_newline();
 
                if (ps.ind_level == 0)
@@ -266,6 +279,11 @@ output_line(void)
                        ps.blank_line_after_decl = true;
                }
 
+               if (opt.swallow_optional_blanklines
+                   && out.line_kind == lk_blank
+                   && is_blank_line_optional())
+                       goto dont_write_line;
+
                int ind = 0;
                if (lab.len > 0)
                        ind = output_line_label();
@@ -275,6 +293,7 @@ output_line(void)
                        output_line_comment(ind);
 
                output_newline();
+               out.prev_line_kind = out.line_kind;
        }
 
        if (indent_enabled == indent_last_off_line) {
@@ -283,6 +302,7 @@ output_line(void)
                out.indent_off_text.len = 0;
        }
 
+dont_write_line:
        ps.decl_on_line = ps.in_decl;   /* for proper comment indentation */
        ps.in_stmt_cont = ps.in_stmt_or_decl && !ps.in_decl;
        ps.decl_indent_done = false;
@@ -303,7 +323,6 @@ output_line(void)
        }
 
        ps.want_blank = false;
-       out.prev_line_kind = out.line_kind;
        out.line_kind = lk_other;
 }
 



Home | Main Index | Thread Index | Old Index