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 printing context around matches #464

Closed
joshtriplett opened this issue Jun 22, 2023 · 1 comment · Fixed by #545
Closed

[feature] Support printing context around matches #464

joshtriplett opened this issue Jun 22, 2023 · 1 comment · Fixed by #545
Assignees
Labels
enhancement New feature or request

Comments

@joshtriplett
Copy link

⭐ Suggestion

grep and git grep support printing context lines around matches. -C 3 prints three lines of context before and after each match; -A 3 prints three lines after, and -B 3 prints three lines before.

I'd love to have the same functionality in ast-grep's sg run: sg -C 3 -p xyz.

As with grep and git grep, context lines should be printed slightly differently than matching lines. When printing context lines, grep and git grep print filename: before each matching line but filename- before each non-matching context line, and print a -- between matches. If a line in the context also matches the pattern, they print that line as filename: as well, and extend the context. For instance, for git grep -C3 matching:

...
--
file- context line
file- context line
file- context line
file: matching line
file- context line
file: another matching line
file- context line
file- context line
file- context line
--
...

💻 Use Cases

Having context makes it easier to skim through a series of matches. I use these options quite frequently with grep and git grep. Not having them in sg means I may need to check each match one by one, such as in an editor, rather than skimming through a series of them and only looking at ones with relevant context.

@HerringtonDarkholme
Copy link
Member

I will add the feature in future releases. At the moment I will unblock the feature by these two issues. #512 #513

Stay tuned! Hopefully it can be released in ast-grep 0.10.0 or so.

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
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants