indent: right-trim single-line comments

This commit is contained in:
rillig 2023-06-06 07:51:35 +00:00
parent c417c79de7
commit 9f9251e958
2 changed files with 25 additions and 6 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: lsym_comment.c,v 1.17 2023/06/06 05:39:49 rillig Exp $ */ /* $NetBSD: lsym_comment.c,v 1.18 2023/06/06 07:51:35 rillig Exp $ */
/* /*
* Tests for the token lsym_comment, which starts a comment. * Tests for the token lsym_comment, which starts a comment.
@ -371,18 +371,28 @@ tab1+++ tab2--- tab3+++ tab4--- tab5+++ tab6--- tab7+++fixed comment*/
/* /*
* TODO: Trailing whitespace in a comment is ignored when determining whether the * When determining whether the comment fits in a single line, only the first
* comment fits in a single line. * trailing space or tab is kept, the others are removed.
*/ */
//indent input //indent input
/* tab: */
/* 456789 123456789 123456789 12345 */ /* 456789 123456789 123456789 12345 */
/* 456789 123456789 123456789 123456 */ /* 456789 123456789 123456789 123456 */
/* space: */
/* 456789 123456789 123456789 12345 */
/* 456789 123456789 123456789 123456 */
//indent end //indent end
//indent run -l38 //indent run -l38
/* tab: */
/* /*
* 456789 123456789 123456789 12345 * 456789 123456789 123456789 12345
*/ */
/*
* 456789 123456789 123456789 123456
*/
/* space: */
/* 456789 123456789 123456789 12345 */
/* /*
* 456789 123456789 123456789 123456 * 456789 123456789 123456789 123456
*/ */
@ -965,7 +975,7 @@ int
f(void) f(void)
{ {
if (0) if (0)
/* 12 1234 123 123456 1234 1234567 123 1234. */; /* 12 1234 123 123456 1234 1234567 123 1234. */;
} }
//indent end //indent end

View File

@ -1,4 +1,4 @@
/* $NetBSD: pr_comment.c,v 1.154 2023/06/06 07:14:20 rillig Exp $ */ /* $NetBSD: pr_comment.c,v 1.155 2023/06/06 07:51:35 rillig Exp $ */
/*- /*-
* SPDX-License-Identifier: BSD-4-Clause * SPDX-License-Identifier: BSD-4-Clause
@ -38,7 +38,7 @@
*/ */
#include <sys/cdefs.h> #include <sys/cdefs.h>
__RCSID("$NetBSD: pr_comment.c,v 1.154 2023/06/06 07:14:20 rillig Exp $"); __RCSID("$NetBSD: pr_comment.c,v 1.155 2023/06/06 07:51:35 rillig Exp $");
#include <string.h> #include <string.h>
@ -62,6 +62,10 @@ fits_in_one_line(int com_ind, int max_line_length)
{ {
for (const char *start = inp_p, *p = start; *p != '\n'; p++) { for (const char *start = inp_p, *p = start; *p != '\n'; p++) {
if (p[0] == '*' && p[1] == '/') { if (p[0] == '*' && p[1] == '/') {
while (p - inp_p >= 2
&& ch_isblank(p[-1])
&& ch_isblank(p[-2]))
p--;
int len = ind_add(com_ind + 3, int len = ind_add(com_ind + 3,
start, (size_t)(p - start)); start, (size_t)(p - start));
len += p == start || ch_isblank(p[-1]) ? 2 : 3; len += p == start || ch_isblank(p[-1]) ? 2 : 3;
@ -257,6 +261,11 @@ copy_comment_wrap_finish(int line_length, bool delim)
output_line(); output_line();
} }
while (com.len >= 2
&& ch_isblank(com.s[com.len - 1])
&& ch_isblank(com.s[com.len - 2]))
com.len--;
inp_p += 2; inp_p += 2;
if (com.len > 0 && ch_isblank(com.s[com.len - 1])) if (com.len > 0 && ch_isblank(com.s[com.len - 1]))
buf_add_chars(&com, "*/", 2); buf_add_chars(&com, "*/", 2);