indent: implement 'blank after declarations'

This commit is contained in:
rillig 2023-05-13 15:34:22 +00:00
parent 61b8d55f2a
commit 2261e976c8
4 changed files with 49 additions and 24 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: opt_bad.c,v 1.8 2023/05/13 14:19:14 rillig Exp $ */
/* $NetBSD: opt_bad.c,v 1.9 2023/05/13 15:34:22 rillig Exp $ */
/*
* Tests for the options '-bad' and '-nbad'.
@ -67,10 +67,10 @@ void
function_definition(void)
{
int local_variable;
/* $ TODO: add empty line */
function_call();
int local_variable_after_statement;
/* $ TODO: add empty line */
function_call();
}
//indent end
@ -127,12 +127,18 @@ comments(void)
void
initializer(void)
{
int local_var_init_1[] = {
1
};
int local_var_init_2[] = {
1
};
int local_var_init_1[] = {1};
int local_var_init_2[] = {1};
function_call();
}
void
initializer_with_blank(void)
{
int local_var_init_1[] = {1};
int local_var_init_2[] = {1};
function_call();
}
//indent end
@ -141,13 +147,19 @@ initializer(void)
void
initializer(void)
{
int local_var_init_1[] = {
1
};
int local_var_init_2[] = {
1
};
/* $ TODO: Add blank line here. */
int local_var_init_1[] = {1};
int local_var_init_2[] = {1};
function_call();
}
void
initializer_with_blank(void)
{
int local_var_init_1[] = {1};
int local_var_init_2[] = {1};
function_call();
}
//indent end

View File

@ -1,4 +1,4 @@
/* $NetBSD: debug.c,v 1.3 2023/05/13 14:30:48 rillig Exp $ */
/* $NetBSD: debug.c,v 1.4 2023/05/13 15:34:22 rillig Exp $ */
/*-
* Copyright (c) 2023 The NetBSD Foundation, Inc.
@ -30,7 +30,7 @@
*/
#include <sys/cdefs.h>
__RCSID("$NetBSD: debug.c,v 1.3 2023/05/13 14:30:48 rillig Exp $");
__RCSID("$NetBSD: debug.c,v 1.4 2023/05/13 15:34:22 rillig Exp $");
#include "indent.h"
@ -224,6 +224,7 @@ debug_parser_state(lexer_symbol lsym)
debug_ps_bool(decl_on_line);
debug_ps_bool(in_decl);
debug_ps_enum(declaration, declaration_name);
debug_ps_bool(blank_line_after_decl);
debug_ps_bool(in_func_def_params);
debug_ps_enum(in_enum, in_enum_name);
debug_ps_bool(decl_indent_done);

View File

@ -1,4 +1,4 @@
/* $NetBSD: indent.h,v 1.127 2023/05/13 14:30:48 rillig Exp $ */
/* $NetBSD: indent.h,v 1.128 2023/05/13 15:34:22 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@ -306,11 +306,14 @@ extern struct parser_state {
bool in_decl; /* whether we are in a declaration. The
* processing of braces is then slightly
* different */
enum declaration {
decl_no, /* no declaration anywhere nearby */
decl_begin, /* collecting tokens of a declaration */
decl_end, /* finished a declaration */
} declaration;
bool blank_line_after_decl;
bool in_func_def_params;
enum {
in_enum_no, /* outside any 'enum { ... }' */

View File

@ -1,4 +1,4 @@
/* $NetBSD: io.c,v 1.159 2023/05/13 14:30:48 rillig Exp $ */
/* $NetBSD: io.c,v 1.160 2023/05/13 15:34:22 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@ -43,7 +43,7 @@ static char sccsid[] = "@(#)io.c 8.1 (Berkeley) 6/6/93";
#include <sys/cdefs.h>
#if defined(__NetBSD__)
__RCSID("$NetBSD: io.c,v 1.159 2023/05/13 14:30:48 rillig Exp $");
__RCSID("$NetBSD: io.c,v 1.160 2023/05/13 15:34:22 rillig Exp $");
#elif defined(__FreeBSD__)
__FBSDID("$FreeBSD: head/usr.bin/indent/io.c 334927 2018-06-10 16:44:18Z pstef $");
#endif
@ -288,10 +288,22 @@ output_complete_line(char line_terminator)
ps.is_function_definition = false;
if (ps.blank_line_after_decl && ps.declaration == decl_no) {
ps.blank_line_after_decl = false;
if (lab.e != lab.s || code.e != code.s || com.e != com.s)
output_char('\n');
}
if (!inhibit_formatting) {
if (ps.ind_level == 0)
ps.in_stmt_cont = false; /* this is a class A kludge */
if (opt.blank_line_after_decl && ps.declaration == decl_end
&& ps.tos > 1) {
ps.declaration = decl_no;
ps.blank_line_after_decl = true;
}
int ind = 0;
if (lab.e != lab.s)
ind = output_line_label();
@ -301,9 +313,6 @@ output_complete_line(char line_terminator)
output_line_comment(ind);
output_char(line_terminator);
if (ps.declaration == decl_end && opt.blank_line_after_decl)
ps.declaration = decl_no;
}
ps.decl_on_line = ps.in_decl; /* for proper comment indentation */