Module Name:    src
Committed By:   rillig
Date:           Mon May 22 10:28:59 UTC 2023

Modified Files:
        src/tests/usr.bin/indent: opt_sob.c
        src/usr.bin/indent: debug.c indent.c indent.h io.c

Log Message:
indent: implement suppressing optional blank lines


To generate a diff of this commit:
cvs rdiff -u -r1.6 -r1.7 src/tests/usr.bin/indent/opt_sob.c
cvs rdiff -u -r1.19 -r1.20 src/usr.bin/indent/debug.c
cvs rdiff -u -r1.302 -r1.303 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.156 -r1.157 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.184 -r1.185 src/usr.bin/indent/io.c

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/opt_sob.c
diff -u src/tests/usr.bin/indent/opt_sob.c:1.6 src/tests/usr.bin/indent/opt_sob.c:1.7
--- src/tests/usr.bin/indent/opt_sob.c:1.6	Thu May 11 09:28:53 2023
+++ src/tests/usr.bin/indent/opt_sob.c	Mon May 22 10:28:59 2023
@@ -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

Index: src/usr.bin/indent/debug.c
diff -u src/usr.bin/indent/debug.c:1.19 src/usr.bin/indent/debug.c:1.20
--- src/usr.bin/indent/debug.c:1.19	Sat May 20 11:53:53 2023
+++ src/usr.bin/indent/debug.c	Mon May 22 10:28:59 2023
@@ -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",
 };

Index: src/usr.bin/indent/indent.c
diff -u src/usr.bin/indent/indent.c:1.302 src/usr.bin/indent/indent.c:1.303
--- src/usr.bin/indent/indent.c:1.302	Sun May 21 10:05:20 2023
+++ src/usr.bin/indent/indent.c	Mon May 22 10:28:59 2023
@@ -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;
 	}
 }
 

Index: src/usr.bin/indent/indent.h
diff -u src/usr.bin/indent/indent.h:1.156 src/usr.bin/indent/indent.h:1.157
--- src/usr.bin/indent/indent.h:1.156	Sat May 20 11:53:53 2023
+++ src/usr.bin/indent/indent.h	Mon May 22 10:28:59 2023
@@ -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 */

Index: src/usr.bin/indent/io.c
diff -u src/usr.bin/indent/io.c:1.184 src/usr.bin/indent/io.c:1.185
--- src/usr.bin/indent/io.c:1.184	Sat May 20 12:05:01 2023
+++ src/usr.bin/indent/io.c	Mon May 22 10:28:59 2023
@@ -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;
 }
 

Reply via email to