Skip to content

Commit 2bf875f

Browse files
committed
patch 8.2.4907: some users do not want a line comment always inserted
Problem: Some users do not want a line comment always inserted. Solution: Add the '/' flag to 'formatoptions' to not repeat the comment leader after a statement when using "o".
1 parent aa04e1b commit 2bf875f

File tree

5 files changed

+26
-7
lines changed

5 files changed

+26
-7
lines changed

runtime/doc/change.txt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1673,9 +1673,9 @@ readability.
16731673

16741674
letter meaning when present in 'formatoptions' ~
16751675
*fo-t*
1676-
t Auto-wrap text using textwidth
1676+
t Auto-wrap text using 'textwidth'
16771677
*fo-c*
1678-
c Auto-wrap comments using textwidth, inserting the current comment
1678+
c Auto-wrap comments using 'textwidth', inserting the current comment
16791679
leader automatically.
16801680
*fo-r*
16811681
r Automatically insert the current comment leader after hitting
@@ -1684,6 +1684,9 @@ r Automatically insert the current comment leader after hitting
16841684
o Automatically insert the current comment leader after hitting 'o' or
16851685
'O' in Normal mode. In case comment is unwanted in a specific place
16861686
use CTRL-U to quickly delete it. |i_CTRL-U|
1687+
*fo-/*
1688+
/ When 'o' is included: do not insert the comment leader for a //
1689+
comment after a statement, only when // is at the start of the line.
16871690
*fo-q*
16881691
q Allow formatting of comments with "gq".
16891692
Note that formatting will not change blank lines or lines containing
@@ -1746,8 +1749,8 @@ B When joining lines, don't insert a space between two multibyte
17461749
1 Don't break a line after a one-letter word. It's broken before it
17471750
instead (if possible).
17481751
*fo-]*
1749-
] Respect textwidth rigorously. With this flag set, no line can be
1750-
longer than textwidth, unless line-break-prohibition rules make this
1752+
] Respect 'textwidth' rigorously. With this flag set, no line can be
1753+
longer than 'textwidth', unless line-break-prohibition rules make this
17511754
impossible. Mainly for CJK scripts and works only if 'encoding' is
17521755
"utf-8".
17531756
*fo-j*

src/change.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1659,7 +1659,8 @@ open_line(
16591659
lead_len = get_leader_len(saved_line, &lead_flags,
16601660
dir == BACKWARD, TRUE);
16611661
#ifdef FEAT_CINDENT
1662-
if (lead_len == 0 && curbuf->b_p_cin && do_cindent && dir == FORWARD)
1662+
if (lead_len == 0 && curbuf->b_p_cin && do_cindent && dir == FORWARD
1663+
&& !has_format_option(FO_NO_OPEN_COMS))
16631664
{
16641665
// Check for a line comment after code.
16651666
comment_start = check_linecomment(saved_line);

src/option.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ typedef enum {
141141
#define FO_WRAP_COMS 'c'
142142
#define FO_RET_COMS 'r'
143143
#define FO_OPEN_COMS 'o'
144+
#define FO_NO_OPEN_COMS '/'
144145
#define FO_Q_COMS 'q'
145146
#define FO_Q_NUMBER 'n'
146147
#define FO_Q_SECOND '2'
@@ -159,7 +160,7 @@ typedef enum {
159160

160161
#define DFLT_FO_VI "vt"
161162
#define DFLT_FO_VIM "tcq"
162-
#define FO_ALL "tcroq2vlb1mMBn,aw]jp" // for do_set()
163+
#define FO_ALL "tcro/q2vlb1mMBn,aw]jp" // for do_set()
163164

164165
// characters for the p_cpo option:
165166
#define CPO_ALTREAD 'a' // ":read" sets alternate file name
@@ -196,7 +197,7 @@ typedef enum {
196197
#define CPO_REMMARK 'R' // remove marks when filtering
197198
#define CPO_BUFOPT 's'
198199
#define CPO_BUFOPTGLOB 'S'
199-
#define CPO_TAGPAT 't'
200+
#define CPO_TAGPAT 't' // tag pattern is used for "n"
200201
#define CPO_UNDO 'u' // "u" undoes itself
201202
#define CPO_BACKSPACE 'v' // "v" keep deleted text
202203
#define CPO_CW 'w' // "cw" only changes one blank

src/testdir/test_textformat.vim

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,18 @@ func Test_format_c_comment()
278278
//
279279
END
280280
call assert_equal(expected, getline(1, '$'))
281+
3delete
282+
283+
" No comment repeated with a slash in 'formatoptions'
284+
set fo+=/
285+
normal 2Gox
286+
let expected =<< trim END
287+
nop;
288+
val = val; // This is a comment
289+
x
290+
END
291+
call assert_equal(expected, getline(1, '$'))
292+
set fo-=/
281293

282294
" using 'indentexpr' instead of 'cindent' does not repeat a comment
283295
setl nocindent indentexpr=2

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,8 @@ static char *(features[]) =
746746

747747
static int included_patches[] =
748748
{ /* Add new patch number below this line */
749+
/**/
750+
4907,
749751
/**/
750752
4906,
751753
/**/

0 commit comments

Comments
 (0)