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 adds a new option,
--format
which allows specifying a new output format that encodes each result as a JSON document. Note that this means that the script's output as a whole is NOT valid JSON (it's not an array of matches) but that each output MATCH is valid JSON. This is how ripgrep outputs JSON and is probably better suited for a tool which generates its output using an iterator.Originally I wanted to support the same format as ripgrep's
--json
option, but the format used by ripgrep is quite complex in order to support the matching and context options of that tool, whereas grepdef will only ever report one line per match. Therefore, I opted to create a custom format which is just a JSON serialization of eachSearchResult
.In order to make it clear that this option is unique, I've avoided using the
--json
option and instead require--format json-per-match
. If other JSON formats are ever added in the future, this leaves the option open to give them more specific names.Even though the format option is stored in the state of the
Searcher
, I also needed a way to "enforce" it at the point where eachSearchResult
is printed. This was tricky with the API of thesearch
function because it just returns objects with no concern for how they are printed. To that end I created a new method,search_and_format
which returns a vector of formatted Strings instead of SearchResults. This method is then used in the binary. Another advantage of this API is that the actual output of the binary's search flow can be checked in the integration tests, which is something I've wanted.