Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] Support ripgrep JSON format #768

Open
dandavison opened this issue Dec 10, 2023 · 1 comment
Open

[feature] Support ripgrep JSON format #768

dandavison opened this issue Dec 10, 2023 · 1 comment
Labels
enhancement New feature or request

Comments

@dandavison
Copy link

dandavison commented Dec 10, 2023

⭐ Suggestion

Hi @HerringtonDarkholme, ast-grep looks great. What do you think about adding support for ripgrep's JSON output format, so that tools that have been written to handle ripgrep's format would automatically work with ast-grep. (I'm the author of delta, which can render ripgrep's JSON format; see https://github.com/BurntSushi/ripgrep#related-tools:

image

).

In addition to the fact that ripgrep is popular and tooling has been written that targets its JSON format it's possible ripgrep's JSON format has other advantages, for example, rg --json retains non-matching context lines when -A, -B, or -C are used, whereas I believe ast-grep --json=stream doesn't.

Perhaps ast-grep --json=ripgrep could be identical to rg --json at the intersection of their output, but could also contain additional keys (e.g. language).

$ cat b.rs
aaa
bbb bbb bbb
ccc
$  sg --json=stream -B1 -p bbb .
{"text":"bbb","range":{"byteOffset":{"start":4,"end":7},"start":{"line":1,"column":0},"end":{"line":1,"column":3}},"file":"./b.rs","lines":"bbb bbb bbb","language":"Rust"}
{"text":"bbb","range":{"byteOffset":{"start":8,"end":11},"start":{"line":1,"column":4},"end":{"line":1,"column":7}},"file":"./b.rs","lines":"bbb bbb bbb","language":"Rust"}
{"text":"bbb","range":{"byteOffset":{"start":12,"end":15},"start":{"line":1,"column":8},"end":{"line":1,"column":11}},"file":"./b.rs","lines":"bbb bbb bbb","language":"Rust"}
$ rg -B1 --json bbb b.rs
{"type":"begin","data":{"path":{"text":"b.rs"}}}
{"type":"context","data":{"path":{"text":"b.rs"},"lines":{"text":"aaa\n"},"line_number":1,"absolute_offset":0,"submatches":[]}}
{"type":"match","data":{"path":{"text":"b.rs"},"lines":{"text":"bbb bbb bbb\n"},"line_number":2,"absolute_offset":4,"submatches":[{"match":{"text":"bbb"},"start":0,"end":3},{"match":{"text":"bbb"},"start":4,"end":7},{"match":{"text":"bbb"},"start":8,"end":11}]}}
{"type":"end","data":{"path":{"text":"b.rs"},"binary_offset":null,"stats":{"elapsed":{"secs":0,"nanos":160166,"human":"0.000160s"},"searches":1,"searches_with_match":1,"bytes_searched":20,"bytes_printed":440,"matched_lines":1,"matches":3}}}
{"data":{"elapsed_total":{"human":"0.001159s","nanos":1159416,"secs":0},"stats":{"bytes_printed":440,"bytes_searched":20,"elapsed":{"human":"0.000160s","nanos":160166,"secs":0},"matched_lines":1,"matches":3,"searches":1,"searches_with_match":1}},"type":"summary"}

💻 Use Cases

  • What do you want to use this for?
    Processing ast-grep output with existing tools
  • What workarounds are you using in the meantime?
    People could write scripts to transform ast-grep into ripgrep format (have to coalesce successive matches into a single record), but it wouldn't be very convenient/portable, and wouldn't get around the problem that context lines are missing.
@dandavison dandavison added the enhancement New feature or request label Dec 10, 2023
@HerringtonDarkholme
Copy link
Member

Hi @dandavison, thanks for giving ast-grep a try! I'm also a delta fan :)

Adding json=ripgrep is a reasonable extension to the output format.

I will consider supporting it in the future!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants