@@ -4244,6 +4244,52 @@ nv_gd(
42444244#endif
42454245}
42464246
4247+ /*
4248+ * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
4249+ * otherwise.
4250+ */
4251+ static int
4252+ is_ident (char_u * line , int offset )
4253+ {
4254+ int i ;
4255+ int incomment = FALSE;
4256+ int instring = 0 ;
4257+ int prev = 0 ;
4258+
4259+ for (i = 0 ; i < offset && line [i ] != NUL ; i ++ )
4260+ {
4261+ if (instring != 0 )
4262+ {
4263+ if (prev != '\\' && line [i ] == instring )
4264+ instring = 0 ;
4265+ }
4266+ else if ((line [i ] == '"' || line [i ] == '\'' ) && !incomment )
4267+ {
4268+ instring = line [i ];
4269+ }
4270+ else
4271+ {
4272+ if (incomment )
4273+ {
4274+ if (prev == '*' && line [i ] == '/' )
4275+ incomment = FALSE;
4276+ }
4277+ else if (prev == '/' && line [i ] == '*' )
4278+ {
4279+ incomment = TRUE;
4280+ }
4281+ else if (prev == '/' && line [i ] == '/' )
4282+ {
4283+ return FALSE;
4284+ }
4285+ }
4286+
4287+ prev = line [i ];
4288+ }
4289+
4290+ return incomment == FALSE && instring == 0 ;
4291+ }
4292+
42474293/*
42484294 * Search for variable declaration of "ptr[len]".
42494295 * When "locally" is TRUE in the current function ("gd"), otherwise in the
@@ -4269,6 +4315,7 @@ find_decl(
42694315 int retval = OK ;
42704316 int incll ;
42714317 int searchflags = flags_arg ;
4318+ int valid ;
42724319
42734320 if ((pat = alloc (len + 7 )) == NULL )
42744321 return FAIL ;
@@ -4306,6 +4353,7 @@ find_decl(
43064353 clearpos (& found_pos );
43074354 for (;;)
43084355 {
4356+ valid = FALSE;
43094357 t = searchit (curwin , curbuf , & curwin -> w_cursor , FORWARD ,
43104358 pat , 1L , searchflags , RE_LAST , (linenr_T )0 , NULL );
43114359 if (curwin -> w_cursor .lnum >= old_pos .lnum )
@@ -4342,21 +4390,41 @@ find_decl(
43424390 continue ;
43434391 }
43444392#endif
4345- if (!locally ) /* global search: use first match found */
4393+ valid = is_ident (ml_get_curline (), curwin -> w_cursor .col );
4394+
4395+ /* If the current position is not a valid identifier and a previous
4396+ * match is present, favor that one instead. */
4397+ if (!valid && found_pos .lnum != 0 )
4398+ {
4399+ curwin -> w_cursor = found_pos ;
43464400 break ;
4347- if (curwin -> w_cursor .lnum >= par_pos .lnum )
4401+ }
4402+
4403+ /* Global search: use first valid match found */
4404+ if (valid && !locally )
4405+ break ;
4406+ if (valid && curwin -> w_cursor .lnum >= par_pos .lnum )
43484407 {
43494408 /* If we previously found a valid position, use it. */
43504409 if (found_pos .lnum != 0 )
43514410 curwin -> w_cursor = found_pos ;
43524411 break ;
43534412 }
43544413
4355- /* For finding a local variable and the match is before the "{" search
4356- * to find a later match. For K&R style function declarations this
4357- * skips the function header without types. Remove SEARCH_START from
4358- * flags to avoid getting stuck at one position. */
4359- found_pos = curwin -> w_cursor ;
4414+ /* For finding a local variable and the match is before the "{" or
4415+ * inside a comment, continue searching. For K&R style function
4416+ * declarations this skips the function header without types. */
4417+ if (!valid )
4418+ {
4419+ /* Braces needed due to macro expansion of clearpos. */
4420+ clearpos (& found_pos );
4421+ }
4422+ else
4423+ {
4424+ found_pos = curwin -> w_cursor ;
4425+ }
4426+ /* Remove SEARCH_START from flags to avoid getting stuck at one
4427+ * position. */
43604428 searchflags &= ~SEARCH_START ;
43614429 }
43624430
0 commit comments