summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrillig <rillig@NetBSD.org>2023-05-16 08:22:11 +0000
committerrillig <rillig@NetBSD.org>2023-05-16 08:22:11 +0000
commitf8a523500fcd3fb827ddbf92c4b0ed047bc71f9c (patch)
treec926aad5dbadbe73adbe7f28aa92496b8b7269cb
parentedd0e944ee968ab366001b7291aa12a9aa9c84a1 (diff)
indent: remove blank between comment and parentheses or brackets
Finally, indent formats its own source code without messing up the layout.
-rw-r--r--tests/usr.bin/indent/fmt_decl.c6
-rw-r--r--usr.bin/indent/indent.c23
2 files changed, 20 insertions, 9 deletions
diff --git a/tests/usr.bin/indent/fmt_decl.c b/tests/usr.bin/indent/fmt_decl.c
index b1c636db8bb..13c440b844c 100644
--- a/tests/usr.bin/indent/fmt_decl.c
+++ b/tests/usr.bin/indent/fmt_decl.c
@@ -1,4 +1,4 @@
-/* $NetBSD: fmt_decl.c,v 1.42 2023/05/15 21:51:46 rillig Exp $ */
+/* $NetBSD: fmt_decl.c,v 1.43 2023/05/16 08:22:11 rillig Exp $ */
/*
* Tests for declarations of global variables, external functions, and local
@@ -128,7 +128,7 @@ void t2 (char *x, int y)
,n
,o
;
- int chars[ /* push the comma beyond column 74 .... */ ], x;
+ int chars[ /* push the comma beyond column 74 ...... */ ], x;
}
//indent end
@@ -145,7 +145,7 @@ t2(char *x, int y)
,n
,o
;
- int chars[ /* push the comma beyond column 74 .... */ ],
+ int chars[/* push the comma beyond column 74 ...... */],
x;
}
//indent end
diff --git a/usr.bin/indent/indent.c b/usr.bin/indent/indent.c
index eb09e4fea10..a772ff64c65 100644
--- a/usr.bin/indent/indent.c
+++ b/usr.bin/indent/indent.c
@@ -1,4 +1,4 @@
-/* $NetBSD: indent.c,v 1.287 2023/05/16 08:04:03 rillig Exp $ */
+/* $NetBSD: indent.c,v 1.288 2023/05/16 08:22:11 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: indent.c,v 1.287 2023/05/16 08:04:03 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.288 2023/05/16 08:22:11 rillig Exp $");
#include <sys/param.h>
#include <err.h>
@@ -357,13 +357,24 @@ maybe_break_line(lexer_symbol lsym)
ps.force_nl = false;
}
+static bool
+want_blank_before_comment(void)
+{
+ if (code.len > 0) {
+ char ch = code.mem[code.len - 1];
+ return ch != '[' && ch != '(';
+ }
+ return lab.len > 0;
+}
+
static void
-move_com_to_code(void)
+move_com_to_code(lexer_symbol lsym)
{
- if (lab.len > 0 || code.len > 0)
+ if (want_blank_before_comment())
buf_add_char(&code, ' ');
buf_add_buf(&code, &com);
- buf_add_char(&code, ' ');
+ if (lsym != lsym_rparen_or_rbracket)
+ buf_add_char(&code, ' ');
com.len = 0;
ps.want_blank = false;
}
@@ -1022,7 +1033,7 @@ main_loop(void)
ps.in_stmt_or_decl = true; /* add an extra level of indentation;
* turned off again by a ';' or '}' */
if (com.len > 0)
- move_com_to_code();
+ move_com_to_code(lsym);
}
switch (lsym) {