ANSWERS · ANSWERS FOR CODING AGENTS

Syntax-aware code search: XERJ vs ast-grep

AUTHOR · XERJ documentation team · REVIEWED BY · XERJ engineering team · PUBLISHED · 2026-08-21 · UPDATED · 2026-11-13
SHORT ANSWER · UPDATED 2026-11-13

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.

PASTE THIS TO YOUR AI AGENT. IT DOES THE REST 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.
RUN THIS XERJ COMMAND curl -fsSL https://xerj.org/get | sh Install the release binary into ~/.local/bin.
RUN THIS XERJ COMMAND xerj autoindex . Index the tree. Code files are parsed with tree-sitter during this pass.
RUN THIS XERJ COMMAND xerj search merge_segments --field defs Files that declare the name, not every mention in the body.
RUN THIS XERJ COMMAND xerj search merge_segments --field body Raw text in the file body, including call sites.
RUN THIS XERJ COMMAND xerj def merge_segments --json Named-symbol lookup: kind, file, line, and a short excerpt.

The concept is What is syntax-aware search?.

Try it: pick the job, then the tool

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.

XERJ behaviour follows engine/crates/xerj-autoindex/src/extract/code.rs (36-language registry, 2 MiB code cap, 5,000 symbols per file, 200-byte name cap). The merge_segments and MergeSegments results are from the three-file capture described on this page (rust, python, go). No large repository was measured here.

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.

Recorded capture: 3 files, 3 languages

One index, two questions

match on body

{"query":{"match":{"body":"checkpoint_journal_offset"}}}
ax_pathscore
src/ingest.py0.5359981
src/lib.rs0.4608517

Any occurrence in the file text, including a call inside a function body.

match on defs

{"query":{"match":{"defs":"merge_segments"}}}
hits2 filesEach hit means the file declares the name, and each carries a symbols array.
missedMergeSegments in src/query.goThe token merge_segments did not match it. Spell the name the way the language does.
Both queries from the capture on this page, run against one index on 1 single-node XERJ process. Scores are the _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.

Recorded capture: the Rust hit

The symbols array, drawn by line

A line axis from 1 to 20 with four declarations: struct SegmentReader at line 3, function open at line 8, function checkpoint_journal_offset at line 12, function merge_segments at line 17. merge_segments is the defs hit. 1 5 10 15 20 struct SegmentReader 3 function open 8 function checkpoint_journal_offset 12 function merge_segments 17
The 4 entries in the 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.

Limits of the code extractor

What tree-sitter extraction covers

languages36Pinned by a unit test.
file size2 MiBPer source file. A larger file is refused, and the refusal names the cap.
symbols5,000Per file. The array stops at 5,000.
name length200 bytesA longer symbol name is skipped.
Caps from extract/code.rs. A file that a grammar fails to parse is indexed as plain text, not dropped.

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

Related