{"id":13496649,"url":"https://github.com/BrianHicks/tree-grepper","last_synced_at":"2025-03-28T19:30:44.981Z","repository":{"id":36953280,"uuid":"396801979","full_name":"BrianHicks/tree-grepper","owner":"BrianHicks","description":"Like grep, but uses tree-sitter grammars to search","archived":false,"fork":false,"pushed_at":"2025-03-20T00:07:35.000Z","size":11742,"stargazers_count":310,"open_issues_count":27,"forks_count":20,"subscribers_count":6,"default_branch":"main","last_synced_at":"2025-03-24T12:15:24.984Z","etag":null,"topics":["hacktoberfest"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/BrianHicks.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-08-16T13:15:23.000Z","updated_at":"2025-03-18T16:43:35.000Z","dependencies_parsed_at":"2023-11-15T12:23:31.954Z","dependency_job_id":"999cbd15-bbe5-4361-868b-aa5fcf85b3ad","html_url":"https://github.com/BrianHicks/tree-grepper","commit_stats":{"total_commits":926,"total_committers":15,"mean_commits":"61.733333333333334","dds":0.519438444924406,"last_synced_commit":"96376ced2526ec42c9fdacf92f6e46bdfe286410"},"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BrianHicks%2Ftree-grepper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BrianHicks%2Ftree-grepper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BrianHicks%2Ftree-grepper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BrianHicks%2Ftree-grepper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/BrianHicks","download_url":"https://codeload.github.com/BrianHicks/tree-grepper/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245476717,"owners_count":20621698,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hacktoberfest"],"created_at":"2024-07-31T19:01:54.780Z","updated_at":"2025-03-28T19:30:44.975Z","avatar_url":"https://github.com/BrianHicks.png","language":"Rust","readme":"# tree-grepper\n\nWorks like `grep`, but uses `tree-sitter` to search for structure instead of strings.\n[Here's a longer introduction to the tool as a blog post](https://bytes.zone/posts/tree-grepper/).\n\n## Installing\n\nUse [`nix`](https://nixos.org/download.html) to install:\n\n1. if you have `cachix` installed, `cachix use tree-grepper`.\n2. `nix-env -if https://github.com/BrianHicks/tree-grepper/archive/refs/heads/main.tar.gz`\n\n## Usage\n\nUse it like `grep` (or really, more like `ack`/`ag`/`pt`/`rg`.)\n\n```console\n$ tree-grepper -q javascript '(call_expression)'\n./tests/cmd/hello-world.js:1:1:query:console.log(\"Hello, World!\")\n\n```\n\nBy default, `tree-grepper` will output one match per (newline-delimited) line.\nThe columns here are filename, row, column, match name, and match text.\n\nNote, however, that if your query includes a match with newlines in the text they will be included in the output!\nIf this causes problems for your use case, try asking for JSON output (`-f json`) instead.\n\n`tree-grepper` uses Tree-sitter's s-expressions to find matches.\nSee [the tree-sitter docs on queries](https://tree-sitter.github.io/tree-sitter/using-parsers#pattern-matching-with-queries) for what all you can do there.\n\nWe add one important thing on top of the standard query stuff (including `#eq?` and `#match?`): if you name a pattern starting with an underscore, it will not be returned in the output.\nThis is primarily useful for filtering out matches you don't really care about.\nFor example, to match JavaScript calls to `require` but not other functions, you could do this:\n\n```scheme\n(call_expression (identifier)@_fn (arguments . (string)@import .) (#eq? @_fn require))\n```\n\nIn addition to text output, we support JSON output for scripting: just  specify `-f json`.\nYou also get more info (the match's end location and node kind) by asking for JSON output.\n\n### Tree View\n\nYou can discover the node names your language uses by using `--show-tree languagename path/to/file`.\nWhen you do this, `tree-grepper` will parse the file and print out an indented tree view.\n\nFor a file like this:\n\n```javascript\nconsole.log(\"Hello, World!\");\n```\n\nYou'll get a tree like this:\n\n```console\n$ tree-grepper --show-tree javascript tests/cmd/hello-world.js\nprogram 1:1\n  expression_statement 1:1\n    call_expression 1:1\n      member_expression 1:1\n        identifier 1:1: console\n        . 1:8: .\n        property_identifier 1:9: log\n      arguments 1:12\n        ( 1:12: (\n        string 1:13\n          \" 1:13: \"\n          string_fragment 1:14: Hello, World!\n          \" 1:27: \"\n        ) 1:28: )\n    ; 1:29: ;\n\n```\n\nEach line takes the format `{node name} {location} {source, if present}`.\nSource is only shown for the leaf-most nodes on the tree to avoid printing a huge block.\nHowever, tree-grepper can extract text from any of these nodes.\n\nYou can use the node names in queries.\nFor example:\n\n- `tree-grepper -q elm (exposed_value)` would have matches on `average` and `percentOf`.\n- `tree-grepper -q elm (module_declaration)` would match on the whole declaration, `module Math exposing (average, percentOf)`\n\n## Supported Languages\n\nYou can find all the languages in your installed version of `tree-grepper` by running `tree-grepper --languages`.\nThe output is the literal language strings you can use in queries or `--show-tree`.\nFor example, here's the list for the current version of `tree-grepper`:\n\n```console\n$ tree-grepper --languages\nc\ncpp\ncuda\nelixir\nelm\ngo\nhaskell\njava\njavascript\nmarkdown\nnix\nphp\npowershell\npython\nruby\nrust\nsass\ntypescript\n\n```\n\nDon't see your favorite?\nWe're open to PRs for adding whatever language you'd like!\n\nFor development, there's a nix-shell setup that'll get you everything you need.\nSet up [nix](https://nixos.org/download.html) (just Nix, not NixOS) and then run `nix-shell` in the root of this repository.\n\nAfter that, you just need to add a tree-sitter grammar to the project.\n[The tree-sitter project keeps an up-to-date list](https://tree-sitter.github.io/tree-sitter/), so you may not even need to write your own!\n\nNote: when you're adding grammars, please keep things in alphabetical order.\n\n1. Add your grammar as an input in `flakes.nix`, following the template of the ones already there.\n   You'll need to add an entry in `inputs` and another in the `updateVendor` script.\n2. Run `direnv reload` to make sure you have the latest changes, then `update-vendor` to get your grammar in the right place.\n   Make sure the repo content under `vendor/YOUR-GRAMMAR` looks how you expect.\n3. Set up compilation in [`build.rs`](./build.rs) by following the pattern there.\n4. Set up a new target in [`src/language.rs`](./src/language.rs) by following the patterns there.\n5. Add the language to the list of supported languages in this readme.\n\n## License\n\nSee [LICENSE](./LICENSE) in the source.\n","funding_links":[],"categories":["Rust","Other"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FBrianHicks%2Ftree-grepper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FBrianHicks%2Ftree-grepper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FBrianHicks%2Ftree-grepper/lists"}