Open BrianHicks opened 4 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:
tree-grepper --elm "(import_statement)@import" some-root/
- i.e. make the language name a flag in itself.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.
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.
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 :-)
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.)
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.
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.
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):
Multiple roots and files can also be specified and/or combined. This gets rid of the
-q
/--query
flag, which would let us maketree-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:tree
becomes an output for--format
. Consistent, but needstree-grepper elm "(.)" --format tree some/File.elm
.--show-tree
and--format
conflict—tree view is a different mode specified by a flag.--show-tree
outputs a tree in json format if--format=json
--show-tree
moves to a separate binary liketree-grepper-show-tree
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!