summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRené Scharfe <l.s.r@web.de>2017-11-18 18:08:08 (GMT)
committerJunio C Hamano <gitster@pobox.com>2017-11-21 00:36:06 (GMT)
commita5dc20b0701cee53b2c37a4aa3a339b48d5bb298 (patch)
treea77c5e843d10486bb70eea127cceadad419f159a
parent6653a01bf2dd1e2aabbcf1f83269bd75737acc94 (diff)
downloadgit-a5dc20b0701cee53b2c37a4aa3a339b48d5bb298.zip
git-a5dc20b0701cee53b2c37a4aa3a339b48d5bb298.tar.gz
git-a5dc20b0701cee53b2c37a4aa3a339b48d5bb298.tar.bz2
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 <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
-rw-r--r--grep.c27
-rwxr-xr-xt/t7810-grep.sh2
2 files changed, 24 insertions, 5 deletions
diff --git a/grep.c b/grep.c
index 2c55d10..a69c05e 100644
--- a/grep.c
+++ b/grep.c
@@ -1476,33 +1476,52 @@ static void show_funcname_line(struct grep_opt *opt, struct grep_source *gs,
}
}
+static int is_empty_line(const char *bol, const char *eol);
+
static void show_pre_context(struct grep_opt *opt, struct grep_source *gs,
char *bol, char *end, unsigned lno)
{
unsigned cur = lno, from = 1, funcname_lno = 0, orig_from;
- int funcname_needed = !!opt->funcname;
+ int funcname_needed = !!opt->funcname, comment_needed = 0;
if (opt->pre_context < lno)
from = lno - opt->pre_context;
if (from <= opt->last_shown)
from = opt->last_shown + 1;
orig_from = from;
- if (opt->funcbody && !match_funcname(opt, gs, bol, end)) {
- funcname_needed = 1;
+ if (opt->funcbody) {
+ if (match_funcname(opt, gs, bol, end))
+ comment_needed = 1;
+ else
+ funcname_needed = 1;
from = opt->last_shown + 1;
}
/* Rewind. */
while (bol > gs->buf && cur > from) {
+ char *next_bol = bol;
char *eol = --bol;
while (bol > gs->buf && bol[-1] != '\n')
bol--;
cur--;
+ if (comment_needed && (is_empty_line(bol, eol) ||
+ match_funcname(opt, gs, bol, eol))) {
+ comment_needed = 0;
+ from = orig_from;
+ if (cur < from) {
+ cur++;
+ bol = next_bol;
+ break;
+ }
+ }
if (funcname_needed && match_funcname(opt, gs, bol, eol)) {
funcname_lno = cur;
funcname_needed = 0;
- from = orig_from;
+ if (opt->funcbody)
+ comment_needed = 1;
+ else
+ from = orig_from;
}
}
diff --git a/t/t7810-grep.sh b/t/t7810-grep.sh
index 632b905..c02ca73 100755
--- a/t/t7810-grep.sh
+++ b/t/t7810-grep.sh
@@ -785,7 +785,7 @@ test_expect_success 'grep -W with userdiff' '
git grep -W echo >function-context-userdiff-actual
'
-test_expect_failure ' includes preceding comment' '
+test_expect_success ' includes preceding comment' '
grep "# Say hello" function-context-userdiff-actual
'