@mrclrchtr/supi-tree-sitter 1.3.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,72 +1,79 @@
1
1
  # @mrclrchtr/supi-tree-sitter
2
2
 
3
- Structural code analysis for PI your agent parses code, not just text.
3
+ Adds a `tree_sitter` tool to the [pi coding agent](https://github.com/earendil-works/pi) for parser-based structural code analysis.
4
4
 
5
- Grep matches strings. Tree-sitter parses structure — functions, classes, imports, call chains. The agent stops pattern-matching and starts understanding your code.
6
-
7
- ## What you get
5
+ ## Install
8
6
 
9
- ### See code structure
7
+ ```bash
8
+ pi install npm:@mrclrchtr/supi-tree-sitter
9
+ ```
10
10
 
11
- Extract functions, classes, interfaces, and methods from any file. The agent knows what lives where without reading every line.
11
+ For local development:
12
12
 
13
- ### Trace call chains
13
+ ```bash
14
+ pi install ./packages/supi-tree-sitter
15
+ ```
14
16
 
15
- Find every function call from a given location. The agent follows the code's actual shape instead of guessing from text proximity.
17
+ After editing the source, run `/reload`.
16
18
 
17
- ### 14 languages
19
+ ## What you get
18
20
 
19
- JavaScript, TypeScript, Python, Rust, Go, C, C++, Java, Kotlin, Ruby, Bash, HTML, R, SQL — get structural analysis for every project you touch.
21
+ After install, pi gets one tool:
20
22
 
21
- Works standalone or alongside LSP. Grammar files are vendored no native toolchain required at install time.
23
+ - `tree_sitter` inspect code structure through Tree-sitter parsers instead of plain text search
22
24
 
23
- ## Install
25
+ ## `tree_sitter` actions
24
26
 
25
- ```bash
26
- pi install npm:@mrclrchtr/supi-tree-sitter
27
- ```
27
+ | Action | What it is for | Current language coverage |
28
+ | --- | --- | --- |
29
+ | `outline` | List structural declarations such as functions, classes, interfaces, and methods | JavaScript / TypeScript only |
30
+ | `imports` | List import statements | JavaScript / TypeScript only |
31
+ | `exports` | List export declarations, re-exports, and export assignments | JavaScript / TypeScript only |
32
+ | `node_at` | Show the syntax node at a position, including ancestry | Any supported grammar |
33
+ | `query` | Run a custom Tree-sitter query against a file | Any supported grammar |
34
+ | `callees` | Find outgoing calls from the enclosing function or method at a position | Supported for most grammars, but not all |
28
35
 
29
- ## Quick look
36
+ Coordinates use **1-based** line and character columns. Character positions use UTF-16 code units.
30
37
 
31
- The agent gets a `tree_sitter` tool with these actions:
38
+ ## Supported file families
32
39
 
33
- | Action | What it does |
34
- |--------|-------------|
35
- | `outline` | List functions, classes, interfaces in a file |
36
- | `callees` | Find all function calls from a position |
37
- | `imports` / `exports` | See what a file imports and exports |
38
- | `node_at` | Inspect the AST node at any line/column |
39
- | `query` | Run a custom Tree-sitter query |
40
+ The current tool description covers:
40
41
 
41
- `outline`, `imports`, and `exports` are currently JavaScript/TypeScript only. `node_at`, `query`, and `callees` work across all 14 supported languages. Coordinates are 1-based, matching the `lsp` tool convention.
42
+ - JavaScript / TypeScript (`.js`, `.jsx`, `.ts`, `.tsx`, `.mjs`, `.cjs`, `.mts`, `.cts`)
43
+ - Python (`.py`, `.pyi`)
44
+ - Rust (`.rs`)
45
+ - Go (`.go`, `.mod`)
46
+ - C / C++ (`.c`, `.h`, `.cpp`, `.hpp`, `.cc`, `.cxx`, `.hxx`, `.c++`, `.h++`)
47
+ - Java (`.java`)
48
+ - Kotlin (`.kt`, `.kts`)
49
+ - Ruby (`.rb`)
50
+ - Bash / shell (`.sh`, `.bash`, `.zsh`)
51
+ - HTML (`.html`, `.htm`, `.xhtml`)
52
+ - R (`.r`)
53
+ - SQL (`.sql`)
42
54
 
43
55
  ## Package surfaces
44
56
 
45
- - `@mrclrchtr/supi-tree-sitter/api` — reusable parsing/session API
57
+ - `@mrclrchtr/supi-tree-sitter/api` — reusable parsing session factory and shared result types
46
58
  - `@mrclrchtr/supi-tree-sitter/extension` — pi extension entrypoint
47
59
 
48
- `pi.extensions` still points at the real file path `./src/extension.ts` inside the package. The `/api` and `/extension` paths are consumer-facing package exports, not manifest aliases.
49
-
50
- ## For extension developers
51
-
52
- This package exports a reusable session-scoped parsing service:
60
+ Example:
53
61
 
54
62
  ```ts
55
63
  import { createTreeSitterSession } from "@mrclrchtr/supi-tree-sitter/api";
56
64
 
57
65
  const session = createTreeSitterSession("/project");
58
66
 
59
- // Check if a file is parseable
60
- const result = await session.canParse("src/index.ts");
61
-
62
- // Get structural outline
67
+ const parseable = await session.canParse("src/index.ts");
63
68
  const outline = await session.outline("src/index.ts");
64
-
65
- // Trace outgoing calls from a position
66
69
  const callees = await session.calleesAt("src/index.ts", 42, 10);
67
70
 
68
- // Always clean up
69
71
  session.dispose();
70
72
  ```
71
73
 
72
- Call `dispose()` when done.
74
+ ## Source
75
+
76
+ - `src/tree-sitter.ts` — tool registration and action handling
77
+ - `src/runtime.ts` — parser and query runtime
78
+ - `src/session.ts` — reusable session API
79
+ - `src/outline.ts`, `src/imports.ts`, `src/exports.ts`, `src/node-at.ts`, `src/callees.ts` — structural analyses
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mrclrchtr/supi-tree-sitter",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
4
4
  "description": "SuPi Tree-sitter extension — structural AST analysis for pi",
5
5
  "license": "MIT",
6
6
  "repository": {
package/src/api.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  // Public tree-sitter session factory and shared types.
2
2
 
3
- export { createTreeSitterSession } from "./session.ts";
3
+ export { createTreeSitterSession } from "./session/session.ts";
4
4
  export type {
5
5
  CalleesAtResult,
6
6
  ExportRecord,
package/src/index.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  // Public session factory and re-exports for @mrclrchtr/supi-tree-sitter.
2
2
 
3
- export { createTreeSitterSession } from "./session.ts";
3
+ export { createTreeSitterSession } from "./session/session.ts";
4
4
  export type {
5
5
  CalleesAtResult,
6
6
  ExportRecord,
@@ -3,9 +3,9 @@
3
3
  import * as fs from "node:fs";
4
4
  import * as path from "node:path";
5
5
  import type { Language, Parser, Tree } from "web-tree-sitter";
6
- import { nodeToRange } from "./coordinates.ts";
7
- import { detectGrammar, resolveGrammarWasmPath } from "./language.ts";
8
- import type { GrammarId, QueryCapture, TreeSitterResult } from "./types.ts";
6
+ import { nodeToRange } from "../coordinates.ts";
7
+ import { detectGrammar, resolveGrammarWasmPath } from "../language.ts";
8
+ import type { GrammarId, QueryCapture, TreeSitterResult } from "../types.ts";
9
9
 
10
10
  interface ParserEntry {
11
11
  parser: Parser;
@@ -1,14 +1,13 @@
1
1
  // Session factory — creates a TreeSitterSession bound to a working directory.
2
2
 
3
- import { detectGrammar, isJsTsGrammar } from "./language.ts";
4
- import { TreeSitterRuntime } from "./runtime.ts";
3
+ import { detectGrammar, isJsTsGrammar } from "../language.ts";
5
4
  import {
6
5
  extractExports,
7
6
  extractImports,
8
7
  extractOutline,
9
8
  lookupCalleesAt,
10
9
  lookupNodeAt,
11
- } from "./structure.ts";
10
+ } from "../tool/structure.ts";
12
11
  import type {
13
12
  CalleesAtResult,
14
13
  ExportRecord,
@@ -18,7 +17,8 @@ import type {
18
17
  QueryCapture,
19
18
  TreeSitterResult,
20
19
  TreeSitterSession,
21
- } from "./types.ts";
20
+ } from "../types.ts";
21
+ import { TreeSitterRuntime } from "./runtime.ts";
22
22
 
23
23
  /**
24
24
  * Create a new Tree-sitter session bound to the given working directory.
@@ -1,8 +1,8 @@
1
1
  // Structural callee extraction — enclosing-scope lookup with per-language queries.
2
2
 
3
- import { detectGrammar } from "./language.ts";
4
- import type { TreeSitterRuntime } from "./runtime.ts";
5
- import type { GrammarId, SourceRange, TreeSitterResult } from "./types.ts";
3
+ import { detectGrammar } from "../language.ts";
4
+ import type { TreeSitterRuntime } from "../session/runtime.ts";
5
+ import type { GrammarId, SourceRange, TreeSitterResult } from "../types.ts";
6
6
 
7
7
  /** Result shape returned by lookupCalleesAt. */
8
8
  export interface CalleesAtResult {
@@ -1,9 +1,9 @@
1
1
  // Export extraction for supported files.
2
2
 
3
- import { nodeToRange } from "./coordinates.ts";
4
- import type { TreeSitterRuntime } from "./runtime.ts";
5
- import type { SyntaxNodeLike } from "./syntax-node.ts";
6
- import type { ExportRecord, TreeSitterResult } from "./types.ts";
3
+ import { nodeToRange } from "../coordinates.ts";
4
+ import type { TreeSitterRuntime } from "../session/runtime.ts";
5
+ import type { SyntaxNodeLike } from "../syntax-node.ts";
6
+ import type { ExportRecord, TreeSitterResult } from "../types.ts";
7
7
 
8
8
  /** Extract export records from a supported file. */
9
9
  export async function extractExports(
@@ -1,6 +1,6 @@
1
1
  // Formatting helpers for tree_sitter tool output.
2
2
 
3
- import type { OutlineItem, TreeSitterResult } from "./types.ts";
3
+ import type { OutlineItem, TreeSitterResult } from "../types.ts";
4
4
 
5
5
  export const MAX_ITEMS = 100;
6
6
 
@@ -0,0 +1,22 @@
1
+ // Prompt guidance and tool description for the tree_sitter tool.
2
+ //
3
+ // Note: We intentionally do NOT include cross-tool routing (e.g., "use lsp for
4
+ // type info") because this package can be installed standalone without supi-lsp.
5
+
6
+ export const toolDescription = `Tree-sitter tool — parser-level structure and syntax queries for supported files.
7
+
8
+ Actions: outline, imports, exports, node_at, query, callees.
9
+
10
+ Use tree_sitter for exact syntax nodes, shallow structure, parsed imports/exports, outgoing calls, or custom AST queries within one file. file is required for all actions. line and character are 1-based UTF-16 coordinates for node_at and callees. query is required for query. outline, imports, and exports are JavaScript/TypeScript-only; node_at and query work across supported grammars; callees works for many grammars. Relative paths resolve from the session working directory.`;
11
+
12
+ export const promptGuidelines = [
13
+ "Use tree_sitter.outline(file), tree_sitter.imports(file), or tree_sitter.exports(file) for shallow JavaScript or TypeScript structure without reading the whole file.",
14
+ "Use tree_sitter.node_at(file, line, character) for the exact syntax node and ancestry at a known position.",
15
+ "Use tree_sitter.callees(file, line, character) for outgoing calls from the enclosing function or method at a known position.",
16
+ "Use tree_sitter.query(file, query) for custom Tree-sitter patterns when the built-in actions are not specific enough.",
17
+ "Use tree_sitter for syntax, node types, source ranges, and other parser-backed structure within one supported file.",
18
+ "Do not use tree_sitter for type information, cross-file references, semantic renames, or codebase-wide orientation.",
19
+ ];
20
+
21
+ export const promptSnippet =
22
+ "tree_sitter — parser-backed single-file structure, node lookup, callees, and AST queries";
@@ -1,9 +1,9 @@
1
1
  // Import extraction for supported files.
2
2
 
3
- import { nodeToRange } from "./coordinates.ts";
4
- import type { TreeSitterRuntime } from "./runtime.ts";
5
- import type { SyntaxNodeLike } from "./syntax-node.ts";
6
- import type { ImportRecord, TreeSitterResult } from "./types.ts";
3
+ import { nodeToRange } from "../coordinates.ts";
4
+ import type { TreeSitterRuntime } from "../session/runtime.ts";
5
+ import type { SyntaxNodeLike } from "../syntax-node.ts";
6
+ import type { ImportRecord, TreeSitterResult } from "../types.ts";
7
7
 
8
8
  /** Extract import records from a supported file. */
9
9
  export async function extractImports(
@@ -1,8 +1,8 @@
1
1
  // Node-at-position lookup.
2
2
 
3
- import { nodeToRange, publicToTreeSitter, splitSourceLines } from "./coordinates.ts";
4
- import type { TreeSitterRuntime } from "./runtime.ts";
5
- import type { NodeAtResult, SourceRange, TreeSitterResult } from "./types.ts";
3
+ import { nodeToRange, publicToTreeSitter, splitSourceLines } from "../coordinates.ts";
4
+ import type { TreeSitterRuntime } from "../session/runtime.ts";
5
+ import type { NodeAtResult, SourceRange, TreeSitterResult } from "../types.ts";
6
6
 
7
7
  const MAX_ANCESTRY = 10;
8
8
 
@@ -1,8 +1,8 @@
1
1
  // Outline extraction for supported files.
2
2
 
3
- import { nodeToRange } from "./coordinates.ts";
4
- import type { SyntaxNodeLike } from "./syntax-node.ts";
5
- import type { OutlineItem } from "./types.ts";
3
+ import { nodeToRange } from "../coordinates.ts";
4
+ import type { SyntaxNodeLike } from "../syntax-node.ts";
5
+ import type { OutlineItem } from "../types.ts";
6
6
 
7
7
  /** Node types that can be extracted directly as outline items. */
8
8
  const OUTLINE_DECLARATION_NODE_TYPES = new Set([
@@ -3,6 +3,8 @@
3
3
  import { StringEnum } from "@earendil-works/pi-ai";
4
4
  import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
5
5
  import { Type } from "typebox";
6
+ import { detectGrammar, isJsTsGrammar } from "./language.ts";
7
+ import { TreeSitterRuntime } from "./session/runtime.ts";
6
8
  import {
7
9
  formatNonSuccess,
8
10
  formatOutlineItemsCapped,
@@ -11,11 +13,10 @@ import {
11
13
  truncatedNotice,
12
14
  truncateText,
13
15
  validationError,
14
- } from "./formatting.ts";
15
- import { detectGrammar, isJsTsGrammar } from "./language.ts";
16
- import { collectOutline } from "./outline.ts";
17
- import { TreeSitterRuntime } from "./runtime.ts";
18
- import { extractExports, extractImports, lookupCalleesAt, lookupNodeAt } from "./structure.ts";
16
+ } from "./tool/formatting.ts";
17
+ import { promptGuidelines, promptSnippet, toolDescription } from "./tool/guidance.ts";
18
+ import { collectOutline } from "./tool/outline.ts";
19
+ import { extractExports, extractImports, lookupCalleesAt, lookupNodeAt } from "./tool/structure.ts";
19
20
 
20
21
  const TreeSitterActionEnum = StringEnum([
21
22
  "outline",
@@ -26,30 +27,6 @@ const TreeSitterActionEnum = StringEnum([
26
27
  "callees",
27
28
  ] as const);
28
29
 
29
- const toolDescription = `Tree-sitter tool — provides structural AST analysis for supported files.
30
-
31
- Actions:
32
- - outline: Extract structural declarations (functions, classes, interfaces, etc.). JavaScript/TypeScript only.
33
- - imports: List import statements with module specifiers. JavaScript/TypeScript only.
34
- - exports: List export declarations, re-exports, and export assignments with names and kinds. JavaScript/TypeScript only.
35
- - node_at: Find the syntax node at a position. Params: file, line, character
36
- - query: Run a Tree-sitter query. Params: file, query
37
- - callees: Find outgoing function/method calls from a position. Params: file, line, character. Supported for most grammars.
38
-
39
- Coordinates are 1-based (line, character), compatible with the lsp tool convention.
40
- Character is a UTF-16 code-unit column.
41
- Relative file paths resolve from the session working directory.
42
-
43
- Supported extensions: .ts, .tsx, .js, .jsx, .mts, .cts, .mjs, .cjs, .py, .pyi, .rs, .go, .mod, .c, .h, .cpp, .hpp, .cc, .cxx, .hxx, .c++, .h++, .java, .kt, .kts, .rb, .sh, .bash, .zsh, .html, .htm, .xhtml, .r, .sql`;
44
-
45
- const promptGuidelines = [
46
- "Use tree_sitter for structural syntax-tree analysis: extracting declarations, imports, exports, node-at-position lookup, and custom queries.",
47
- "Prefer tree_sitter when you need AST node types, exact source ranges, or parser-level structure that semantic language-server tooling does not expose.",
48
- "tree_sitter is a standalone structural analysis tool; use semantic language-server features separately when they are available for hover, definitions, references, or diagnostics.",
49
- ];
50
-
51
- const promptSnippet = `Use the tree_sitter tool for structural code analysis — outline, imports, exports, node-at-position lookup, and custom queries.`;
52
-
53
30
  export default function treeSitterExtension(pi: ExtensionAPI) {
54
31
  let runtime: TreeSitterRuntime | undefined;
55
32
 
File without changes