diff options
| author | rillig <rillig@NetBSD.org> | 2023-06-26 20:03:09 +0000 |
|---|---|---|
| committer | rillig <rillig@NetBSD.org> | 2023-06-26 20:03:09 +0000 |
| commit | 4742c517f8e3fcb9a3265d2d2f8abdd325e0bdee (patch) | |
| tree | 16f338e80acff451291dfcb4002b1cb44b6f8d2c /usr.bin | |
| parent | 274c5f621f37cf5ab33873db54ec09550d36e21e (diff) | |
indent: implement 'blank line above first statement in function body'
Diffstat (limited to 'usr.bin')
| -rw-r--r-- | usr.bin/indent/debug.c | 13 | ||||
| -rw-r--r-- | usr.bin/indent/indent.c | 22 | ||||
| -rw-r--r-- | usr.bin/indent/indent.h | 16 | ||||
| -rw-r--r-- | usr.bin/indent/io.c | 12 |
4 files changed, 56 insertions, 7 deletions
diff --git a/usr.bin/indent/debug.c b/usr.bin/indent/debug.c index b2d54a648d1..18ec528d8fc 100644 --- a/usr.bin/indent/debug.c +++ b/usr.bin/indent/debug.c @@ -1,4 +1,4 @@ -/* $NetBSD: debug.c,v 1.68 2023/06/23 20:43:21 rillig Exp $ */ +/* $NetBSD: debug.c,v 1.69 2023/06/26 20:03:09 rillig Exp $ */ /*- * Copyright (c) 2023 The NetBSD Foundation, Inc. @@ -30,7 +30,7 @@ */ #include <sys/cdefs.h> -__RCSID("$NetBSD: debug.c,v 1.68 2023/06/23 20:43:21 rillig Exp $"); +__RCSID("$NetBSD: debug.c,v 1.69 2023/06/26 20:03:09 rillig Exp $"); #include <stdarg.h> #include <string.h> @@ -113,6 +113,14 @@ static const char *const declaration_name[] = { "end", }; +static const char *const badp_name[] = { + "none", + "seen{", + "decl", + "seen_decl", + "yes", +}; + const char *const paren_level_cast_name[] = { "(unknown cast)", "(maybe cast)", @@ -370,6 +378,7 @@ debug_parser_state(void) debug_ps_bool(break_after_comma); debug_ps_bool(want_newline); debug_ps_enum(declaration, declaration_name); + debug_ps_enum(badp, badp_name); debug_ps_bool(blank_line_after_decl); state.heading = NULL; diff --git a/usr.bin/indent/indent.c b/usr.bin/indent/indent.c index 10304330a6d..20a2d4bcf4b 100644 --- a/usr.bin/indent/indent.c +++ b/usr.bin/indent/indent.c @@ -1,4 +1,4 @@ -/* $NetBSD: indent.c,v 1.385 2023/06/26 14:54:40 rillig Exp $ */ +/* $NetBSD: indent.c,v 1.386 2023/06/26 20:03:09 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-4-Clause @@ -38,7 +38,7 @@ */ #include <sys/cdefs.h> -__RCSID("$NetBSD: indent.c,v 1.385 2023/06/26 14:54:40 rillig Exp $"); +__RCSID("$NetBSD: indent.c,v 1.386 2023/06/26 20:03:09 rillig Exp $"); #include <sys/param.h> #include <err.h> @@ -352,6 +352,22 @@ update_ps_lbrace_kind(lexer_symbol lsym) } static void +update_ps_badp(lexer_symbol lsym) +{ + if (lsym == lsym_lbrace && ps.lbrace_kind == psym_lbrace_block + && ps.psyms.len == 3) + ps.badp = badp_seen_lbrace; + if (lsym == lsym_rbrace && ps.decl_level == 0) + ps.badp = badp_none; + if (lsym == lsym_type && ps.paren.len == 0 + && (ps.badp == badp_seen_lbrace || ps.badp == badp_yes)) + ps.badp = badp_decl; + if (lsym == lsym_semicolon && ps.badp == badp_decl + && ps.decl_level == 0) + ps.badp = badp_seen_decl; +} + +static void indent_declarator(int decl_ind, bool tabs_to_var) { int base = ps.ind_level * opt.indent_size; @@ -1122,6 +1138,8 @@ indent(void) process_lsym(lsym); + if (opt.blank_line_after_decl_at_top) + update_ps_badp(lsym); if (lsym != lsym_preprocessing && lsym != lsym_newline && lsym != lsym_comment) diff --git a/usr.bin/indent/indent.h b/usr.bin/indent/indent.h index 24e9ee7b027..6c2bb608b29 100644 --- a/usr.bin/indent/indent.h +++ b/usr.bin/indent/indent.h @@ -1,4 +1,4 @@ -/* $NetBSD: indent.h,v 1.203 2023/06/23 20:43:21 rillig Exp $ */ +/* $NetBSD: indent.h,v 1.204 2023/06/26 20:03:09 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD @@ -404,6 +404,20 @@ extern struct parser_state { decl_end, /* finished a declaration */ } declaration; bool blank_line_after_decl; + + enum { + badp_none, + badp_seen_lbrace, + badp_decl, /* in an unfinished declaration in the first + * block of declarations in a function body */ + badp_seen_decl, /* seen the semicolon of a declaration; the + * next line is a candidate for inserting a + * blank line above */ + badp_yes, /* this line is a candidate for inserting a + * blank line above, unless the line turns out + * to start a declaration */ + } badp; /* insert a blank line before the first + * statement in a function body */ } ps; extern struct output_state { diff --git a/usr.bin/indent/io.c b/usr.bin/indent/io.c index f0ce5561f5b..a8cb347d46c 100644 --- a/usr.bin/indent/io.c +++ b/usr.bin/indent/io.c @@ -1,4 +1,4 @@ -/* $NetBSD: io.c,v 1.230 2023/06/26 14:54:40 rillig Exp $ */ +/* $NetBSD: io.c,v 1.231 2023/06/26 20:03:09 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-4-Clause @@ -38,7 +38,7 @@ */ #include <sys/cdefs.h> -__RCSID("$NetBSD: io.c,v 1.230 2023/06/26 14:54:40 rillig Exp $"); +__RCSID("$NetBSD: io.c,v 1.231 2023/06/26 20:03:09 rillig Exp $"); #include <stdio.h> @@ -176,6 +176,11 @@ want_blank_line(void) ps.blank_line_after_decl = false; return true; } + if (ps.badp == badp_yes) { + ps.badp = badp_none; + return true; + } + if (opt.blank_line_around_conditional_compilation) { if (out.prev_line_kind != lk_pre_if && out.line_kind == lk_pre_if) @@ -400,6 +405,9 @@ prepare_next_line(void) ps.ind_level = ps.ind_level_follow; ps.ind_paren_level = (int)ps.paren.len; ps.want_blank = false; + if ((ps.badp == badp_seen_lbrace || ps.badp == badp_seen_decl) + && !ps.in_decl) + ps.badp = badp_yes; if (ps.paren.len > 0) { /* TODO: explain what negative indentation means */ |
