Skip to content

Commit a5dc20b

Browse files
rscharfegitster
authored andcommitted
grep: show non-empty lines before functions with -W
Non-empty lines before a function definition are most likely comments for that function and thus relevant. Include them in function context. Such a non-empty line might also belong to the preceding function if there is no separating blank line. Stop extending the context upwards also at the next function line to make sure only one extra function body is shown at most. Signed-off-by: Rene Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6653a01 commit a5dc20b

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

grep.c

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,33 +1476,52 @@ static void show_funcname_line(struct grep_opt *opt, struct grep_source *gs,
14761476
}
14771477
}
14781478

1479+
static int is_empty_line(const char *bol, const char *eol);
1480+
14791481
static void show_pre_context(struct grep_opt *opt, struct grep_source *gs,
14801482
char *bol, char *end, unsigned lno)
14811483
{
14821484
unsigned cur = lno, from = 1, funcname_lno = 0, orig_from;
1483-
int funcname_needed = !!opt->funcname;
1485+
int funcname_needed = !!opt->funcname, comment_needed = 0;
14841486

14851487
if (opt->pre_context < lno)
14861488
from = lno - opt->pre_context;
14871489
if (from <= opt->last_shown)
14881490
from = opt->last_shown + 1;
14891491
orig_from = from;
1490-
if (opt->funcbody && !match_funcname(opt, gs, bol, end)) {
1491-
funcname_needed = 1;
1492+
if (opt->funcbody) {
1493+
if (match_funcname(opt, gs, bol, end))
1494+
comment_needed = 1;
1495+
else
1496+
funcname_needed = 1;
14921497
from = opt->last_shown + 1;
14931498
}
14941499

14951500
/* Rewind. */
14961501
while (bol > gs->buf && cur > from) {
1502+
char *next_bol = bol;
14971503
char *eol = --bol;
14981504

14991505
while (bol > gs->buf && bol[-1] != '\n')
15001506
bol--;
15011507
cur--;
1508+
if (comment_needed && (is_empty_line(bol, eol) ||
1509+
match_funcname(opt, gs, bol, eol))) {
1510+
comment_needed = 0;
1511+
from = orig_from;
1512+
if (cur < from) {
1513+
cur++;
1514+
bol = next_bol;
1515+
break;
1516+
}
1517+
}
15021518
if (funcname_needed && match_funcname(opt, gs, bol, eol)) {
15031519
funcname_lno = cur;
15041520
funcname_needed = 0;
1505-
from = orig_from;
1521+
if (opt->funcbody)
1522+
comment_needed = 1;
1523+
else
1524+
from = orig_from;
15061525
}
15071526
}
15081527

t/t7810-grep.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,7 @@ test_expect_success 'grep -W with userdiff' '
785785
git grep -W echo >function-context-userdiff-actual
786786
'
787787

788-
test_expect_failure ' includes preceding comment' '
788+
test_expect_success ' includes preceding comment' '
789789
grep "# Say hello" function-context-userdiff-actual
790790
'
791791

0 commit comments

Comments
 (0)