@mrclrchtr/supi-tree-sitter 1.3.1 → 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 +46 -39
- package/package.json +1 -1
- package/src/api.ts +1 -1
- package/src/index.ts +1 -1
- package/src/{runtime.ts → session/runtime.ts} +3 -3
- package/src/{session.ts → session/session.ts} +4 -4
- package/src/{callees.ts → tool/callees.ts} +3 -3
- package/src/{exports.ts → tool/exports.ts} +4 -4
- package/src/{formatting.ts → tool/formatting.ts} +1 -1
- package/src/tool/guidance.ts +22 -0
- package/src/{imports.ts → tool/imports.ts} +4 -4
- package/src/{node-at.ts → tool/node-at.ts} +3 -3
- package/src/{outline.ts → tool/outline.ts} +3 -3
- package/src/tree-sitter.ts +6 -29
- /package/src/{structure.ts → tool/structure.ts} +0 -0
package/README.md
CHANGED
|
@@ -1,72 +1,79 @@
|
|
|
1
1
|
# @mrclrchtr/supi-tree-sitter
|
|
2
2
|
|
|
3
|
-
|
|
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
|
-
|
|
6
|
-
|
|
7
|
-
## What you get
|
|
5
|
+
## Install
|
|
8
6
|
|
|
9
|
-
|
|
7
|
+
```bash
|
|
8
|
+
pi install npm:@mrclrchtr/supi-tree-sitter
|
|
9
|
+
```
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
For local development:
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
```bash
|
|
14
|
+
pi install ./packages/supi-tree-sitter
|
|
15
|
+
```
|
|
14
16
|
|
|
15
|
-
|
|
17
|
+
After editing the source, run `/reload`.
|
|
16
18
|
|
|
17
|
-
|
|
19
|
+
## What you get
|
|
18
20
|
|
|
19
|
-
|
|
21
|
+
After install, pi gets one tool:
|
|
20
22
|
|
|
21
|
-
|
|
23
|
+
- `tree_sitter` — inspect code structure through Tree-sitter parsers instead of plain text search
|
|
22
24
|
|
|
23
|
-
##
|
|
25
|
+
## `tree_sitter` actions
|
|
24
26
|
|
|
25
|
-
|
|
26
|
-
|
|
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
|
-
|
|
36
|
+
Coordinates use **1-based** line and character columns. Character positions use UTF-16 code units.
|
|
30
37
|
|
|
31
|
-
|
|
38
|
+
## Supported file families
|
|
32
39
|
|
|
33
|
-
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
package/src/api.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -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 "
|
|
7
|
-
import { detectGrammar, resolveGrammarWasmPath } from "
|
|
8
|
-
import type { GrammarId, QueryCapture, TreeSitterResult } from "
|
|
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 "
|
|
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 "
|
|
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 "
|
|
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 "
|
|
4
|
-
import type { TreeSitterRuntime } from "
|
|
5
|
-
import type { GrammarId, SourceRange, TreeSitterResult } from "
|
|
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 "
|
|
4
|
-
import type { TreeSitterRuntime } from "
|
|
5
|
-
import type { SyntaxNodeLike } from "
|
|
6
|
-
import type { ExportRecord, TreeSitterResult } from "
|
|
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(
|
|
@@ -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 "
|
|
4
|
-
import type { TreeSitterRuntime } from "
|
|
5
|
-
import type { SyntaxNodeLike } from "
|
|
6
|
-
import type { ImportRecord, TreeSitterResult } from "
|
|
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 "
|
|
4
|
-
import type { TreeSitterRuntime } from "
|
|
5
|
-
import type { NodeAtResult, SourceRange, TreeSitterResult } from "
|
|
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 "
|
|
4
|
-
import type { SyntaxNodeLike } from "
|
|
5
|
-
import type { OutlineItem } from "
|
|
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([
|
package/src/tree-sitter.ts
CHANGED
|
@@ -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 {
|
|
16
|
-
import { collectOutline } from "./outline.ts";
|
|
17
|
-
import {
|
|
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
|