Skip to content

Commit

Permalink
feat(cli): --<LANG>-query-file option for reading queries from files (
Browse files Browse the repository at this point in the history
#168)

This adds support for reading language queries from files on disk.

This new option works the same as the existing `--<LANG>-query` ones,
e.g. `--python-query '(some_node (some_other_node))'` etc., except
the argument to `--<LANG>-query-file` is a file path and the file
contains the query. Don't forget to `@bind` nodes to names for
results to show up etc.
  • Loading branch information
bheylin authored Nov 9, 2024
1 parent 4a513ec commit 4967356
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 114 deletions.
73 changes: 51 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ some combinations from making sense: for example, searching for a Python `class`
however:

```console
$ cat birds.py | srgn --py 'class' --py 'doc-strings' # From earlier example; note `--py` is a shorthand for `--python`
$ cat birds.py | srgn --py 'class' --py 'doc-strings'
8: """A bird!"""
19: """Create a bird from an egg."""
```
Expand Down Expand Up @@ -1198,7 +1198,7 @@ Typing out tree-sitter queries at the CLI can be unwieldy. To mitigate this you
Below we use the same Python file from the previous section with an invocation of

```bash
cat cond.py | srgn --python-query 'docs/python_cond_query.scm'
cat cond.py | srgn --python-query-file 'docs/python_cond_query.scm'
1:if x:
2: return left
3:else:
Expand Down Expand Up @@ -1581,12 +1581,16 @@ Language scopes:
- declaration: Declaration
- call-expression: Call expression

--c-query <TREE-SITTER-QUERY-OR-FILENAME>
Scope C code using a custom tree-sitter query. The query can be given inline or
as a path to a file containing a query.
--c-query <TREE-SITTER-QUERY-VALUE>
Scope C code using a custom tree-sitter query.
[env: C_QUERY=]

--c-query-file <TREE-SITTER-QUERY-FILENAME>
Scope C code using a custom tree-sitter query from file.
[env: C_QUERY_FILE=]

--csharp <CSHARP>
Scope C# code using a prepared query.
Expand All @@ -1611,12 +1615,16 @@ Language scopes:
- attribute: Attribute names
- identifier: Identifier names

--csharp-query <TREE-SITTER-QUERY-OR-FILENAME>
Scope C# code using a custom tree-sitter query. The query can be given inline or
as a path to a file containing a query.
--csharp-query <TREE-SITTER-QUERY-VALUE>
Scope C# code using a custom tree-sitter query.
[env: CSHARP_QUERY=]

--csharp-query-file <TREE-SITTER-QUERY-FILENAME>
Scope C# code using a custom tree-sitter query from file.
[env: CSHARP_QUERY_FILE=]

--go <GO>
Scope Go code using a prepared query.
Expand Down Expand Up @@ -1645,12 +1653,16 @@ Language scopes:
- goto: `goto` statements
- struct-tags: Struct tags

--go-query <TREE-SITTER-QUERY-OR-FILENAME>
Scope Go code using a custom tree-sitter query. The query can be given inline or
as a path to a file containing a query.
--go-query <TREE-SITTER-QUERY-VALUE>
Scope Go code using a custom tree-sitter query.
[env: GO_QUERY=]

--go-query-file <TREE-SITTER-QUERY-FILENAME>
Scope Go code using a custom tree-sitter query from file.
[env: GO_QUERY_FILE=]

--hcl <HCL>
Scope HashiCorp Configuration Language code using a prepared query.
Expand All @@ -1673,12 +1685,17 @@ Language scopes:
- comments: Comments
- strings: Literal strings

--hcl-query <TREE-SITTER-QUERY-OR-FILENAME>
--hcl-query <TREE-SITTER-QUERY-VALUE>
Scope HashiCorp Configuration Language code using a custom tree-sitter query.
The query can be given inline or as a path to a file containing a query.
[env: HCL_QUERY=]

--hcl-query-file <TREE-SITTER-QUERY-FILENAME>
Scope HashiCorp Configuration Language code using a custom tree-sitter query
from file.
[env: HCL_QUERY_FILE=]

--python <PYTHON>
Scope Python code using a prepared query.
Expand Down Expand Up @@ -1713,12 +1730,16 @@ Language scopes:
- types: Types in type hints
- identifiers: Identifiers (variable names, ...)

--python-query <TREE-SITTER-QUERY-OR-FILENAME>
Scope Python code using a custom tree-sitter query. The query can be given
inline or as a path to a file containing a query.
--python-query <TREE-SITTER-QUERY-VALUE>
Scope Python code using a custom tree-sitter query.
[env: PYTHON_QUERY=]

--python-query-file <TREE-SITTER-QUERY-FILENAME>
Scope Python code using a custom tree-sitter query from file.
[env: PYTHON_QUERY_FILE=]

--rust <RUST>
Scope Rust code using a prepared query.
Expand Down Expand Up @@ -1774,12 +1795,16 @@ Language scopes:
- unsafe: `unsafe` keyword usages (`unsafe fn`, `unsafe` blocks,
`unsafe Trait`, `unsafe impl Trait`)

--rust-query <TREE-SITTER-QUERY-OR-FILENAME>
Scope Rust code using a custom tree-sitter query. The query can be given inline
or as a path to a file containing a query.
--rust-query <TREE-SITTER-QUERY-VALUE>
Scope Rust code using a custom tree-sitter query.
[env: RUST_QUERY=]

--rust-query-file <TREE-SITTER-QUERY-FILENAME>
Scope Rust code using a custom tree-sitter query from file.
[env: RUST_QUERY_FILE=]

--typescript <TYPESCRIPT>
Scope TypeScript code using a prepared query.
Expand Down Expand Up @@ -1808,12 +1833,16 @@ Language scopes:
- namespace: `namespace` blocks
- export: `export` blocks

--typescript-query <TREE-SITTER-QUERY-OR-FILENAME>
Scope TypeScript code using a custom tree-sitter query. The query can be given
inline or as a path to a file containing a query.
--typescript-query <TREE-SITTER-QUERY-VALUE>
Scope TypeScript code using a custom tree-sitter query.
[env: TYPESCRIPT_QUERY=]

--typescript-query-file <TREE-SITTER-QUERY-FILENAME>
Scope TypeScript code using a custom tree-sitter query from file.
[env: TYPESCRIPT_QUERY_FILE=]

Options (german):
--german-prefer-original
When some original version and its replacement are equally legal, prefer the
Expand Down
Loading

0 comments on commit 4967356

Please sign in to comment.