Syntax-aware code search: XERJ vs ast-grep
How do I run syntax-aware code search, and how does it compare to ast-grep? XERJ uses tree-sitter to store each declared symbol as kind, name and line, across 36 languages. Run xerj search --field defs or xerj def to find declarations. XERJ rewrites nothing. For a structural rewrite, use ast-grep or a language server.
TL;DR Syntax-aware code search in XERJ is a tree-sitter pass during autoindex: each declared symbol is stored as kind, name and line, across 36 languages. Run xerj search --field defs or xerj def to find declarations, and a body query to find the text. XERJ rewrites nothing. For a structural rewrite, use ast-grep or a language server.
Read https://xerj.org/llms.txt. Install xerj, autoindex the repo, then run xerj search --field defs for declarations or xerj def for a named symbol. Prefer a body query when you want call sites, not only declarations. Do not use xerj to rewrite source; use ast-grep or a language server for a structural rewrite. Quote file:line, and say so when a query returns no hits.
curl -fsSL https://xerj.org/get | sh
Install the release binary into ~/.local/bin.
xerj autoindex .
Index the tree. Code files are parsed with tree-sitter during this pass.
xerj search merge_segments --field defs
Files that declare the name, not every mention in the body.
xerj search merge_segments --field body
Raw text in the file body, including call sites.
xerj def merge_segments --json
Named-symbol lookup: kind, file, line, and a short excerpt.
The concept is What is syntax-aware search?.
What XERJ searches, and what ast-grep rewrites
Use XERJ, match on defs
XERJ parses each source file with tree-sitter and writes declared symbols into a defs field and a symbols array (kind, name, line). A match on defs returns the files that declare the name. In the capture on this page, merge_segments returned 2 files that declare it. Extraction is definitions only: there is no call graph and no cross-file reference resolution. 36 languages are covered, pinned by a unit test.
Use XERJ, match on body
The same index also holds the file body. A match on body finds raw text, including call sites that are not declarations. If a grammar fails to parse a file, XERJ indexes that file as plain text, so the text query still works. Case matters: src/query.go declares MergeSegments, and the token merge_segments did not match it in the capture.
Use ast-grep, not XERJ
XERJ rewrites nothing. For a structural rewrite, or a rename that must be sound, use ast-grep or a language server. This page does not report an ast-grep benchmark; the split is about the job, not a speed claim.
What each tool is for
XERJ and ast-grep both read syntax, and they do different jobs.
XERJ is a search index. During xerj autoindex, it parses each source file with tree-sitter and writes declared symbols into a defs field and a symbols array (kind, name, line). You query that index later: a defs query returns the files that declare a name; a body query returns raw text, including call sites. The result is file:line, for a coding agent to read.
ast-grep is a structural search and rewrite tool. It matches a pattern against a syntax tree and can apply an edit. Use it when the job is to change the code, or when a rename must be sound. This page does not report an ast-grep benchmark. The split is about the job, not a speed claim.
If you already know the exact string and want every line, ripgrep is still the right tool. The local MCP answer covers ranked passages versus grep in tokens and bytes.
One index, two questions
match on body
{"query":{"match":{"body":"checkpoint_journal_offset"}}}
| ax_path | score |
|---|---|
src/ingest.py | 0.5359981 |
src/lib.rs | 0.4608517 |
Any occurrence in the file text, including a call inside a function body.
match on defs
{"query":{"match":{"defs":"merge_segments"}}}
symbols array.MergeSegments in src/query.goThe token merge_segments did not match it. Spell the name the way the language does._score values returned.Run the two queries
Index the tree once, then search. This example uses a name from the capture on this page.
curl -fsSL https://xerj.org/get | sh
xerj autoindex .
xerj search merge_segments --field defs
xerj search merge_segments --field body
xerj def merge_segments --json
A defs query answers "where is this declared?". A body query answers "where does this text appear?". xerj def returns the declaration: kind, file, line and a short excerpt, which is the form a coding agent can quote.
The symbols array, drawn by line
symbols array of src/lib.rs, each with a kind, a name and the line of its declaration. The axis is drawn to line 20. A rename tool starts from these lines.What the capture showed
The capture used one node and three files, in rust, python and go. Both the body query and the defs query returned 2 hits on that tree. That is a small tree, labelled as a capture, not a repository-scale measurement.
Case matters. src/query.go declares MergeSegments, and the token merge_segments did not match it. If a grammar fails to parse a file, XERJ indexes that file as plain text, so a body query still works. Extraction is definitions only: there is no call graph, and there is no cross-file reference resolution.
The 36-language list lives in engine/crates/xerj-autoindex/src/extract/code.rs and is pinned by a unit test.
What tree-sitter extraction covers
Caps, and when not to use this
A code file larger than 2 MiB is skipped (CODE_CAP). At most 5,000 symbols are stored per file. A symbol name longer than 200 bytes is dropped. Those caps are in the same extract module.
Do not use XERJ to rewrite source. It has no rewrite command. For a structural rewrite, use ast-grep or a language server.
Do not treat a defs query as a reference or call-site search. It finds declarations only.
Do not expect a case-insensitive match. The capture missed MergeSegments when the query was merge_segments.
Wiring these queries into Claude Code over MCP is in code search MCP for Claude Code. For the coding-agent loop that retrieves a declaration before it writes, see the reference-coding case study.
Use the Copy the prompt button if you want an agent to run a defs or body query. Ask it to quote file:line, and to leave rewrites to ast-grep or a language server.
FAQ
How do I run syntax-aware code search in XERJ?
Install xerj and run xerj autoindex on the repository. Then run xerj search --field defs to find declarations, or xerj def for a named symbol. A body query finds the raw text, including call sites.
How does XERJ compare to ast-grep?
XERJ finds declarations and text, with file:line. It rewrites nothing. ast-grep matches syntax trees and can apply a structural rewrite. Use XERJ to locate a symbol; use ast-grep when the job is to change the code.
How many languages does the tree-sitter pass cover?
36, in the registry in engine/crates/xerj-autoindex/src/extract/code.rs, pinned by a unit test. If a grammar fails to parse a file, XERJ indexes that file as plain text, so a body query still works.
Does a defs query find every mention of a name?
No. It finds files that declare the name. Use a body query for call sites. There is no call graph and no cross-file reference resolution.
Are the queries case-sensitive?
Yes, in the capture on this page. src/query.go declares MergeSegments, and the token merge_segments did not match it.
What are the caps?
A code file larger than 2 MiB is skipped. At most 5,000 symbols are stored per file. A symbol name longer than 200 bytes is dropped.
Evidence
- 36 languages in the tree-sitter registry, pinned by a unit test; CODE_CAP 2 MiB; max 5,000 symbols per file; name cap 200 bytes; definitions only, no call graph Source:
engine/crates/xerj-autoindex/src/extract/code.rs
Related
- What is syntax-aware search?
- Local code search MCP that uses fewer tokens than grep
- I need my agent to find a passage and cite the file, not dump the whole tree. How?
- Chat with a GitHub repo without cloning it
- Is ripgrep enough for a coding agent, or do I need a code index?
- Reference-coding for Claude Code