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 idea: "context matches" #346

Closed
quodlibetor opened this issue Feb 2, 2017 · 9 comments
Closed

Feature idea: "context matches" #346

quodlibetor opened this issue Feb 2, 2017 · 9 comments
Labels
enhancement An enhancement to the functionality of the software. libripgrep An issue related to modularizing ripgrep into libraries.

Comments

@quodlibetor
Copy link

I "often" find myself doing something like:

$ rg -A5 something | grep -B5 something-else

it would be kind of awesome if rg took a --context-matches regex (that requires one of the other context specifiers (-A -B -C)) that does this automatically for me.

$ rg -A5 -B2 --context-matches 'some.regex' 'main.search'
...
$ rg --context-matches 'some.regex' 'main.search'
error: --context-matches requires one of --before, --after, or --context
@BurntSushi
Copy link
Owner

I'm not against this on principle, but it would be a very difficult feature to add in the current implementation.

@BurntSushi BurntSushi added enhancement An enhancement to the functionality of the software. libripgrep An issue related to modularizing ripgrep into libraries. labels Feb 2, 2017
@quodlibetor
Copy link
Author

Ah, that makes sense. I was hoping that it would be as simple as just caching the output for each chunk for a bit and running a regex over that cached chunk.

@quodlibetor
Copy link
Author

Which is obviously less efficient than some kind of fancy one-pass analysis.

@timotheecour
Copy link

related to: #360

@BurntSushi
Copy link
Owner

I was hoping that it would be as simple as just caching the output for each chunk for a bit and running a regex over that cached chunk.

The current implementation already does that. It has to, because it doesn't know which lines to print until a match occurs.

You're welcome to go look at the code and marvel at just how hard an addition like this would be. The context handling is complicated, which is why I've tagged this with libripgrep. Namely, we should think about this use case while refactoring the search code into a reusable library.

@Hywan
Copy link

Hywan commented Mar 3, 2017

Hello,

It might sound stupid, but what would be the difference between:

$ rg --context-matches 'foo' 'bar'

and:

$ rg 'foo.*bar'

@BurntSushi
Copy link
Owner

BurntSushi commented Mar 3, 2017 via email

@BurntSushi
Copy link
Owner

I'm going to close this not only because the implementation complexity of this scares me a little, but it opens the door to a whole new world of feature bloat that I'd rather avoid. I'd kindly suggest that if someone wants a search this sophisticated, they should just write some code or figure out a clever work-around using the existing context flags. :-)

@jdtsmith
Copy link

jdtsmith commented Feb 20, 2023

A related idea that might be much easier to implement: a context state match. Rather than printing lines before or after the match, a context match would print the most recent matching line of context, if any. Any given context match is only printed (once) when the main match hits. So with a file like:

* foo
** bar
*** baz
something
*boo
something else

and rg --context-match '^\*+' something would produce:

*** baz
something
* boo
something else

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement An enhancement to the functionality of the software. libripgrep An issue related to modularizing ripgrep into libraries.
Projects
None yet
Development

No branches or pull requests

5 participants