dandavison / delta

A syntax-highlighting pager for git, diff, grep, and blame output
https://dandavison.github.io/delta/
MIT License
21.81k stars 364 forks source link

Fix ripgrep trailing newline #1496

Open ippsav opened 1 year ago

ippsav commented 1 year ago

Description

Fix trailing newline issue in code highlights (#1487 )

The ripgrep JSON output includes a trailing newline at the end of each match. However, the code highlighting in delta does not expect a trailing newline, which was causing an issue when the match contains a newline at the end.

To fix this, We check if the trailing newline is part of the actual match text and it's at the end of the string. If not, the trailing newline is removed.

Unit tests:

ripgrep_json.rs:

dandavison commented 1 year ago

Could you give an example of a toy input file and a ripgrep command that crashes delta without this fix and behaves correctly with this fix?

ippsav commented 1 year ago

Here is an example @dandavison :

example file:

{"type":"match","data":{"path":{"text":"src/cli.rs"},"lines":{"text":"    fn from_clap_and_git_config(config\n"},"line_number":null,"absolute_offset":35837,"submatches":[{"match":{"text":"config\n"},"start":33,"end":39}]}}

Output of running the command with delta: 20230806_14h35m27s_grim

Output of running the same command with the fix: 20230806_14h36m57s_grim

I ve just realised the test cases are not really testing what it should be testing i ll be fixing it right now, my bad on that !

dandavison commented 1 year ago

Can you give the input file and ripgrep command that you're using to generate the example? I think if you include that command as a comment by the test case that would be helpful.

If you want to use delta's source code as the example input file you could make it reproducible via a command that specifies a master commit like

git show 0608765f40a193d16651e1ac0c547bd2f9280bf9:src/cli.rs | rg --json config
ippsav commented 1 year ago

here is a simpler example that relates to the example i mentioned above: raw.txt:

    fn from_clap_and_git_config(config

There is a new line at the end make sure you press enter.

The output of running cat raw.txt | rg -U --json "config\n"

{"type":"begin","data":{"path":{"text":"<stdin>"}}}
{"type":"match","data":{"path":{"text":"<stdin>"},"lines":{"text":"    fn from_clap_and_git_config(config\n"},"line_number":1,"absolute_offset":0,"submatches":[{"match":{"text":"config\n"},"start":32,"end":39}]}}
{"type":"end","data":{"path":{"text":"<stdin>"},"binary_offset":null,"stats":{"elapsed":{"secs":0,"nanos":14720,"human":"0.000015s"},"searches":1,"searches_with_match":1,"bytes_searched":40,"bytes_printed":265,"matched_lines":1,"matches":1}}}
{"data":{"elapsed_total":{"human":"0.000646s","nanos":646020,"secs":0},"stats":{"bytes_printed":265,"bytes_searched":40,"elapsed":{"human":"0.000015s","nanos":14720,"secs":0},"matched_lines":1,"matches":1,"searches":1,"searches_with_match":1}},"type":"summary"}
tbm commented 9 months ago

@ippsav do you have any time to update your PR with the comments from @dandavison ? I'm the guy who reported this issue and would like to see your PR in. Thank you for working on this!

ippsav commented 9 months ago

@tbm there have been a lot of work lately, hence I was not very active, I ll take a look at it very soon this week, sorry for the inconvenience !