BrianHicks / tree-grepper

Like grep, but uses tree-sitter grammars to search
Other
288 stars 20 forks source link

proposal: CLI for tree-grepper 3.0 #436

Open BrianHicks opened 4 months ago

BrianHicks commented 4 months ago

Right now we have a conflict (#355) between single files and additional queries which means that queries cannot be run against single files.

To be honest, I've only ever needed additional queries in one situation, and in that situation it would have been fine to make two calls. (One each for Elm and JavaScript.)

I'd like to change the CLI to something like this (each line is an invocation, using Elm as an example):

tree-grepper elm "(import_statement)@import" # searches below .
tree-grepper elm "(import_statement)@import" some-root/ # searches below some-root
tree-grepper elm "(import_statement)@import" some-root/File.elm # searches only in the provided file

Multiple roots and files can also be specified and/or combined. This gets rid of the -q/--query flag, which would let us make tree-grepper faster by only loading a single language at a time.

I'd also like to refine the differences between --format and --show-tree. There are a couple possibilities for this:

I like the first option best, even though it's a little more typing, since it would let us show the trees for individual matches as well as entire files.

I'm going to let this sit for a while before implementing it. If you've got ideas, chime in!

rtfb commented 3 months ago

Since it's desirable to drop the -q flag (or at least make it optional), and there could be genuine clash between a language name and a directory with exactly the same name, it seems like the query parameter is begging for some syntax. A couple ideas come to mind:

  1. tree-grepper --elm "(import_statement)@import" some-root/ - i.e. make the language name a flag in itself.
  2. tree-grepper elm:"(import_statement)@import" some-root/ another-root/ - equip the query argument with its own syntax, separate the language name and the query string with a colon.

While both suggestions have a downside of being somewhat quirky and unusual, they would both work even if one of the search paths is also named elm, and in both cases the queries and paths become position-independent.

BrianHicks commented 3 months ago

I could have been clearer! The proposal moves away from using -q to having two fixed positional arguments and any number of files or search paths. The help string might look like:

tree-sitter LANG QUERY [PATH..]

So if you have a directory named elm and want to query it, that'd look like:

tree-sitter elm '(import_statement)' elm

It's already unambiguous positionally; no extra syntax required.

rtfb commented 3 months ago

Ah, gotcha, so you're pivoting towards having only a single query string, right? I was trying to come up with something that would still allow any number of queries and paths. But I haven't used tree-grepper much yet, so not sure if that's very useful in practice :-)

BrianHicks commented 3 months ago

I added it because I found it useful in one place, but it's just more noise and complexity in almost every other place. Scanning the filesystem is generally doesn't have as much overhead as I was planning for, so running tree-grepper twice is not such a huge deal (and if scanning twice represents a large amount of overhead, this design would also allow you to specify different subsets or files that you've scanned beforehand.)

meain commented 1 month ago

Ah, gotcha, so you're pivoting towards having only a single query string, right?

I think you could just string concat your queries instead of having to specify multiple times if you really need the feature.

BrianHicks commented 1 month ago

Yes, and in fact that’s what tree-grepper does internally. But before you could load multiple languages in an invocation. That’s what’s going away.