Skip to content

Commit c6aa475

Browse files
committed
patch 8.0.0148: wrong indent in C preprocessor with line continuation
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)
1 parent 6e450a5 commit c6aa475

4 files changed

Lines changed: 54 additions & 11 deletions

File tree

src/misc1.c

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5422,7 +5422,6 @@ static int skip_label(linenr_T, char_u **pp);
54225422
static int cin_first_id_amount(void);
54235423
static int cin_get_equal_amount(linenr_T lnum);
54245424
static int cin_ispreproc(char_u *);
5425-
static int cin_ispreproc_cont(char_u **pp, linenr_T *lnump);
54265425
static int cin_iscomment(char_u *);
54275426
static int cin_islinecomment(char_u *);
54285427
static int cin_isterminated(char_u *, int, int);
@@ -6002,13 +6001,18 @@ cin_ispreproc(char_u *s)
60026001
* Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
60036002
* continuation line of a preprocessor statement. Decrease "*lnump" to the
60046003
* start and return the line in "*pp".
6004+
* Put the amount of indent in "*amount".
60056005
*/
60066006
static int
6007-
cin_ispreproc_cont(char_u **pp, linenr_T *lnump)
6007+
cin_ispreproc_cont(char_u **pp, linenr_T *lnump, int *amount)
60086008
{
60096009
char_u *line = *pp;
60106010
linenr_T lnum = *lnump;
60116011
int retval = FALSE;
6012+
int candidate_amount = *amount;
6013+
6014+
if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6015+
candidate_amount = get_indent_lnum(lnum);
60126016

60136017
for (;;)
60146018
{
@@ -6027,6 +6031,8 @@ cin_ispreproc_cont(char_u **pp, linenr_T *lnump)
60276031

60286032
if (lnum != *lnump)
60296033
*pp = ml_get(*lnump);
6034+
if (retval)
6035+
*amount = candidate_amount;
60306036
return retval;
60316037
}
60326038

@@ -7390,7 +7396,7 @@ get_c_indent(void)
73907396
l = skipwhite(ml_get(lnum));
73917397
if (cin_nocode(l)) /* skip comment lines */
73927398
continue;
7393-
if (cin_ispreproc_cont(&l, &lnum))
7399+
if (cin_ispreproc_cont(&l, &lnum, &amount))
73947400
continue; /* ignore #define, #if, etc. */
73957401
curwin->w_cursor.lnum = lnum;
73967402

@@ -7803,10 +7809,10 @@ get_c_indent(void)
78037809
*/
78047810
if (curwin->w_cursor.lnum <= ourscope)
78057811
{
7806-
/* we reached end of scope:
7807-
* if looking for a enum or structure initialization
7812+
/* We reached end of scope:
7813+
* If looking for a enum or structure initialization
78087814
* go further back:
7809-
* if it is an initializer (enum xxx or xxx =), then
7815+
* If it is an initializer (enum xxx or xxx =), then
78107816
* don't add ind_continuation, otherwise it is a variable
78117817
* declaration:
78127818
* int x,
@@ -7845,7 +7851,8 @@ get_c_indent(void)
78457851
/*
78467852
* Skip preprocessor directives and blank lines.
78477853
*/
7848-
if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
7854+
if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
7855+
&amount))
78497856
continue;
78507857

78517858
if (cin_nocode(l))
@@ -7962,7 +7969,8 @@ get_c_indent(void)
79627969
}
79637970

79647971
/* Skip preprocessor directives and blank lines. */
7965-
if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
7972+
if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
7973+
&amount))
79667974
continue;
79677975

79687976
/* Finally the actual check for "namespace". */
@@ -8138,7 +8146,7 @@ get_c_indent(void)
81388146
* unlocked it)
81398147
*/
81408148
l = ml_get_curline();
8141-
if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum)
8149+
if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount)
81428150
|| cin_nocode(l))
81438151
continue;
81448152

@@ -8859,7 +8867,7 @@ get_c_indent(void)
88598867
/*
88608868
* Skip preprocessor directives and blank lines.
88618869
*/
8862-
if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
8870+
if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount))
88638871
continue;
88648872

88658873
if (cin_nocode(l))
@@ -8960,7 +8968,7 @@ get_c_indent(void)
89608968
{
89618969
look = ml_get(--curwin->w_cursor.lnum);
89628970
if (!(cin_nocode(look) || cin_ispreproc_cont(
8963-
&look, &curwin->w_cursor.lnum)))
8971+
&look, &curwin->w_cursor.lnum, &amount)))
89648972
break;
89658973
}
89668974
if (curwin->w_cursor.lnum > 0

src/testdir/test3.in

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2317,6 +2317,25 @@ h,
23172317
i;
23182318
JSEND
23192319

2320+
STARTTEST
2321+
:set cin cino&
2322+
/start of define
2323+
=/end of define
2324+
ENDTEST
2325+
2326+
/* start of define */
2327+
{
2328+
}
2329+
#define AAA \
2330+
BBB\
2331+
CCC
2332+
2333+
#define CNT \
2334+
1 + \
2335+
2 + \
2336+
4
2337+
/* end of define */
2338+
23202339
STARTTEST
23212340
:g/^STARTTEST/.,/^ENDTEST/d
23222341
:1;/start of AUTO/,$wq! test.out

src/testdir/test3.ok

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2080,3 +2080,17 @@ var a,
20802080
i;
20812081
JSEND
20822082

2083+
2084+
/* start of define */
2085+
{
2086+
}
2087+
#define AAA \
2088+
BBB\
2089+
CCC
2090+
2091+
#define CNT \
2092+
1 + \
2093+
2 + \
2094+
4
2095+
/* end of define */
2096+

src/version.c

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

765765
static int included_patches[] =
766766
{ /* Add new patch number below this line */
767+
/**/
768+
148,
767769
/**/
768770
147,
769771
/**/

0 commit comments

Comments
 (0)