Skip to content

Commit 226630a

Browse files
committed
patch 8.0.0023
Problem: "gd" and "gD" may find a match in a comment or string. Solution: Ignore matches in comments and strings. (Anton Lindqvist)
1 parent ec68a99 commit 226630a

File tree

3 files changed

+345
-20
lines changed

3 files changed

+345
-20
lines changed

src/normal.c

Lines changed: 75 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4239,6 +4239,52 @@ nv_gd(
42394239
#endif
42404240
}
42414241

4242+
/*
4243+
* Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
4244+
* otherwise.
4245+
*/
4246+
static int
4247+
is_ident(char_u *line, int offset)
4248+
{
4249+
int i;
4250+
int incomment = FALSE;
4251+
int instring = 0;
4252+
int prev = 0;
4253+
4254+
for (i = 0; i < offset && line[i] != NUL; i++)
4255+
{
4256+
if (instring != 0)
4257+
{
4258+
if (prev != '\\' && line[i] == instring)
4259+
instring = 0;
4260+
}
4261+
else if ((line[i] == '"' || line[i] == '\'') && !incomment)
4262+
{
4263+
instring = line[i];
4264+
}
4265+
else
4266+
{
4267+
if (incomment)
4268+
{
4269+
if (prev == '*' && line[i] == '/')
4270+
incomment = FALSE;
4271+
}
4272+
else if (prev == '/' && line[i] == '*')
4273+
{
4274+
incomment = TRUE;
4275+
}
4276+
else if (prev == '/' && line[i] == '/')
4277+
{
4278+
return FALSE;
4279+
}
4280+
}
4281+
4282+
prev = line[i];
4283+
}
4284+
4285+
return incomment == FALSE && instring == 0;
4286+
}
4287+
42424288
/*
42434289
* Search for variable declaration of "ptr[len]".
42444290
* When "locally" is TRUE in the current function ("gd"), otherwise in the
@@ -4264,6 +4310,7 @@ find_decl(
42644310
int retval = OK;
42654311
int incll;
42664312
int searchflags = flags_arg;
4313+
int valid;
42674314

42684315
if ((pat = alloc(len + 7)) == NULL)
42694316
return FAIL;
@@ -4301,6 +4348,7 @@ find_decl(
43014348
clearpos(&found_pos);
43024349
for (;;)
43034350
{
4351+
valid = FALSE;
43044352
t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD,
43054353
pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL);
43064354
if (curwin->w_cursor.lnum >= old_pos.lnum)
@@ -4337,21 +4385,41 @@ find_decl(
43374385
continue;
43384386
}
43394387
#endif
4340-
if (!locally) /* global search: use first match found */
4388+
valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
4389+
4390+
/* If the current position is not a valid identifier and a previous
4391+
* match is present, favor that one instead. */
4392+
if (!valid && found_pos.lnum != 0)
4393+
{
4394+
curwin->w_cursor = found_pos;
43414395
break;
4342-
if (curwin->w_cursor.lnum >= par_pos.lnum)
4396+
}
4397+
4398+
/* Global search: use first valid match found */
4399+
if (valid && !locally)
4400+
break;
4401+
if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
43434402
{
43444403
/* If we previously found a valid position, use it. */
43454404
if (found_pos.lnum != 0)
43464405
curwin->w_cursor = found_pos;
43474406
break;
43484407
}
43494408

4350-
/* For finding a local variable and the match is before the "{" search
4351-
* to find a later match. For K&R style function declarations this
4352-
* skips the function header without types. Remove SEARCH_START from
4353-
* flags to avoid getting stuck at one position. */
4354-
found_pos = curwin->w_cursor;
4409+
/* For finding a local variable and the match is before the "{" or
4410+
* inside a comment, continue searching. For K&R style function
4411+
* declarations this skips the function header without types. */
4412+
if (!valid)
4413+
{
4414+
/* Braces needed due to macro expansion of clearpos. */
4415+
clearpos(&found_pos);
4416+
}
4417+
else
4418+
{
4419+
found_pos = curwin->w_cursor;
4420+
}
4421+
/* Remove SEARCH_START from flags to avoid getting stuck at one
4422+
* position. */
43554423
searchflags &= ~SEARCH_START;
43564424
}
43574425

0 commit comments

Comments
 (0)