Skip to content

Commit

Permalink
patch 8.1.0369: continuation lines cannot contain comments
Browse files Browse the repository at this point in the history
Problem:    Continuation lines cannot contain comments.
Solution:   Support using "\ .
  • Loading branch information
brammool committed Sep 11, 2018
1 parent 25328e3 commit 67f8ab8
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 23 deletions.
18 changes: 18 additions & 0 deletions runtime/doc/repeat.txt
Expand Up @@ -465,6 +465,16 @@ flag when defining the function, it is not relevant when executing it. >
. .
:endfunction :endfunction
:set cpo-=C :set cpo-=C
<
*line-continuation-comment*
To add a comment in between the lines start with '\" '. Notice the space
after the double quote. Example: >
let array = [
"\ first entry comment
\ 'first',
"\ second entry comment
\ 'second',
\ ]

This comment has been minimized.

Copy link
@chdiza

chdiza Sep 11, 2018

The example doesn't match what the sentence says it should look like.

Rationale: Rationale:
Most programs work with a trailing backslash to indicate line Most programs work with a trailing backslash to indicate line
Expand All @@ -473,6 +483,14 @@ Rationale:
:map xx asdf\ :map xx asdf\
< Therefore the unusual leading backslash is used. < Therefore the unusual leading backslash is used.


Starting a comment in a continuation line results in all following
continuation lines to be part of the comment. Since it was like this
for a long time, when making it possible to add a comment halfway a
sequence of continuation lines, it was not possible to use \", since
that was a valid continuation line. Using '"\ ' comes closest, even
though it may look a bit weird. Requiring the space after the
backslash is to make it very unlikely this is a normal comment line.

============================================================================== ==============================================================================
5. Using Vim packages *packages* 5. Using Vim packages *packages*


Expand Down
18 changes: 10 additions & 8 deletions runtime/indent/vim.vim
Expand Up @@ -10,7 +10,7 @@ endif
let b:did_indent = 1 let b:did_indent = 1


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


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


Expand All @@ -31,15 +31,17 @@ function GetVimIndent()
endtry endtry
endfunc endfunc


let s:lineContPat = '^\s*\(\\\|"\\ \)'

function GetVimIndentIntern() function GetVimIndentIntern()
" Find a non-blank line above the current line. " Find a non-blank line above the current line.
let lnum = prevnonblank(v:lnum - 1) let lnum = prevnonblank(v:lnum - 1)


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


" Add a 'shiftwidth' after :if, :while, :try, :catch, :finally, :function " Add a 'shiftwidth' after :if, :while, :try, :catch, :finally, :function
" and :else. Add it three times for a line that starts with '\' after " and :else. Add it three times for a line that starts with '\' or '"\ '
" a line that doesn't (or g:vim_indent_cont if it exists). " after a line that doesn't (or g:vim_indent_cont if it exists).
let ind = indent(lnum) let ind = indent(lnum)
if cur_text =~ '^\s*\\' && v:lnum > 1 && prev_text !~ '^\s*\\' if cur_text =~ s:lineContPat && v:lnum > 1 && prev_text !~ s:lineContPat
if exists("g:vim_indent_cont") if exists("g:vim_indent_cont")
let ind = ind + g:vim_indent_cont let ind = ind + g:vim_indent_cont
else else
Expand Down
37 changes: 22 additions & 15 deletions src/ex_cmds2.c
Expand Up @@ -4864,36 +4864,43 @@ getsourceline(int c UNUSED, void *cookie, int indent UNUSED)
/* compensate for the one line read-ahead */ /* compensate for the one line read-ahead */
--sourcing_lnum; --sourcing_lnum;


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


ga_init2(&ga, (int)sizeof(char_u), 400); ga_init2(&ga, (int)sizeof(char_u), 400);
ga_concat(&ga, line); ga_concat(&ga, line);
ga_concat(&ga, p + 1); if (*p == '\\')
ga_concat(&ga, p + 1);
for (;;) for (;;)
{ {
vim_free(sp->nextline); vim_free(sp->nextline);
sp->nextline = get_one_sourceline(sp); sp->nextline = get_one_sourceline(sp);
if (sp->nextline == NULL) if (sp->nextline == NULL)
break; break;
p = skipwhite(sp->nextline); p = skipwhite(sp->nextline);
if (*p != '\\') if (*p == '\\')
break;
/* Adjust the growsize to the current length to speed up
* concatenating many lines. */
if (ga.ga_len > 400)
{ {
if (ga.ga_len > 8000) // Adjust the growsize to the current length to speed up
ga.ga_growsize = 8000; // concatenating many lines.
else if (ga.ga_len > 400)
ga.ga_growsize = ga.ga_len; {
if (ga.ga_len > 8000)
ga.ga_growsize = 8000;
else
ga.ga_growsize = ga.ga_len;
}
ga_concat(&ga, p + 1);
} }
ga_concat(&ga, p + 1); else if (p[0] != '"' || p[1] != '\\' || p[2] != ' ')
break;
} }
ga_append(&ga, NUL); ga_append(&ga, NUL);
vim_free(line); vim_free(line);
Expand Down
11 changes: 11 additions & 0 deletions src/testdir/test_eval_stuff.vim
Expand Up @@ -42,3 +42,14 @@ func Test_mkdir_p()
call delete('Xfile') call delete('Xfile')
call delete('Xmkdir', 'rf') call delete('Xmkdir', 'rf')
endfunc endfunc

func Test_line_continuation()
let array = [5,
"\ ignore this
\ 6,
"\ more to ignore
"\ more moreto ignore
\ ]
"\ and some more
call assert_equal([5, 6], array)
endfunc
2 changes: 2 additions & 0 deletions src/version.c
Expand Up @@ -794,6 +794,8 @@ static char *(features[]) =


static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
369,
/**/ /**/
368, 368,
/**/ /**/
Expand Down

1 comment on commit 67f8ab8

@tonymec
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also noticed this typo before I came across this thread, and sent a patch to vim_dev. FYI, I'm pasting it here too.

# HG changeset patch
# User Tony Mechelynck <antoine.mechelynck@gmail.com>
# Parent  8014307a918820085ed49a07995c3c4c10373ba1
Fix typo in comment continuation documentation

diff --git a/runtime/doc/repeat.txt b/runtime/doc/repeat.txt
--- a/runtime/doc/repeat.txt
+++ b/runtime/doc/repeat.txt
@@ -462,18 +462,18 @@ flag when defining the function, it is n
    :function Foo()
    :1append
    \asdf
    .
    :endfunction
    :set cpo-=C
 <
 					*line-continuation-comment*
-To add a comment in between the lines start with '\" '.  Notice the space
-after the double quote.  Example: >
+To add a comment in between the lines start with '"\ '.  Notice the space
+after the backslash.  Example: >
 	let array = [
 		"\ first entry comment
 		\ 'first',
 		"\ second entry comment
 		\ 'second',
 		\ ]
 
 Rationale:

Please sign in to comment.