Attempt at a unified diff output format for use with --replace #2149
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a draft for what could be a unified diff output for ripgrep when using the
--replace
option.The chance for acceptance is low considering the feature request #74 was closed,
however this is an implementation of @samuelcolvin suggestion to produce a diff output format
instead of actually writing to files. The job of changing files can then be handed to something like
patch
orgit apply
.I wanted to at least investigate what this would look like and managed to get something working,
so I hope this can be a way to discuss around some actual code. @BurntSushi stated they were
actively looking to moving more functionality into a library, so I'm not sure if this was totally dismissed.
I ran into a few problems with the replacement buffer which seems to contain more than the original match
and I managed to reproduce this with ripgrep 13.0.0, notes are in the code. I managed to get it working by
modifying the replacement, not sure but it could be related to #2095.
There is also no support for additional context, as that would require keeping track of it before and after a match
to then produce the correct hunk header with context included, and I'm not very familiar with the code or rust
to come up with a nice way to do that.
The implementation is mostly taken from the JSON printer with necessary parts replaced.
It can handle multiline replacements that differ in the amount of lines before/after replacement.
I've tested mostly with running the following in the ripgrep repo:
as these also produced the spurious output I found with rg 13.0.0.
Outstanding stuff to leave draft status: