Skip to content

Commit 67f8ab8

Browse files
committed
patch 8.1.0369: continuation lines cannot contain comments
Problem: Continuation lines cannot contain comments. Solution: Support using "\ .
1 parent 25328e3 commit 67f8ab8

File tree

5 files changed

+63
-23
lines changed

5 files changed

+63
-23
lines changed

runtime/doc/repeat.txt

Lines changed: 18 additions & 0 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -465,6 +465,16 @@ flag when defining the function, it is not relevant when executing it. >
465
.
465
.
466
:endfunction
466
:endfunction
467
:set cpo-=C
467
:set cpo-=C
468+
<
469+
*line-continuation-comment*
470+
To add a comment in between the lines start with '\" '. Notice the space
471+
after the double quote. Example: >
472+
let array = [
473+
"\ first entry comment
474+
\ 'first',
475+
"\ second entry comment
476+
\ 'second',
477+
\ ]
468
478
469
Rationale:
479
Rationale:
470
Most programs work with a trailing backslash to indicate line
480
Most programs work with a trailing backslash to indicate line
@@ -473,6 +483,14 @@ Rationale:
473
:map xx asdf\
483
:map xx asdf\
474
< Therefore the unusual leading backslash is used.
484
< Therefore the unusual leading backslash is used.
475

485

486+
Starting a comment in a continuation line results in all following
487+
continuation lines to be part of the comment. Since it was like this
488+
for a long time, when making it possible to add a comment halfway a
489+
sequence of continuation lines, it was not possible to use \", since
490+
that was a valid continuation line. Using '"\ ' comes closest, even
491+
though it may look a bit weird. Requiring the space after the
492+
backslash is to make it very unlikely this is a normal comment line.
493+
476
==============================================================================
494
==============================================================================
477
5. Using Vim packages *packages*
495
5. Using Vim packages *packages*
478

496

runtime/indent/vim.vim

Lines changed: 10 additions & 8 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -10,7 +10,7 @@ endif
10
let b:did_indent = 1
10
let b:did_indent = 1
11

11

12
setlocal indentexpr=GetVimIndent()
12
setlocal indentexpr=GetVimIndent()
13-
setlocal indentkeys+==end,=else,=cat,=fina,=END,0\\
13+
setlocal indentkeys+==end,=else,=cat,=fina,=END,0\\,0=\"\\\
14

14

15
let b:undo_indent = "setl indentkeys< indentexpr<"
15
let b:undo_indent = "setl indentkeys< indentexpr<"
16

16

@@ -31,15 +31,17 @@ function GetVimIndent()
31
endtry
31
endtry
32
endfunc
32
endfunc
33

33

34+
let s:lineContPat = '^\s*\(\\\|"\\ \)'
35+
34
function GetVimIndentIntern()
36
function GetVimIndentIntern()
35
" Find a non-blank line above the current line.
37
" Find a non-blank line above the current line.
36
let lnum = prevnonblank(v:lnum - 1)
38
let lnum = prevnonblank(v:lnum - 1)
37

39

38-
" If the current line doesn't start with '\' and below a line that starts
40+
" If the current line doesn't start with '\' or '"\ ' and below a line that
39-
" with '\', use the indent of the line above it.
41+
" starts with '\' or '"\ ', use the indent of the line above it.
40
let cur_text = getline(v:lnum)
42
let cur_text = getline(v:lnum)
41-
if cur_text !~ '^\s*\\'
43+
if cur_text !~ s:lineContPat
42-
while lnum > 0 && getline(lnum) =~ '^\s*\\'
44+
while lnum > 0 && getline(lnum) =~ s:lineContPat
43
let lnum = lnum - 1
45
let lnum = lnum - 1
44
endwhile
46
endwhile
45
endif
47
endif
@@ -51,10 +53,10 @@ function GetVimIndentIntern()
51
let prev_text = getline(lnum)
53
let prev_text = getline(lnum)
52

54

53
" Add a 'shiftwidth' after :if, :while, :try, :catch, :finally, :function
55
" Add a 'shiftwidth' after :if, :while, :try, :catch, :finally, :function
54-
" and :else. Add it three times for a line that starts with '\' after
56+
" and :else. Add it three times for a line that starts with '\' or '"\ '
55-
" a line that doesn't (or g:vim_indent_cont if it exists).
57+
" after a line that doesn't (or g:vim_indent_cont if it exists).
56
let ind = indent(lnum)
58
let ind = indent(lnum)
57-
if cur_text =~ '^\s*\\' && v:lnum > 1 && prev_text !~ '^\s*\\'
59+
if cur_text =~ s:lineContPat && v:lnum > 1 && prev_text !~ s:lineContPat
58
if exists("g:vim_indent_cont")
60
if exists("g:vim_indent_cont")
59
let ind = ind + g:vim_indent_cont
61
let ind = ind + g:vim_indent_cont
60
else
62
else

src/ex_cmds2.c

Lines changed: 22 additions & 15 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -4864,36 +4864,43 @@ getsourceline(int c UNUSED, void *cookie, int indent UNUSED)
4864
/* compensate for the one line read-ahead */
4864
/* compensate for the one line read-ahead */
4865
--sourcing_lnum;
4865
--sourcing_lnum;
4866

4866

4867-
/* Get the next line and concatenate it when it starts with a
4867+
// Get the next line and concatenate it when it starts with a
4868-
* backslash. We always need to read the next line, keep it in
4868+
// backslash. We always need to read the next line, keep it in
4869-
* sp->nextline. */
4869+
// sp->nextline.
4870+
/* Also check for a comment in between continuation lines: "\ */
4870
sp->nextline = get_one_sourceline(sp);
4871
sp->nextline = get_one_sourceline(sp);
4871-
if (sp->nextline != NULL && *(p = skipwhite(sp->nextline)) == '\\')
4872+
if (sp->nextline != NULL
4873+
&& (*(p = skipwhite(sp->nextline)) == '\\'
4874+
|| (p[0] == '"' && p[1] == '\\' && p[2] == ' ')))
4872
{
4875
{
4873
garray_T ga;
4876
garray_T ga;
4874

4877

4875
ga_init2(&ga, (int)sizeof(char_u), 400);
4878
ga_init2(&ga, (int)sizeof(char_u), 400);
4876
ga_concat(&ga, line);
4879
ga_concat(&ga, line);
4877-
ga_concat(&ga, p + 1);
4880+
if (*p == '\\')
4881+
ga_concat(&ga, p + 1);
4878
for (;;)
4882
for (;;)
4879
{
4883
{
4880
vim_free(sp->nextline);
4884
vim_free(sp->nextline);
4881
sp->nextline = get_one_sourceline(sp);
4885
sp->nextline = get_one_sourceline(sp);
4882
if (sp->nextline == NULL)
4886
if (sp->nextline == NULL)
4883
break;
4887
break;
4884
p = skipwhite(sp->nextline);
4888
p = skipwhite(sp->nextline);
4885-
if (*p != '\\')
4889+
if (*p == '\\')
4886-
break;
4887-
/* Adjust the growsize to the current length to speed up
4888-
* concatenating many lines. */
4889-
if (ga.ga_len > 400)
4890
{
4890
{
4891-
if (ga.ga_len > 8000)
4891+
// Adjust the growsize to the current length to speed up
4892-
ga.ga_growsize = 8000;
4892+
// concatenating many lines.
4893-
else
4893+
if (ga.ga_len > 400)
4894-
ga.ga_growsize = ga.ga_len;
4894+
{
4895+
if (ga.ga_len > 8000)
4896+
ga.ga_growsize = 8000;
4897+
else
4898+
ga.ga_growsize = ga.ga_len;
4899+
}
4900+
ga_concat(&ga, p + 1);
4895
}
4901
}
4896-
ga_concat(&ga, p + 1);
4902+
else if (p[0] != '"' || p[1] != '\\' || p[2] != ' ')
4903+
break;
4897
}
4904
}
4898
ga_append(&ga, NUL);
4905
ga_append(&ga, NUL);
4899
vim_free(line);
4906
vim_free(line);

src/testdir/test_eval_stuff.vim

Lines changed: 11 additions & 0 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -42,3 +42,14 @@ func Test_mkdir_p()
42
call delete('Xfile')
42
call delete('Xfile')
43
call delete('Xmkdir', 'rf')
43
call delete('Xmkdir', 'rf')
44
endfunc
44
endfunc
45+
46+
func Test_line_continuation()
47+
let array = [5,
48+
"\ ignore this
49+
\ 6,
50+
"\ more to ignore
51+
"\ more moreto ignore
52+
\ ]
53+
"\ and some more
54+
call assert_equal([5, 6], array)
55+
endfunc

src/version.c

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

794

795
static int included_patches[] =
795
static int included_patches[] =
796
{ /* Add new patch number below this line */
796
{ /* Add new patch number below this line */
797+
/**/
798+
369,
797
/**/
799
/**/
798
368,
800
368,
799
/**/
801
/**/

0 commit comments

Comments
 (0)