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: trim trailing blank lines



details:   https://anonhg.NetBSD.org/src/rev/0bb0e85aa319
branches:  trunk
changeset: 376302:0bb0e85aa319
user:      rillig <rillig%NetBSD.org@localhost>
date:      Fri Jun 09 22:01:26 2023 +0000

description:
indent: trim trailing blank lines

diffstat:

 tests/usr.bin/indent/lsym_newline.c |  14 +++++++++++++-
 usr.bin/indent/indent.c             |  13 +++----------
 usr.bin/indent/indent.h             |   3 ++-
 usr.bin/indent/io.c                 |  37 +++++++++++++++++++++++++++++++++----
 4 files changed, 51 insertions(+), 16 deletions(-)

diffs (177 lines):

diff -r fae900486a94 -r 0bb0e85aa319 tests/usr.bin/indent/lsym_newline.c
--- a/tests/usr.bin/indent/lsym_newline.c       Fri Jun 09 21:41:52 2023 +0000
+++ b/tests/usr.bin/indent/lsym_newline.c       Fri Jun 09 22:01:26 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: lsym_newline.c,v 1.4 2022/04/24 10:36:37 rillig Exp $ */
+/* $NetBSD: lsym_newline.c,v 1.5 2023/06/09 22:01:26 rillig Exp $ */
 
 /*
  * Tests for the token lsym_newline, which represents a forced line break in
@@ -32,3 +32,15 @@ 1
 + 3
 + 4;
 //indent end
+
+
+// Trim trailing blank lines.
+//indent input
+int x;
+
+
+//indent end
+
+//indent run -di0
+int x;
+//indent end
diff -r fae900486a94 -r 0bb0e85aa319 usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Fri Jun 09 21:41:52 2023 +0000
+++ b/usr.bin/indent/indent.c   Fri Jun 09 22:01:26 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.c,v 1.347 2023/06/09 16:23:43 rillig Exp $      */
+/*     $NetBSD: indent.c,v 1.348 2023/06/09 22:01:26 rillig Exp $      */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: indent.c,v 1.347 2023/06/09 16:23:43 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.348 2023/06/09 22:01:26 rillig Exp $");
 
 #include <sys/param.h>
 #include <err.h>
@@ -83,7 +83,6 @@ struct buffer com;
 bool found_err;
 bool had_eof;
 int line_no = 1;
-enum indent_enabled indent_enabled;
 
 static int ifdef_level;
 static struct parser_state state_stack[5];
@@ -380,17 +379,11 @@ is_function_pointer_declaration(void)
 static int
 process_eof(void)
 {
-       if (lab.len > 0 || code.len > 0 || com.len > 0)
-               output_line();
-       if (indent_enabled != indent_on) {
-               indent_enabled = indent_last_off_line;
-               output_line();
-       }
+       output_finish();
 
        if (ps.psyms.top > 1)   /* check for balanced braces */
                diag(1, "Stuff missing from end of file");
 
-       fflush(output);
        return found_err ? EXIT_FAILURE : EXIT_SUCCESS;
 }
 
diff -r fae900486a94 -r 0bb0e85aa319 usr.bin/indent/indent.h
--- a/usr.bin/indent/indent.h   Fri Jun 09 21:41:52 2023 +0000
+++ b/usr.bin/indent/indent.h   Fri Jun 09 22:01:26 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.h,v 1.182 2023/06/09 16:23:43 rillig Exp $      */
+/*     $NetBSD: indent.h,v 1.183 2023/06/09 22:01:26 rillig Exp $      */
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -472,6 +472,7 @@ int ind_add(int, const char *, size_t);
 
 void inp_skip(void);
 char inp_next(void);
+void output_finish(void);
 
 lexer_symbol lexi(void);
 void diag(int, const char *, ...) __printflike(2, 3);
diff -r fae900486a94 -r 0bb0e85aa319 usr.bin/indent/io.c
--- a/usr.bin/indent/io.c       Fri Jun 09 21:41:52 2023 +0000
+++ b/usr.bin/indent/io.c       Fri Jun 09 22:01:26 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: io.c,v 1.207 2023/06/09 08:10:58 rillig Exp $  */
+/*     $NetBSD: io.c,v 1.208 2023/06/09 22:01:26 rillig Exp $  */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: io.c,v 1.207 2023/06/09 08:10:58 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.208 2023/06/09 22:01:26 rillig Exp $");
 
 #include <stdio.h>
 
@@ -48,10 +48,12 @@ struct buffer inp;
 const char *inp_p;
 
 struct output_state out;
+enum indent_enabled indent_enabled;
 static int out_ind;            /* width of the line that is being written */
 static unsigned wrote_newlines = 2;    /* 0 in the middle of a line, 1 after a
                                         * single '\n', > 1 means there were (n
                                         * - 1) blank lines above */
+static unsigned buffered_blank_lines;
 static int paren_indent;
 
 
@@ -108,15 +110,24 @@ inp_next(void)
 static void
 output_newline(void)
 {
-       fputc('\n', output);
-       debug_println("output_newline");
+       buffered_blank_lines++;
        wrote_newlines++;
        out_ind = 0;
 }
 
 static void
+write_buffered_blank_lines(void)
+{
+       for (; buffered_blank_lines > 0; buffered_blank_lines--) {
+               fputc('\n', output);
+               debug_println("output_newline");
+       }
+}
+
+static void
 output_range(const char *s, size_t len)
 {
+       write_buffered_blank_lines();
        fwrite(s, 1, len, output);
        debug_vis_range("output_range \"", s, len, "\"\n");
        for (size_t i = 0; i < len; i++)
@@ -127,6 +138,8 @@ output_range(const char *s, size_t len)
 static void
 output_indent(int new_ind)
 {
+       write_buffered_blank_lines();
+
        int ind = out_ind;
 
        if (opt.use_tabs) {
@@ -148,6 +161,22 @@ output_indent(int new_ind)
        out_ind = ind;
 }
 
+void
+output_finish(void)
+{
+       if (lab.len > 0 || code.len > 0 || com.len > 0)
+               output_line();
+       if (indent_enabled == indent_on) {
+               if (buffered_blank_lines > 1)
+                       buffered_blank_lines = 1;
+               write_buffered_blank_lines();
+       } else {
+               indent_enabled = indent_last_off_line;
+               output_line();
+       }
+       fflush(output);
+}
+
 static bool
 want_blank_line(void)
 {



Home | Main Index | Thread Index | Old Index