vim-patch:8.1.1585: :let-heredoc does not trim enough

Problem:    :let-heredoc does not trim enough.
Solution:   Trim indent from the contents based on the indent of the first
            line.  Use let-heredoc in more tests.
e7eb92708e
This commit is contained in:
Jurica Bradaric
2019-10-12 09:48:48 +02:00
parent 9af0fe529d
commit 6c012b0624
11 changed files with 461 additions and 413 deletions

View File

@@ -1521,7 +1521,9 @@ heredoc_get(exarg_T *eap, char_u *cmd)
{
char_u *marker;
char_u *p;
int indent_len = 0;
int marker_indent_len = 0;
int text_indent_len = 0;
char_u *text_indent = NULL;
if (eap->getline == NULL) {
EMSG(_("E991: cannot use =<< here"));
@@ -1534,14 +1536,16 @@ heredoc_get(exarg_T *eap, char_u *cmd)
&& (cmd[4] == NUL || ascii_iswhite(cmd[4]))) {
cmd = skipwhite(cmd + 4);
// Trim the indentation from all the lines in the here document
// Trim the indentation from all the lines in the here document.
// The amount of indentation trimmed is the same as the indentation of
// the :let command line.
// the first line after the :let command line. To find the end marker
// the indent of the :let command line is trimmed.
p = *eap->cmdlinep;
while (ascii_iswhite(*p)) {
p++;
indent_len++;
marker_indent_len++;
}
text_indent_len = -1;
}
// The marker is the next word. Default marker is "."
@@ -1559,28 +1563,48 @@ heredoc_get(exarg_T *eap, char_u *cmd)
list_T *l = tv_list_alloc(0);
for (;;) {
int i = 0;
int mi = 0;
int ti = 0;
char_u *theline = eap->getline(NUL, eap->cookie, 0, false);
if (theline != NULL && indent_len > 0) {
// trim the indent matching the first line
if (STRNCMP(theline, *eap->cmdlinep, indent_len) == 0) {
i = indent_len;
}
}
if (theline == NULL) {
EMSG2(_("E990: Missing end marker '%s'"), marker);
break;
}
if (STRCMP(marker, theline + i) == 0) {
// with "trim": skip the indent matching the :let line to find the
// marker
if (marker_indent_len > 0
&& STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0) {
mi = marker_indent_len;
}
if (STRCMP(marker, theline + mi) == 0) {
xfree(theline);
break;
}
if (text_indent_len == -1 && *theline != NUL) {
// set the text indent from the first line.
p = theline;
text_indent_len = 0;
while (ascii_iswhite(*p)) {
p++;
text_indent_len++;
}
text_indent = vim_strnsave(theline, text_indent_len);
}
// with "trim": skip the indent matching the first line
if (text_indent != NULL) {
for (ti = 0; ti < text_indent_len; ti++) {
if (theline[ti] != text_indent[ti]) {
break;
}
}
}
tv_list_append_string(l, (char *)(theline + i), -1);
tv_list_append_string(l, (char *)(theline + ti), -1);
xfree(theline);
}
xfree(text_indent);
return l;
}