vim-patch:8.0.0148 #7344

Problem:    When a C preprocessor statement has two line continuations the
            following line does not have the right indent. (Ken Takata)
Solution:   Add the indent of the previous continuation line. (Hirohito
            Higashi)

c6aa475a27
This commit is contained in:
ckelsel 2017-09-30 21:43:03 +08:00 committed by Justin M. Keyes
parent 9ad7529f70
commit c0e45d97b0
3 changed files with 83 additions and 35 deletions

View File

@ -734,16 +734,20 @@ static int cin_ispreproc(char_u *s)
return FALSE; return FALSE;
} }
/* /// Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
* Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a /// continuation line of a preprocessor statement. Decrease "*lnump" to the
* continuation line of a preprocessor statement. Decrease "*lnump" to the /// start and return the line in "*pp".
* start and return the line in "*pp". /// Put the amount of indent in "*amount".
*/ static int cin_ispreproc_cont(char_u **pp, linenr_T *lnump, int *amount)
static int cin_ispreproc_cont(char_u **pp, linenr_T *lnump)
{ {
char_u *line = *pp; char_u *line = *pp;
linenr_T lnum = *lnump; linenr_T lnum = *lnump;
int retval = FALSE; int retval = false;
int candidate_amount = *amount;
if (*line != NUL && line[STRLEN(line) - 1] == '\\') {
candidate_amount = get_indent_lnum(lnum);
}
for (;; ) { for (;; ) {
if (cin_ispreproc(line)) { if (cin_ispreproc(line)) {
@ -758,8 +762,12 @@ static int cin_ispreproc_cont(char_u **pp, linenr_T *lnump)
break; break;
} }
if (lnum != *lnump) if (lnum != *lnump) {
*pp = ml_get(*lnump); *pp = ml_get(*lnump);
}
if (retval) {
*amount = candidate_amount;
}
return retval; return retval;
} }
@ -1994,10 +2002,12 @@ int get_c_indent(void)
amount = -1; amount = -1;
for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum) { for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum) {
l = skipwhite(ml_get(lnum)); l = skipwhite(ml_get(lnum));
if (cin_nocode(l)) /* skip comment lines */ if (cin_nocode(l)) { // skip comment lines
continue; continue;
if (cin_ispreproc_cont(&l, &lnum)) }
continue; /* ignore #define, #if, etc. */ if (cin_ispreproc_cont(&l, &lnum, &amount)) {
continue; // ignore #define, #if, etc.
}
curwin->w_cursor.lnum = lnum; curwin->w_cursor.lnum = lnum;
/* Skip a comment or raw string. XXX */ /* Skip a comment or raw string. XXX */
@ -2353,15 +2363,14 @@ int get_c_indent(void)
* up with it. * up with it.
*/ */
if (curwin->w_cursor.lnum <= ourscope) { if (curwin->w_cursor.lnum <= ourscope) {
/* we reached end of scope: // We reached end of scope:
* if looking for an enum or structure initialization // If looking for a enum or structure initialization
* go further back: // go further back:
* if it is an initializer (enum xxx or xxx =), then // If it is an initializer (enum xxx or xxx =), then
* don't add ind_continuation, otherwise it is a variable // don't add ind_continuation, otherwise it is a variable
* declaration: // declaration:
* int x, // int x,
* here; <-- add ind_continuation // here; <-- add ind_continuation
*/
if (lookfor == LOOKFOR_ENUM_OR_INIT) { if (lookfor == LOOKFOR_ENUM_OR_INIT) {
if (curwin->w_cursor.lnum == 0 if (curwin->w_cursor.lnum == 0
|| curwin->w_cursor.lnum || curwin->w_cursor.lnum
@ -2389,11 +2398,12 @@ int get_c_indent(void)
continue; continue;
} }
/* //
* Skip preprocessor directives and blank lines. // Skip preprocessor directives and blank lines.
*/ //
if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum)) if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount)) {
continue; continue;
}
if (cin_nocode(l)) if (cin_nocode(l))
continue; continue;
@ -2497,9 +2507,10 @@ int get_c_indent(void)
continue; continue;
} }
/* Skip preprocessor directives and blank lines. */ // Skip preprocessor directives and blank lines.
if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum)) if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount)) {
continue; continue;
}
/* Finally the actual check for "namespace". */ /* Finally the actual check for "namespace". */
if (cin_is_cpp_namespace(l)) { if (cin_is_cpp_namespace(l)) {
@ -2662,9 +2673,10 @@ int get_c_indent(void)
* unlocked it) * unlocked it)
*/ */
l = get_cursor_line_ptr(); l = get_cursor_line_ptr();
if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum) if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount)
|| cin_nocode(l)) || cin_nocode(l)) {
continue; continue;
}
/* /*
* Are we at the start of a cpp base class declaration or * Are we at the start of a cpp base class declaration or
@ -3309,11 +3321,12 @@ term_again:
break; break;
} }
/* //
* Skip preprocessor directives and blank lines. // Skip preprocessor directives and blank lines.
*/ //
if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum)) if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount)) {
continue; continue;
}
if (cin_nocode(l)) if (cin_nocode(l))
continue; continue;
@ -3405,9 +3418,10 @@ term_again:
while (curwin->w_cursor.lnum > 1) { while (curwin->w_cursor.lnum > 1) {
look = ml_get(--curwin->w_cursor.lnum); look = ml_get(--curwin->w_cursor.lnum);
if (!(cin_nocode(look) || cin_ispreproc_cont( if (!(cin_nocode(look)
&look, &curwin->w_cursor.lnum))) || cin_ispreproc_cont(&look, &curwin->w_cursor.lnum, &amount))) {
break; break;
}
} }
if (curwin->w_cursor.lnum > 0 if (curwin->w_cursor.lnum > 0
&& cin_ends_in(look, (char_u *)"}", NULL)) && cin_ends_in(look, (char_u *)"}", NULL))

View File

@ -955,7 +955,7 @@ static const int included_patches[] = {
// 151, // 151,
150, 150,
149, 149,
// 148, 148,
147, 147,
146, 146,
// 145 NA // 145 NA

View File

@ -4718,4 +4718,38 @@ describe('cindent', function()
JSEND JSEND
]=]) ]=])
end) end)
it('line continuations in macros / vim-patch 8.0.0148', function()
insert_([=[
/* start of define */
{
}
#define AAA \
BBB\
CCC
#define CNT \
1 + \
2 + \
4
/* end of define */]=])
feed_command('set cino&')
feed_command('/start of define')
feed('=/end of define<cr>')
expect([=[
/* start of define */
{
}
#define AAA \
BBB\
CCC
#define CNT \
1 + \
2 + \
4
/* end of define */]=])
end)
end) end)