diff options
author | Piotr Pawel Stefaniak <pstef@FreeBSD.org> | 2018-06-03 15:28:55 +0000 |
---|---|---|
committer | Piotr Pawel Stefaniak <pstef@FreeBSD.org> | 2018-06-03 15:28:55 +0000 |
commit | 3c51c3cf5ffe336cc7f1e74d8d90ec7af6930219 (patch) | |
tree | a1e1671abfc4fa8c398b06f79047cdde5e4fffe8 /usr.bin/indent | |
parent | 63c3f226969b89226e223bca86761b285d302ed6 (diff) | |
download | src-3c51c3cf5ffe336cc7f1e74d8d90ec7af6930219.tar.gz src-3c51c3cf5ffe336cc7f1e74d8d90ec7af6930219.zip |
indent(1): improve handling of boxed comments indentation
The trick is to copy everything from the start of the line into the buffer
that stores newlines and comments until indent finds a brace or an else.
pr_comment() will use that information to calculate the original indentation
of the boxed comment.
This requires storing two pieces of information: the real start of the
buffer (sc_buf) and the start of the comment (save_com).
Notes
Notes:
svn path=/head/; revision=334563
Diffstat (limited to 'usr.bin/indent')
-rw-r--r-- | usr.bin/indent/indent.c | 16 | ||||
-rw-r--r-- | usr.bin/indent/indent_globs.h | 11 | ||||
-rw-r--r-- | usr.bin/indent/pr_comment.c | 7 | ||||
-rw-r--r-- | usr.bin/indent/tests/comments.0 | 20 | ||||
-rw-r--r-- | usr.bin/indent/tests/comments.0.stdout | 21 |
5 files changed, 67 insertions, 8 deletions
diff --git a/usr.bin/indent/indent.c b/usr.bin/indent/indent.c index 425e25197c52..d19d2c100dfd 100644 --- a/usr.bin/indent/indent.c +++ b/usr.bin/indent/indent.c @@ -341,6 +341,7 @@ main(int argc, char **argv) switch (type_code) { case newline: if (sc_end == NULL) { + save_com = sc_buf; save_com[0] = save_com[1] = ' '; sc_end = &save_com[2]; } @@ -359,6 +360,13 @@ main(int argc, char **argv) break; case comment: if (sc_end == NULL) { + /* + * Copy everything from the start of the line, because + * pr_comment() will use that to calculate original + * indentation of a boxed comment. + */ + memcpy(sc_buf, in_buffer, buf_ptr - in_buffer - 4); + save_com = sc_buf + (buf_ptr - in_buffer - 4); save_com[0] = save_com[1] = ' '; sc_end = &save_com[2]; } @@ -1172,9 +1180,11 @@ check_type: e_lab--; if (e_lab - s_lab == com_end && bp_save == NULL) { /* comment on preprocessor line */ - if (sc_end == NULL) /* if this is the first comment, we - * must set up the buffer */ - sc_end = &(save_com[0]); + if (sc_end == NULL) { /* if this is the first comment, + * we must set up the buffer */ + save_com = sc_buf; + sc_end = &save_com[0]; + } else { *sc_end++ = '\n'; /* add newline between * comments */ diff --git a/usr.bin/indent/indent_globs.h b/usr.bin/indent/indent_globs.h index c97194ff38d5..c5cc17199993 100644 --- a/usr.bin/indent/indent_globs.h +++ b/usr.bin/indent/indent_globs.h @@ -126,8 +126,9 @@ char *buf_ptr; /* ptr to next character to be taken from * in_buffer */ char *buf_end; /* ptr to first after last char in in_buffer */ -char save_com[sc_size]; /* input text is saved here when looking for +char sc_buf[sc_size]; /* input text is saved here when looking for * the brace after an if, while, etc */ +char *save_com; /* start of the comment stored in sc_buf */ char *sc_end; /* pointer into save_com buffer */ char *bp_save; /* saved value of buf_ptr when taking input @@ -241,8 +242,12 @@ struct parser_state { int box_com; /* set to true when we are in a "boxed" * comment. In that case, the first non-blank * char should be lined up with the / in / followed by * */ - int comment_delta, - n_comment_delta; + int comment_delta; /* used to set up indentation for all lines + * of a boxed comment after the first one */ + int n_comment_delta;/* remembers how many columns there were + * before the start of a box comment so that + * forthcoming lines of the comment are + * indented properly */ int cast_mask; /* indicates which close parens potentially * close off casts */ int not_cast_mask; /* indicates which close parens definitely diff --git a/usr.bin/indent/pr_comment.c b/usr.bin/indent/pr_comment.c index 6a7ce5c036dc..2ee11de51902 100644 --- a/usr.bin/indent/pr_comment.c +++ b/usr.bin/indent/pr_comment.c @@ -158,8 +158,11 @@ pr_comment(void) * The comment we're about to read usually comes from in_buffer, * unless it has been copied into save_com. */ - char *start = buf_ptr >= save_com && buf_ptr < save_com + sc_size ? bp_save : buf_ptr; - ps.n_comment_delta = 1 - count_spaces_until(1, in_buffer, start - 2); + char *start; + + start = buf_ptr >= save_com && buf_ptr < save_com + sc_size ? + sc_buf : in_buffer; + ps.n_comment_delta = 1 - count_spaces_until(1, start, buf_ptr - 2); } else { ps.n_comment_delta = 0; diff --git a/usr.bin/indent/tests/comments.0 b/usr.bin/indent/tests/comments.0 index 567d234ec1a0..7b65c2eb552b 100644 --- a/usr.bin/indent/tests/comments.0 +++ b/usr.bin/indent/tests/comments.0 @@ -30,3 +30,23 @@ void t(void) { /* r309343 */ } + +int c(void) +{ + if (1) { /*- a christmas tree * + *** + ***** */ + /*- another one * + *** + ***** */ + 7; + } + + if (1) /*- a christmas tree * + *** + ***** */ + /*- another one * + *** + ***** */ + 1; +} diff --git a/usr.bin/indent/tests/comments.0.stdout b/usr.bin/indent/tests/comments.0.stdout index 0ad246bd8179..8ca5aa518c5e 100644 --- a/usr.bin/indent/tests/comments.0.stdout +++ b/usr.bin/indent/tests/comments.0.stdout @@ -37,3 +37,24 @@ t(void) /* r309343 */ } + +int +c(void) +{ + if (1) { /*- a christmas tree * + *** + ***** */ + /*- another one * + *** + ***** */ + 7; + } + + if (1) /*- a christmas tree * + *** + ***** */ + /*- another one * + *** + ***** */ + 1; +} |