git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [RFC 0/1] Leading whitespace as a function identification heuristic?

From
PWPhillip Wood <phillip.wood123@gmail.com>
Date
Sep 25, 2020, 19:01 UTC
Message-ID
<005de229-ad05-437c-7b83-8f6502b4e27d@gmail.com>
In-Reply-To
<20200925184319.GA660343@coredump.intra.peff.net>
Hi Peff
On 25/09/2020 19:43, Jeff King wrote:
Show 41 quoted lines
> On Fri, Sep 25, 2020 at 10:11:33AM +0100, Phillip Wood wrote:
> 
>> If I've understood correctly when ...code 2... contains changes that are
>> themselves indented then we'll pick the wrong function header for example
>>
>> 	void foo() {
>> 		void bar() {
>> 			...code 1...
>> 		}
>> 		for (...) {
>> 			// if this line is changed we pick bar rather
>> 			// than foo because it is the first function
>> 			// header with a smaller indentation than the
>> 			// first changed line.
>> 		}
>> 	}
>>
>> Unfortunately I suspect code like that is not uncommon and the diff would
>> regress with this simple heuristic. It might be possible to recalculate the
>> required indentation as we walk backwards up the file though, so when we hit
>> the "for" line we reduce the maximum indentation allowed for a match and so
>> skip "bar" as a function header.
> 
> Thanks, that's a great counter-example I hadn't considered.
> 
> Yes, I agree that adjusting the desired indentation as we walk backwards
> would work. That's assuming indentation is hierarchical, but I think
> that's implied by the existence of this feature at all.
> 
> Another possible corner case: tabs vs spaces. If I have:
> 
>    <space><space><space><space><space><space><space><space>foo
>    <tab><tab>bar
> 
> which is more indented? Counting isspace(), it is the first one. But
> visually, it would _usually_ be the second one. But of course it would
> depend on your tabstops.
> 
> The above example is obviously stupid and contrived, but I wonder if
> there are legitimate confusing cases where people mix tabs and spaces
> (e.g., mixed tabs and spaces to align function parameters, etc).

To calculate the indentation for diff --color-moved-ws=allow-indentation-change in fill_es_indent_data() we use the tabwidth whitespace attribute to calculate the width of a tab in spaces

	int tab_width = es->flags & WS_TAB_WIDTH_MASK;
	/* calculate the visual width of indentation */
	while(1) {
		if (s[off] == ' ') {
			width++;
			off++;
		} else if (s[off] == '\t') {
			width += tab_width - (width % tab_width);
			while (s[++off] == '\t')
				width += tab_width;
		} else {
			break;
		}
	}

I think we could probably do something similar here assuming it is the visual width of the indentation that we care about.

Best Wishes
Phillip
> -Peff
> 
Previous: Jeff KingNext: Jeff King
Message 8 of 10 in “[RFC 0/1] Leading whitespace as a function identification heuristic?”
  1. Ryan ZoellerSep 23, 2020
  2. 1/1 xdiff: use leading whitespace in function heuristicRyan Zoeller, Sep 23, 2020
  3. Junio C HamanoSep 24, 2020
  4. Jeff KingSep 24, 2020
  5. Ryan ZoellerSep 24, 2020
  6. Phillip WoodSep 25, 2020
  7. Jeff KingSep 25, 2020
  8. Phillip WoodSep 25, 2020
  9. Jeff KingSep 25, 2020
  10. Johannes SixtSep 25, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.