@macroforge/mcp-server 0.1.40 → 0.1.42
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/docs/api/api-overview.md +32 -32
- package/docs/api/expand-sync.md +30 -30
- package/docs/api/native-plugin.md +38 -38
- package/docs/api/position-mapper.md +18 -18
- package/docs/api/transform-sync.md +31 -31
- package/docs/builtin-macros/macros-overview.md +84 -84
- package/docs/builtin-macros/partial-ord.md +18 -20
- package/docs/concepts/architecture.md +16 -16
- package/docs/concepts/derive-system.md +89 -68
- package/docs/concepts/how-macros-work.md +13 -12
- package/docs/custom-macros/custom-overview.md +36 -36
- package/docs/custom-macros/rust-setup.md +71 -71
- package/docs/custom-macros/ts-macro-derive.md +167 -167
- package/docs/custom-macros/ts-quote.md +347 -347
- package/docs/getting-started/first-macro.md +57 -55
- package/docs/getting-started/installation.md +34 -35
- package/docs/integration/cli.md +43 -43
- package/docs/integration/configuration.md +41 -41
- package/docs/integration/integration-overview.md +4 -4
- package/docs/integration/mcp-server.md +22 -22
- package/docs/integration/svelte-preprocessor.md +87 -87
- package/docs/integration/typescript-plugin.md +23 -24
- package/docs/integration/vite-plugin.md +40 -40
- package/docs/language-servers/svelte.md +15 -16
- package/docs/language-servers/zed.md +14 -15
- package/package.json +2 -2
package/docs/api/api-overview.md
CHANGED
|
@@ -1,53 +1,53 @@
|
|
|
1
1
|
# API Reference
|
|
2
|
-
<span class="stats svelte-1c8t0id">
|
|
2
|
+
<span class="stats svelte-1c8t0id">55 exported items *Macroforge provides a programmatic API for expanding macros in TypeScript code.*
|
|
3
3
|
## Overview
|
|
4
4
|
```
|
|
5
|
-
import
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
5
|
+
import {
|
|
6
|
+
expandSync,
|
|
7
|
+
transformSync,
|
|
8
|
+
checkSyntax,
|
|
9
|
+
parseImportSources,
|
|
10
|
+
NativePlugin,
|
|
11
|
+
PositionMapper
|
|
12
|
+
} from "macroforge";
|
|
13
13
|
``` ## Core Functions
|
|
14
14
|
| Function | Description |
|
|
15
15
|
| --- | --- |
|
|
16
|
-
| [
|
|
17
|
-
| [
|
|
18
|
-
|
|
|
19
|
-
|
|
|
16
|
+
| [expandSync()](../docs/api/expand-sync) | Expand macros synchronously |
|
|
17
|
+
| [transformSync()](../docs/api/transform-sync) | Transform code with additional metadata |
|
|
18
|
+
| checkSyntax() | Validate TypeScript syntax |
|
|
19
|
+
| parseImportSources() | Extract import information |
|
|
20
20
|
## Classes
|
|
21
21
|
| Class | Description |
|
|
22
22
|
| --- | --- |
|
|
23
|
-
| [
|
|
24
|
-
| [
|
|
23
|
+
| [NativePlugin](../docs/api/native-plugin) | Stateful plugin with caching |
|
|
24
|
+
| [PositionMapper](../docs/api/position-mapper) | Maps positions between original and expanded code |
|
|
25
25
|
## Quick Example
|
|
26
26
|
```
|
|
27
|
-
import
|
|
27
|
+
import { expandSync } from "macroforge";
|
|
28
28
|
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
class
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
29
|
+
const sourceCode = \`
|
|
30
|
+
/** @derive(Debug) */
|
|
31
|
+
class User {
|
|
32
|
+
name: string;
|
|
33
|
+
constructor(name: string) {
|
|
34
|
+
this.name = name;
|
|
35
|
+
}
|
|
36
36
|
}
|
|
37
37
|
\`;
|
|
38
38
|
|
|
39
|
-
const
|
|
40
|
-
|
|
39
|
+
const result = expandSync(sourceCode, "user.ts", {
|
|
40
|
+
keepDecorators: false
|
|
41
41
|
});
|
|
42
42
|
|
|
43
43
|
console.log(result.code);
|
|
44
|
-
|
|
44
|
+
// Output: class with toString() method generated
|
|
45
45
|
|
|
46
|
-
if
|
|
47
|
-
|
|
46
|
+
if (result.diagnostics.length > 0) {
|
|
47
|
+
console.error("Errors:", result.diagnostics);
|
|
48
48
|
}
|
|
49
49
|
``` ## Detailed Reference
|
|
50
|
-
- [
|
|
51
|
-
- [
|
|
52
|
-
- [
|
|
53
|
-
- [
|
|
50
|
+
- [<code class="shiki-inline"><span class="line"><span style="--shiki-dark:#B392F0;--shiki-light:#6F42C1">expandSync<span style="--shiki-dark:#E1E4E8;--shiki-light:#24292E">()</code>](../docs/api/expand-sync) - Full options and return types
|
|
51
|
+
- [<code class="shiki-inline"><span class="line"><span style="--shiki-dark:#B392F0;--shiki-light:#6F42C1">transformSync<span style="--shiki-dark:#E1E4E8;--shiki-light:#24292E">()</code>](../docs/api/transform-sync) - Transform with source maps
|
|
52
|
+
- [<code class="shiki-inline"><span class="line"><span style="--shiki-dark:#E1E4E8;--shiki-light:#24292E">NativePlugin</code>](../docs/api/native-plugin) - Caching for language servers
|
|
53
|
+
- [<code class="shiki-inline"><span class="line"><span style="--shiki-dark:#E1E4E8;--shiki-light:#24292E">PositionMapper</code>](../docs/api/position-mapper) - Position mapping utilities
|
package/docs/api/expand-sync.md
CHANGED
|
@@ -2,50 +2,50 @@
|
|
|
2
2
|
*Synchronously expands macros in TypeScript code. This is the standalone macro expansion function that doesn't use caching. For cached expansion, use [`NativePlugin::process_file`] instead.*
|
|
3
3
|
## Signature
|
|
4
4
|
```
|
|
5
|
-
function
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
)
|
|
5
|
+
function expandSync(
|
|
6
|
+
code: string,
|
|
7
|
+
filepath: string,
|
|
8
|
+
options?: ExpandOptions
|
|
9
|
+
): ExpandResult
|
|
10
10
|
``` ## Parameters
|
|
11
11
|
| Parameter | Type | Description |
|
|
12
12
|
| --- | --- | --- |
|
|
13
|
-
|
|
|
14
|
-
|
|
|
15
|
-
|
|
|
13
|
+
| code | string | TypeScript source code to transform |
|
|
14
|
+
| filepath | string | File path (used for error reporting) |
|
|
15
|
+
| options | ExpandOptions | Optional configuration |
|
|
16
16
|
## ExpandOptions
|
|
17
17
|
```
|
|
18
|
-
interface
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
interface ExpandOptions {
|
|
19
|
+
// Keep @derive decorators in output (default: false)
|
|
20
|
+
keepDecorators?: boolean;
|
|
21
21
|
}
|
|
22
22
|
``` ## ExpandResult
|
|
23
23
|
```
|
|
24
|
-
interface
|
|
25
|
-
|
|
26
|
-
|
|
24
|
+
interface ExpandResult {
|
|
25
|
+
// Transformed TypeScript code
|
|
26
|
+
code: string;
|
|
27
27
|
|
|
28
|
-
|
|
29
|
-
|
|
28
|
+
// Generated type declarations (.d.ts content)
|
|
29
|
+
types?: string;
|
|
30
30
|
|
|
31
|
-
|
|
32
|
-
|
|
31
|
+
// Macro expansion metadata (JSON string)
|
|
32
|
+
metadata?: string;
|
|
33
33
|
|
|
34
|
-
|
|
35
|
-
|
|
34
|
+
// Warnings and errors from macro expansion
|
|
35
|
+
diagnostics: MacroDiagnostic[];
|
|
36
36
|
|
|
37
|
-
|
|
38
|
-
|
|
37
|
+
// Position mapping data for source maps
|
|
38
|
+
sourceMapping?: SourceMappingResult;
|
|
39
39
|
}
|
|
40
40
|
``` ## MacroDiagnostic
|
|
41
41
|
```
|
|
42
|
-
interface
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
42
|
+
interface MacroDiagnostic {
|
|
43
|
+
message: string;
|
|
44
|
+
severity: "error" | "warning" | "info";
|
|
45
|
+
span: {
|
|
46
|
+
start: number;
|
|
47
|
+
end: number;
|
|
48
|
+
};
|
|
49
49
|
}
|
|
50
50
|
``` ## Example
|
|
51
51
|
```
|
|
@@ -80,7 +80,7 @@ if (result.diagnostics.length > 0) {
|
|
|
80
80
|
}
|
|
81
81
|
}
|
|
82
82
|
``` ## Error Handling
|
|
83
|
-
Syntax errors and macro errors are returned in the
|
|
83
|
+
Syntax errors and macro errors are returned in the <code class="shiki-inline"><span class="line"><span style="--shiki-dark:#E1E4E8;--shiki-light:#24292E">diagnostics</code> array, not thrown as exceptions:
|
|
84
84
|
```
|
|
85
85
|
const result = expandSync(invalidCode, "file.ts");
|
|
86
86
|
|
|
@@ -2,73 +2,73 @@
|
|
|
2
2
|
*The main plugin class for macro expansion with caching support. `NativePlugin` is designed to be instantiated once and reused across multiple file processing operations. It maintains a cache of expansion results keyed by filepath and version, enabling efficient incremental processing.*
|
|
3
3
|
## Constructor
|
|
4
4
|
```
|
|
5
|
-
const
|
|
5
|
+
const plugin = new NativePlugin();
|
|
6
6
|
``` ## Methods
|
|
7
7
|
### processFile()
|
|
8
8
|
Process a file with version-based caching:
|
|
9
9
|
```
|
|
10
10
|
processFile(
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
)
|
|
11
|
+
filepath: string,
|
|
12
|
+
code: string,
|
|
13
|
+
options?: ProcessFileOptions
|
|
14
|
+
): ExpandResult
|
|
15
15
|
``` ```
|
|
16
|
-
interface
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
interface ProcessFileOptions {
|
|
17
|
+
// Cache key - if unchanged, returns cached result
|
|
18
|
+
version?: string;
|
|
19
19
|
}
|
|
20
20
|
``` ### getMapper()
|
|
21
21
|
Get the position mapper for a previously processed file:
|
|
22
22
|
```
|
|
23
|
-
getMapper(filepath
|
|
23
|
+
getMapper(filepath: string): NativeMapper | null
|
|
24
24
|
``` ### mapDiagnostics()
|
|
25
25
|
Map diagnostics from expanded positions to original positions:
|
|
26
26
|
```
|
|
27
27
|
mapDiagnostics(
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
)
|
|
28
|
+
filepath: string,
|
|
29
|
+
diagnostics: JsDiagnostic[]
|
|
30
|
+
): JsDiagnostic[]
|
|
31
31
|
``` ### log() / setLogFile()
|
|
32
32
|
Logging utilities for debugging:
|
|
33
33
|
```
|
|
34
|
-
log(message
|
|
35
|
-
setLogFile(path
|
|
34
|
+
log(message: string): void
|
|
35
|
+
setLogFile(path: string): void
|
|
36
36
|
``` ## Caching Behavior
|
|
37
37
|
The plugin caches expansion results by file path and version:
|
|
38
38
|
```
|
|
39
|
-
const
|
|
39
|
+
const plugin = new NativePlugin();
|
|
40
40
|
|
|
41
|
-
|
|
42
|
-
const
|
|
41
|
+
// First call - performs expansion
|
|
42
|
+
const result1 = plugin.processFile("user.ts", code, { version: "1" });
|
|
43
43
|
|
|
44
|
-
|
|
45
|
-
const
|
|
44
|
+
// Same version - returns cached result instantly
|
|
45
|
+
const result2 = plugin.processFile("user.ts", code, { version: "1" });
|
|
46
46
|
|
|
47
|
-
|
|
48
|
-
const
|
|
47
|
+
// Different version - re-expands
|
|
48
|
+
const result3 = plugin.processFile("user.ts", newCode, { version: "2" });
|
|
49
49
|
``` ## Example: Language Server Integration
|
|
50
50
|
```
|
|
51
|
-
import
|
|
51
|
+
import { NativePlugin } from "macroforge";
|
|
52
52
|
|
|
53
|
-
class
|
|
54
|
-
|
|
53
|
+
class MacroforgeLanguageService {
|
|
54
|
+
private plugin = new NativePlugin();
|
|
55
55
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
56
|
+
processDocument(uri: string, content: string, version: number) {
|
|
57
|
+
// Process with version-based caching
|
|
58
|
+
const result = this.plugin.processFile(uri, content, {
|
|
59
|
+
version: String(version)
|
|
60
|
+
});
|
|
61
61
|
|
|
62
|
-
|
|
63
|
-
|
|
62
|
+
// Get mapper for position translation
|
|
63
|
+
const mapper = this.plugin.getMapper(uri);
|
|
64
64
|
|
|
65
|
-
|
|
66
|
-
|
|
65
|
+
return { result, mapper };
|
|
66
|
+
}
|
|
67
67
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
68
|
+
getSemanticDiagnostics(uri: string, diagnostics: Diagnostic[]) {
|
|
69
|
+
// Map positions from expanded to original
|
|
70
|
+
return this.plugin.mapDiagnostics(uri, diagnostics);
|
|
71
|
+
}
|
|
72
72
|
}
|
|
73
73
|
``` ## Thread Safety
|
|
74
|
-
The
|
|
74
|
+
The <code class="shiki-inline"><span class="line"><span style="--shiki-dark:#E1E4E8;--shiki-light:#24292E">NativePlugin</code> class is thread-safe and can be used from multiple async contexts. Each file is processed in an isolated thread with its own stack space.
|
|
@@ -2,51 +2,51 @@
|
|
|
2
2
|
*Bidirectional position mapper for translating between original and expanded source positions. This mapper enables IDE features like error reporting, go-to-definition, and hover to work correctly with macro-expanded code by translating positions between the original source (what the user wrote) and the expanded source (what the compiler sees).*
|
|
3
3
|
## Getting a Mapper
|
|
4
4
|
```
|
|
5
|
-
import
|
|
5
|
+
import { NativePlugin, PositionMapper } from "macroforge";
|
|
6
6
|
|
|
7
|
-
const
|
|
8
|
-
const
|
|
7
|
+
const plugin = new NativePlugin();
|
|
8
|
+
const result = plugin.processFile("user.ts", code, { version: "1" });
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
const
|
|
12
|
-
if
|
|
13
|
-
|
|
10
|
+
// Get the mapper for this file
|
|
11
|
+
const mapper = plugin.getMapper("user.ts");
|
|
12
|
+
if (mapper) {
|
|
13
|
+
// Use the mapper...
|
|
14
14
|
}
|
|
15
15
|
``` ## Methods
|
|
16
16
|
### isEmpty()
|
|
17
17
|
Check if the mapper has any mappings:
|
|
18
18
|
```
|
|
19
|
-
isEmpty()
|
|
19
|
+
isEmpty(): boolean
|
|
20
20
|
``` ### originalToExpanded()
|
|
21
21
|
Map a position from original to expanded code:
|
|
22
22
|
```
|
|
23
|
-
originalToExpanded(pos
|
|
23
|
+
originalToExpanded(pos: number): number
|
|
24
24
|
``` ### expandedToOriginal()
|
|
25
25
|
Map a position from expanded to original code:
|
|
26
26
|
```
|
|
27
|
-
expandedToOriginal(pos
|
|
28
|
-
``` Returns
|
|
27
|
+
expandedToOriginal(pos: number): number | null
|
|
28
|
+
``` Returns <code class="shiki-inline"><span class="line"><span style="--shiki-dark:#79B8FF;--shiki-light:#005CC5">null</code> if the position is in generated code.
|
|
29
29
|
### isInGenerated()
|
|
30
30
|
Check if a position is in macro-generated code:
|
|
31
31
|
```
|
|
32
|
-
isInGenerated(pos
|
|
32
|
+
isInGenerated(pos: number): boolean
|
|
33
33
|
``` ### generatedBy()
|
|
34
34
|
Get the name of the macro that generated code at a position:
|
|
35
35
|
```
|
|
36
|
-
generatedBy(pos
|
|
36
|
+
generatedBy(pos: number): string | null
|
|
37
37
|
``` ### mapSpanToOriginal()
|
|
38
38
|
Map a span (range) from expanded to original code:
|
|
39
39
|
```
|
|
40
|
-
mapSpanToOriginal(start
|
|
40
|
+
mapSpanToOriginal(start: number, length: number): SpanResult | null
|
|
41
41
|
|
|
42
|
-
interface
|
|
43
|
-
|
|
44
|
-
|
|
42
|
+
interface SpanResult {
|
|
43
|
+
start: number;
|
|
44
|
+
length: number;
|
|
45
45
|
}
|
|
46
46
|
``` ### mapSpanToExpanded()
|
|
47
47
|
Map a span from original to expanded code:
|
|
48
48
|
```
|
|
49
|
-
mapSpanToExpanded(start
|
|
49
|
+
mapSpanToExpanded(start: number, length: number): SpanResult
|
|
50
50
|
``` ## Example: Error Position Mapping
|
|
51
51
|
```
|
|
52
52
|
import { NativePlugin } from "macroforge";
|
|
@@ -2,32 +2,32 @@
|
|
|
2
2
|
*Synchronously transforms TypeScript code through the macro expansion system. This is similar to [`expand_sync`] but returns a [`TransformResult`] which includes source map information (when available).*
|
|
3
3
|
## Signature
|
|
4
4
|
```
|
|
5
|
-
function
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
)
|
|
5
|
+
function transformSync(
|
|
6
|
+
code: string,
|
|
7
|
+
filepath: string
|
|
8
|
+
): TransformResult
|
|
9
9
|
``` ## Parameters
|
|
10
10
|
| Parameter | Type | Description |
|
|
11
11
|
| --- | --- | --- |
|
|
12
|
-
|
|
|
13
|
-
|
|
|
12
|
+
| code | string | TypeScript source code to transform |
|
|
13
|
+
| filepath | string | File path (used for error reporting) |
|
|
14
14
|
## TransformResult
|
|
15
15
|
```
|
|
16
|
-
interface
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
interface TransformResult {
|
|
17
|
+
// Transformed TypeScript code
|
|
18
|
+
code: string;
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
// Source map (JSON string, not yet implemented)
|
|
21
|
+
map?: string;
|
|
22
22
|
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
// Generated type declarations
|
|
24
|
+
types?: string;
|
|
25
25
|
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
// Macro expansion metadata
|
|
27
|
+
metadata?: string;
|
|
28
28
|
}
|
|
29
29
|
``` ## Comparison with expandSync()
|
|
30
|
-
| Feature |
|
|
30
|
+
| Feature | expandSync | transformSync |
|
|
31
31
|
| --- | --- | --- |
|
|
32
32
|
| Options | Yes | No |
|
|
33
33
|
| Diagnostics | Yes | No |
|
|
@@ -35,32 +35,32 @@ interface TransformResult {
|
|
|
35
35
|
| Use Case | General purpose | Build tools |
|
|
36
36
|
## Example
|
|
37
37
|
```
|
|
38
|
-
import
|
|
38
|
+
import { transformSync } from "macroforge";
|
|
39
39
|
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
class
|
|
43
|
-
|
|
40
|
+
const sourceCode = \`
|
|
41
|
+
/** @derive(Debug) */
|
|
42
|
+
class User {
|
|
43
|
+
name: string;
|
|
44
44
|
}
|
|
45
45
|
\`;
|
|
46
46
|
|
|
47
|
-
const
|
|
47
|
+
const result = transformSync(sourceCode, "user.ts");
|
|
48
48
|
|
|
49
49
|
console.log(result.code);
|
|
50
50
|
|
|
51
|
-
if
|
|
52
|
-
|
|
53
|
-
|
|
51
|
+
if (result.types) {
|
|
52
|
+
// Write to .d.ts file
|
|
53
|
+
fs.writeFileSync("user.d.ts", result.types);
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
-
if
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
56
|
+
if (result.metadata) {
|
|
57
|
+
// Parse and use metadata
|
|
58
|
+
const meta = JSON.parse(result.metadata);
|
|
59
|
+
console.log("Macros expanded:", meta);
|
|
60
60
|
}
|
|
61
61
|
``` ## When to Use
|
|
62
|
-
Use
|
|
62
|
+
Use <code class="shiki-inline"><span class="line"><span style="--shiki-dark:#E1E4E8;--shiki-light:#24292E">transformSync</code> when:
|
|
63
63
|
- Building custom integrations
|
|
64
64
|
- You need raw output without diagnostics
|
|
65
65
|
- You're implementing a build tool plugin
|
|
66
|
-
Use
|
|
66
|
+
Use <code class="shiki-inline"><span class="line"><span style="--shiki-dark:#E1E4E8;--shiki-light:#24292E">expandSync</code> for most other use cases, as it provides better error handling.
|