git@vger.kernel.org list mirror (unofficial, one of many)
 help / color / mirror / code / Atom feed
From: "René Scharfe" <l.s.r@web.de>
To: "Martin Ågren" <martin.agren@gmail.com>,
	"Grégoire PARIS" <postmaster@greg0ire.fr>
Cc: Git Mailing List <git@vger.kernel.org>
Subject: Re: phpdoc diff in git -L is not the correct one
Date: Sun, 15 Nov 2020 00:40:51 +0100	[thread overview]
Message-ID: <5a2d70eb-da75-ff59-8ac1-6ba81e1632da@web.de> (raw)
In-Reply-To: <CAN0heSr9=_So-sUhQN86vawBEkJhnrHHsd3OcSYZMZ-idZGFAQ@mail.gmail.com>

Am 14.11.20 um 18:35 schrieb Martin Ågren:
> Hi,
>
> On Sat, 14 Nov 2020 at 16:55, Grégoire PARIS <postmaster@greg0ire.fr> wrote:
>
>>> In the end I think your best option right now is to give explicit line
>>> numbers for <end> and <start>.
>> That is indeed what I currently do, I plugged that to vim's visual
>> selection with
>>
>>    vnoremap <leader>l :<c-u>exe '!git log -L'
>> line("'<").','.line("'>").':'.expand('%')<CR>
>>
>> and it works great!
>
> Great!
>
>> I also suppose the issue is the same for any other language that has
>> documentation above function declarations.
>
> Yeah, you'll see the same thing for C files, e.g., using this in the
> repo of Git itself:
>
>   git log -L :strbuf_swap:strbuf.h
>
> It will follow the lines from the function definition all the way down
> to the next function, but just as you saw in your example, it will not
> match the comment immediately *before* the function. That is, these
> lines will be followed:
>
>   https://github.com/git/git/blob/v2.29.2/strbuf.h#L125-L138

The --function-context options of git diff and git grep try to show
comments by including non-empty lines before function lines.  This
heuristic might work for -L :funcname:file as well (patch below), but
breaks seven tests in each of t8001-annotate.sh, t8002-blame.sh and
t8012-blame-colors.sh.

René

---
 line-range.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/line-range.c b/line-range.c
index 9b50583dc0..5d55fa255f 100644
--- a/line-range.c
+++ b/line-range.c
@@ -163,6 +163,13 @@ static const char *find_funcname_matching_regexp(xdemitconf_t *xecfg, const char
 	}
 }

+static int is_empty_line(const char *bol, const char *eol)
+{
+	while (bol < eol && isspace(*bol))
+		bol++;
+	return bol == eol;
+}
+
 static const char *parse_range_funcname(
 	const char *arg, nth_line_fn_t nth_line_cb,
 	void *cb_data, long lines, long anchor, long *begin, long *end,
@@ -233,6 +240,18 @@ static const char *parse_range_funcname(
 		(*end)++;
 	}

+	/*
+	 * Include non-empty, non-funcname lines before the found
+	 * funcname line, as they probably contain related comments.
+	 */
+	while (*begin > 0) {
+		const char *bol = nth_line_cb(cb_data, *begin - 1);
+		const char *eol = nth_line_cb(cb_data, *begin);
+		if (is_empty_line(bol, eol) || match_funcname(xecfg, bol, eol))
+			break;
+		(*begin)--;
+	}
+
 	regfree(&regexp);
 	free(xecfg);
 	free(pattern);
--
2.29.2

  reply	other threads:[~2020-11-14 23:42 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-14 13:19 Grégoire PARIS
2020-11-14 15:32 ` Martin Ågren
2020-11-14 15:55   ` Grégoire PARIS
2020-11-14 17:35     ` Martin Ågren
2020-11-14 23:40       ` René Scharfe [this message]
2020-11-18  8:17         ` Grégoire PARIS
2020-11-18 19:18           ` Martin Ågren

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5a2d70eb-da75-ff59-8ac1-6ba81e1632da@web.de \
    --to=l.s.r@web.de \
    --cc=git@vger.kernel.org \
    --cc=martin.agren@gmail.com \
    --cc=postmaster@greg0ire.fr \
    --subject='Re: phpdoc diff in git -L is not the correct one' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Code repositories for project(s) associated with this inbox:

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).