Skip to content

🚀 Support for "git grep" colorization, specifically with the -W / --function-context flag #769

@zachriggle

Description

@zachriggle

Hey @dandavison! Thanks for making such an absolutely indispensable tool like delta!

Feature Request

delta is great for diffs (and soon blame?), but I have a suggestion* for an additional mode that might be useful to others -- the ability to syntax-highlight git grep output!

Why git grep?

I mention git grep specifically as opposed to grep / ack / ag/ pt / rg because of two extremely powerful features of git grep that the competition lack.

Boolean Expressions

First, let's look at its ability to have complex nested boolean expressions like --and --not and grouping which make git grep very powerful. For example, let's say we wanted to search for certain kmalloc invocations that also DO NOT have GFP_KERNEL or GFP_ATOMIC. Yes, you could do e.g. `grep kmalloc | grep -vE 'GFP_(KERNEL|ATOMIC)', but for the sake of demonstration...

Screen Shot 2021-11-08 at 12 06 29 PM

Full Function Context

More importantly, it also has the ability to show the entire function in which a match resides with --function-context (-W).

Note the =, :, and - markers after the line numbers, indicating lines-with-match, the function name, and context lines.

(Large screenshot warning)

Screen Shot 2021-11-08 at 12 07 34 PM

Example Output

I've jury rigged a (very ugly zsh-based) solution** that gives me effectively what I'm asking for in this feature request, but it would be better if it were directly integrated into delta (and others would also be able to benefit from this). Effectively, I parse the markers and shells out to bat to show and highlight the correct lines.

Screen Shot 2021-11-08 at 12 13 07 PM

Added Bonus Functionality!

While git grep is very powerful, if you look at the second screenshot above, you'll note that there is only ONE line matching the query -- line 1482. However, every instance of sizeof and ; etc that are in the query are also highlighted.

I'm not sure why Git does this, but by parsing out the symbols at the beginning of the line, delta could be even BETTER by not highlighting these partial-matches-in-the-same-function. Note that my example output (third screenshot) does not replicate this undesirable behavior.

Additional Niceties

For many of the default themes, the "highlight" color for bat is not ideal. I work around this by using a custom-compiled theme based off solarized-dark, to effectively achieve the same highlight color as delta uses for added (green) lines.

Hopefully this isn't an issue for delta, because you can just use the internal setting for whatever the "line added" theme color is.

Closing Remarks & Comments

Let me know what you think of the idea!

* This suggestion might belong better in the bat project, but I feel like it fits with delta better due to the useful functionality is based on Git (i.e., git grep -W and git log -p -W), delta having good support and features for lots of Git things with highlighting, and because bat is just a highlighter for languages and should probably remain pure that way.

** _Technically it's a wrapper around bat, i.e. it parses the --function-context markers. I showed the command-line that it generates in the screenshot above, for an example.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions