@mdsvex/language-server 0.0.0 → 0.1.0-next.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/CHANGELOG.md +12 -0
- package/LICENSE +21 -0
- package/bin/pfm-language-server.js +3 -0
- package/dist/server.cjs +149070 -0
- package/package.json +36 -7
- package/src/clean_hover.ts +97 -0
- package/src/index.ts +186 -0
- package/test/clean_hover.spec.ts +113 -0
- package/test/integration.spec.ts +222 -0
- package/tsconfig.json +11 -0
- package/vite.config.build.ts +26 -0
- package/README.md +0 -3
package/package.json
CHANGED
|
@@ -1,15 +1,44 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mdsvex/language-server",
|
|
3
|
-
"version": "0.0.0",
|
|
4
|
-
"description": "Placeholder release so npm trusted publishing can be configured.",
|
|
5
|
-
"license": "MIT",
|
|
6
|
-
"homepage": "https://mdsvex.pngwn.io",
|
|
3
|
+
"version": "0.1.0-next.0",
|
|
7
4
|
"repository": {
|
|
8
5
|
"type": "git",
|
|
9
|
-
"url": "https://github.com/pngwn/MDsveX",
|
|
6
|
+
"url": "git+https://github.com/pngwn/MDsveX.git",
|
|
10
7
|
"directory": "language-tools/language-server"
|
|
11
8
|
},
|
|
12
|
-
"
|
|
13
|
-
|
|
9
|
+
"description": "PFM Language Server — LSP binary for .pfm files",
|
|
10
|
+
"type": "module",
|
|
11
|
+
"bin": {
|
|
12
|
+
"pfm-language-server": "./bin/pfm-language-server.js"
|
|
13
|
+
},
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"import": "./src/index.ts",
|
|
17
|
+
"types": "./src/index.ts"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"@volar/language-core": "^2.4.28",
|
|
22
|
+
"@volar/language-server": "^2.4.28",
|
|
23
|
+
"@volar/typescript": "^2.4.28",
|
|
24
|
+
"volar-service-css": "^0.0.70",
|
|
25
|
+
"volar-service-markdown": "^0.0.70",
|
|
26
|
+
"svelte2tsx": "^0.7.53",
|
|
27
|
+
"volar-service-typescript": "^0.0.70",
|
|
28
|
+
"vscode-languageserver": "^9.0.1",
|
|
29
|
+
"vscode-uri": "^3.0.8",
|
|
30
|
+
"@mdsvex/language-core": "0.1.0-next.0"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@volar/test-utils": "^2.4.28",
|
|
34
|
+
"svelte": "^5.0.0",
|
|
35
|
+
"typescript": "^5.0.0"
|
|
36
|
+
},
|
|
37
|
+
"author": "pngwn <hello@pngwn.io>",
|
|
38
|
+
"license": "MIT",
|
|
39
|
+
"scripts": {
|
|
40
|
+
"test": "vitest --disableConsoleIntercept",
|
|
41
|
+
"test:run": "vitest run --silent",
|
|
42
|
+
"build": "vite build --config vite.config.build.ts"
|
|
14
43
|
}
|
|
15
44
|
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Clean up svelte2tsx internal types in hover tooltips.
|
|
3
|
+
*
|
|
4
|
+
* 1. Strips `(alias) type X = SvelteComponent<...> & { $$bindings?: ... }\n`
|
|
5
|
+
* 2. Transforms `const X: __sveltets_2_IsomorphicComponent<{props}, ...>` into `const X<{props}>`
|
|
6
|
+
*/
|
|
7
|
+
export function clean_svelte_hover(markdown: string): string {
|
|
8
|
+
markdown = strip_svelte_component_type(markdown);
|
|
9
|
+
markdown = clean_isomorphic_component(markdown);
|
|
10
|
+
return markdown;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** Remove `(alias) type X = SvelteComponent<...> & { ... }\n` lines. */
|
|
14
|
+
function strip_svelte_component_type(md: string): string {
|
|
15
|
+
const marker = "type ";
|
|
16
|
+
let search_from = 0;
|
|
17
|
+
while (true) {
|
|
18
|
+
const type_idx = md.indexOf(marker, search_from);
|
|
19
|
+
if (type_idx === -1) return md;
|
|
20
|
+
|
|
21
|
+
// Check: does this type declaration use SvelteComponent?
|
|
22
|
+
const eq_idx = md.indexOf("= SvelteComponent<", type_idx);
|
|
23
|
+
if (eq_idx === -1 || eq_idx - type_idx > 80) {
|
|
24
|
+
search_from = type_idx + 1;
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Find the start of this declaration line (scan back to "(alias) " or newline)
|
|
29
|
+
let line_start = type_idx;
|
|
30
|
+
while (line_start > 0 && md[line_start - 1] !== "\n") line_start--;
|
|
31
|
+
|
|
32
|
+
// Find the end: track braces past the `& { $$bindings... }` block
|
|
33
|
+
let i = eq_idx + "= SvelteComponent<".length;
|
|
34
|
+
let angle_depth = 1;
|
|
35
|
+
// First pass: find closing > of SvelteComponent<...>
|
|
36
|
+
while (i < md.length && angle_depth > 0) {
|
|
37
|
+
if (md[i] === "<") angle_depth++;
|
|
38
|
+
else if (md[i] === ">" && md[i - 1] !== "=") angle_depth--;
|
|
39
|
+
i++;
|
|
40
|
+
}
|
|
41
|
+
// Check for `& { ... }` after the closing >
|
|
42
|
+
const after_angle = md.slice(i, i + 10).trimStart();
|
|
43
|
+
if (after_angle.startsWith("& {")) {
|
|
44
|
+
i = md.indexOf("& {", i) + 2;
|
|
45
|
+
let brace_depth = 0;
|
|
46
|
+
while (i < md.length) {
|
|
47
|
+
if (md[i] === "{") brace_depth++;
|
|
48
|
+
else if (md[i] === "}") {
|
|
49
|
+
brace_depth--;
|
|
50
|
+
if (brace_depth === 0) { i++; break; }
|
|
51
|
+
}
|
|
52
|
+
i++;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
// Skip trailing newline
|
|
56
|
+
if (i < md.length && md[i] === "\n") i++;
|
|
57
|
+
|
|
58
|
+
md = md.slice(0, line_start) + md.slice(i);
|
|
59
|
+
search_from = line_start;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Transform `: __sveltets_2_IsomorphicComponent<{props}, ...>` into `<{props}>`. */
|
|
64
|
+
function clean_isomorphic_component(md: string): string {
|
|
65
|
+
const marker = ": __sveltets_2_IsomorphicComponent<";
|
|
66
|
+
const idx = md.indexOf(marker);
|
|
67
|
+
if (idx === -1) return md;
|
|
68
|
+
|
|
69
|
+
const args_start = idx + marker.length;
|
|
70
|
+
|
|
71
|
+
// Find end of first generic arg (props), first comma at brace depth 0
|
|
72
|
+
let depth = 0;
|
|
73
|
+
let first_arg_end = -1;
|
|
74
|
+
for (let i = args_start; i < md.length; i++) {
|
|
75
|
+
const ch = md[i];
|
|
76
|
+
if (ch === "{") depth++;
|
|
77
|
+
else if (ch === "}") depth--;
|
|
78
|
+
else if (ch === "," && depth === 0) { first_arg_end = i; break; }
|
|
79
|
+
}
|
|
80
|
+
if (first_arg_end === -1) return md;
|
|
81
|
+
|
|
82
|
+
// Find closing > (skip => arrow functions)
|
|
83
|
+
let angle_depth = 1;
|
|
84
|
+
let full_end = -1;
|
|
85
|
+
for (let i = args_start; i < md.length; i++) {
|
|
86
|
+
if (md[i] === "<") angle_depth++;
|
|
87
|
+
else if (md[i] === ">") {
|
|
88
|
+
if (i > 0 && md[i - 1] === "=") continue;
|
|
89
|
+
angle_depth--;
|
|
90
|
+
if (angle_depth === 0) { full_end = i + 1; break; }
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if (full_end === -1) return md;
|
|
94
|
+
|
|
95
|
+
const first_arg = md.slice(args_start, first_arg_end).trim();
|
|
96
|
+
return md.slice(0, idx) + "<" + first_arg + ">" + md.slice(full_end);
|
|
97
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PFM Language Server.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
createConnection,
|
|
7
|
+
createServer,
|
|
8
|
+
createTypeScriptProject,
|
|
9
|
+
loadTsdkByPath,
|
|
10
|
+
} from "@volar/language-server/node";
|
|
11
|
+
import { create as createTypeScriptServices } from "volar-service-typescript";
|
|
12
|
+
import { create as createCssService } from "volar-service-css";
|
|
13
|
+
import { create as createMarkdownService } from "volar-service-markdown";
|
|
14
|
+
import { create_pfm_language_plugin, create_svelte_language_plugin } from "@mdsvex/language-core";
|
|
15
|
+
import { clean_svelte_hover } from "./clean_hover";
|
|
16
|
+
import { URI } from "vscode-uri";
|
|
17
|
+
import { forEachEmbeddedCode } from "@volar/language-core";
|
|
18
|
+
import type { LanguagePlugin, VirtualCode } from "@volar/language-core";
|
|
19
|
+
import type { TypeScriptExtraServiceScript } from "@volar/typescript";
|
|
20
|
+
import type * as ts from "typescript";
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Create the PFM LanguagePlugin for Volar's server (URI-based),
|
|
24
|
+
* with the critical `typescript` property that registers .pfm
|
|
25
|
+
* as a TypeScript-processable extension.
|
|
26
|
+
*/
|
|
27
|
+
function createPfmLanguagePluginForServer(): LanguagePlugin<URI> {
|
|
28
|
+
const inner = create_pfm_language_plugin();
|
|
29
|
+
|
|
30
|
+
return {
|
|
31
|
+
getLanguageId(scriptId: URI): string | undefined {
|
|
32
|
+
if (scriptId.path.endsWith(".pfm")) {
|
|
33
|
+
return "pfm";
|
|
34
|
+
}
|
|
35
|
+
return undefined;
|
|
36
|
+
},
|
|
37
|
+
|
|
38
|
+
createVirtualCode(scriptId, languageId, snapshot) {
|
|
39
|
+
if (languageId !== "pfm") return undefined;
|
|
40
|
+
try {
|
|
41
|
+
const result = inner.createVirtualCode!(
|
|
42
|
+
scriptId.fsPath,
|
|
43
|
+
languageId,
|
|
44
|
+
snapshot,
|
|
45
|
+
{ getAssociatedScript: () => undefined },
|
|
46
|
+
);
|
|
47
|
+
return result as VirtualCode | undefined;
|
|
48
|
+
} catch (e: any) {
|
|
49
|
+
console.error("[PFM] createVirtualCode error:", e.message);
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
|
|
54
|
+
updateVirtualCode(scriptId, virtualCode, newSnapshot) {
|
|
55
|
+
try {
|
|
56
|
+
const result = inner.updateVirtualCode!(
|
|
57
|
+
scriptId.fsPath,
|
|
58
|
+
virtualCode as any,
|
|
59
|
+
newSnapshot,
|
|
60
|
+
{ getAssociatedScript: () => undefined },
|
|
61
|
+
);
|
|
62
|
+
return result as VirtualCode | undefined;
|
|
63
|
+
} catch (e: any) {
|
|
64
|
+
console.error("[PFM] updateVirtualCode error:", e.message);
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
|
|
69
|
+
// This is the critical piece: tells TypeScript about .pfm files
|
|
70
|
+
typescript: {
|
|
71
|
+
extraFileExtensions: [
|
|
72
|
+
{
|
|
73
|
+
extension: "pfm",
|
|
74
|
+
isMixedContent: true,
|
|
75
|
+
scriptKind: 7 satisfies ts.ScriptKind.Deferred,
|
|
76
|
+
},
|
|
77
|
+
],
|
|
78
|
+
getServiceScript(root: VirtualCode) {
|
|
79
|
+
const tsCode = root.embeddedCodes?.find(
|
|
80
|
+
(c) => c.languageId === "typescript",
|
|
81
|
+
);
|
|
82
|
+
if (tsCode) {
|
|
83
|
+
return {
|
|
84
|
+
code: tsCode,
|
|
85
|
+
extension: ".ts" as any,
|
|
86
|
+
scriptKind: 3 satisfies ts.ScriptKind.TS,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
return undefined;
|
|
90
|
+
},
|
|
91
|
+
getExtraServiceScripts() {
|
|
92
|
+
return [];
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function createSvelteLanguagePluginForServer(): LanguagePlugin<URI> {
|
|
99
|
+
const inner = create_svelte_language_plugin();
|
|
100
|
+
|
|
101
|
+
return {
|
|
102
|
+
getLanguageId(scriptId: URI): string | undefined {
|
|
103
|
+
if (scriptId.path.endsWith(".svelte")) {
|
|
104
|
+
return "svelte";
|
|
105
|
+
}
|
|
106
|
+
return undefined;
|
|
107
|
+
},
|
|
108
|
+
|
|
109
|
+
createVirtualCode(scriptId, languageId, snapshot) {
|
|
110
|
+
if (languageId !== "svelte") return undefined;
|
|
111
|
+
try {
|
|
112
|
+
return inner.createVirtualCode!(
|
|
113
|
+
scriptId.fsPath,
|
|
114
|
+
languageId,
|
|
115
|
+
snapshot,
|
|
116
|
+
{ getAssociatedScript: () => undefined },
|
|
117
|
+
) as VirtualCode | undefined;
|
|
118
|
+
} catch {
|
|
119
|
+
return undefined;
|
|
120
|
+
}
|
|
121
|
+
},
|
|
122
|
+
|
|
123
|
+
updateVirtualCode(scriptId, virtualCode, newSnapshot) {
|
|
124
|
+
try {
|
|
125
|
+
return inner.updateVirtualCode!(
|
|
126
|
+
scriptId.fsPath,
|
|
127
|
+
virtualCode as any,
|
|
128
|
+
newSnapshot,
|
|
129
|
+
{ getAssociatedScript: () => undefined },
|
|
130
|
+
) as VirtualCode | undefined;
|
|
131
|
+
} catch {
|
|
132
|
+
return undefined;
|
|
133
|
+
}
|
|
134
|
+
},
|
|
135
|
+
|
|
136
|
+
typescript: inner.typescript as any,
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const connection = createConnection();
|
|
141
|
+
|
|
142
|
+
// Intercept hover to clean up svelte2tsx internal type noise.
|
|
143
|
+
const _on_hover = connection.onHover.bind(connection);
|
|
144
|
+
(connection as any).onHover = (handler: Function) => {
|
|
145
|
+
_on_hover(async (params: any, token: any) => {
|
|
146
|
+
const result = await handler(params, token);
|
|
147
|
+
if (result?.contents?.kind === "markdown" && typeof result.contents.value === "string") {
|
|
148
|
+
result.contents.value = clean_svelte_hover(result.contents.value);
|
|
149
|
+
}
|
|
150
|
+
return result;
|
|
151
|
+
});
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
const server = createServer(connection);
|
|
155
|
+
|
|
156
|
+
connection.listen();
|
|
157
|
+
|
|
158
|
+
connection.onInitialize((params) => {
|
|
159
|
+
const tsdk = loadTsdkByPath(
|
|
160
|
+
params.initializationOptions?.typescript?.tsdk,
|
|
161
|
+
params.locale,
|
|
162
|
+
);
|
|
163
|
+
|
|
164
|
+
return server.initialize(
|
|
165
|
+
params,
|
|
166
|
+
createTypeScriptProject(
|
|
167
|
+
tsdk.typescript,
|
|
168
|
+
tsdk.diagnosticMessages,
|
|
169
|
+
() => ({
|
|
170
|
+
languagePlugins: [
|
|
171
|
+
createPfmLanguagePluginForServer(),
|
|
172
|
+
createSvelteLanguagePluginForServer(),
|
|
173
|
+
],
|
|
174
|
+
}),
|
|
175
|
+
),
|
|
176
|
+
[
|
|
177
|
+
...createTypeScriptServices(tsdk.typescript),
|
|
178
|
+
createCssService(),
|
|
179
|
+
createMarkdownService(),
|
|
180
|
+
],
|
|
181
|
+
);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
connection.onInitialized(server.initialized);
|
|
185
|
+
|
|
186
|
+
connection.onShutdown(server.shutdown);
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { clean_svelte_hover } from "../src/clean_hover";
|
|
3
|
+
|
|
4
|
+
describe("clean_svelte_hover", () => {
|
|
5
|
+
it("strips IsomorphicComponent wrapper, keeps only props", () => {
|
|
6
|
+
const input = `\`\`\`typescript
|
|
7
|
+
(alias) const Test: __sveltets_2_IsomorphicComponent<{
|
|
8
|
+
value: number;
|
|
9
|
+
label?: string | undefined;
|
|
10
|
+
count?: number | undefined;
|
|
11
|
+
}, {
|
|
12
|
+
[evt: string]: CustomEvent<any>;
|
|
13
|
+
}, {}, {}, string>
|
|
14
|
+
import Test
|
|
15
|
+
\`\`\``;
|
|
16
|
+
|
|
17
|
+
const result = clean_svelte_hover(input);
|
|
18
|
+
expect(result).toContain("const Test<{");
|
|
19
|
+
expect(result).toContain("value: number;");
|
|
20
|
+
expect(result).not.toContain("__sveltets_2_IsomorphicComponent");
|
|
21
|
+
expect(result).not.toContain("CustomEvent");
|
|
22
|
+
expect(result).not.toContain("[evt: string]");
|
|
23
|
+
expect(result).toContain("import Test");
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("handles arrow function types in props without breaking", () => {
|
|
27
|
+
const input = `\`\`\`typescript
|
|
28
|
+
(alias) const Test: __sveltets_2_IsomorphicComponent<{
|
|
29
|
+
value: number;
|
|
30
|
+
label?: string | undefined;
|
|
31
|
+
count?: number | undefined;
|
|
32
|
+
onClick?: (() => void) | undefined;
|
|
33
|
+
}, {
|
|
34
|
+
[evt: string]: CustomEvent<any>;
|
|
35
|
+
}, {}, {}, string>
|
|
36
|
+
import Test
|
|
37
|
+
\`\`\``;
|
|
38
|
+
|
|
39
|
+
const result = clean_svelte_hover(input);
|
|
40
|
+
expect(result).toContain("const Test<{");
|
|
41
|
+
expect(result).toContain("onClick?: (() => void) | undefined;");
|
|
42
|
+
expect(result).not.toContain("CustomEvent");
|
|
43
|
+
expect(result).not.toContain("[evt: string]");
|
|
44
|
+
expect(result).toContain("import Test");
|
|
45
|
+
// Should NOT have leftover text from the old definition
|
|
46
|
+
expect(result).not.toContain("}, {");
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("returns unchanged markdown when no marker present", () => {
|
|
50
|
+
const input = "```typescript\nconst x: number\n```";
|
|
51
|
+
expect(clean_svelte_hover(input)).toBe(input);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("handles generic prop types", () => {
|
|
55
|
+
const input = `(alias) const Foo: __sveltets_2_IsomorphicComponent<{
|
|
56
|
+
items: Array<string>;
|
|
57
|
+
map: Map<string, number>;
|
|
58
|
+
}, {}, {}, {}, string>
|
|
59
|
+
import Foo`;
|
|
60
|
+
|
|
61
|
+
const result = clean_svelte_hover(input);
|
|
62
|
+
expect(result).toContain("const Foo<{");
|
|
63
|
+
expect(result).toContain("items: Array<string>;");
|
|
64
|
+
expect(result).toContain("map: Map<string, number>;");
|
|
65
|
+
expect(result).not.toContain("__sveltets_2_IsomorphicComponent");
|
|
66
|
+
expect(result).toContain("import Foo");
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("strips SvelteComponent type alias from import hover", () => {
|
|
70
|
+
const input = `\`\`\`typescript
|
|
71
|
+
(alias) type Test = SvelteComponent<{
|
|
72
|
+
value: number;
|
|
73
|
+
label?: string | undefined;
|
|
74
|
+
count?: number | undefined;
|
|
75
|
+
onClick?: (() => void) | undefined;
|
|
76
|
+
}, {
|
|
77
|
+
[evt: string]: CustomEvent<any>;
|
|
78
|
+
}, {}> & {
|
|
79
|
+
$$bindings?: string | undefined;
|
|
80
|
+
}
|
|
81
|
+
(alias) const Test<{
|
|
82
|
+
value: number;
|
|
83
|
+
label?: string | undefined;
|
|
84
|
+
count?: number | undefined;
|
|
85
|
+
onClick?: (() => void) | undefined;
|
|
86
|
+
}>
|
|
87
|
+
import Test
|
|
88
|
+
\`\`\``;
|
|
89
|
+
|
|
90
|
+
const result = clean_svelte_hover(input);
|
|
91
|
+
// Should remove the SvelteComponent type alias entirely
|
|
92
|
+
expect(result).not.toContain("SvelteComponent");
|
|
93
|
+
expect(result).not.toContain("$$bindings");
|
|
94
|
+
expect(result).not.toContain("CustomEvent");
|
|
95
|
+
// Should keep the cleaned const + import
|
|
96
|
+
expect(result).toContain("const Test<{");
|
|
97
|
+
expect(result).toContain("onClick?: (() => void) | undefined;");
|
|
98
|
+
expect(result).toContain("import Test");
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("handles props with no callback types", () => {
|
|
102
|
+
const input = `(alias) const Simple: __sveltets_2_IsomorphicComponent<{
|
|
103
|
+
name: string;
|
|
104
|
+
}, {}, {}, {}, string>
|
|
105
|
+
import Simple`;
|
|
106
|
+
|
|
107
|
+
const result = clean_svelte_hover(input);
|
|
108
|
+
expect(result).toBe(`(alias) const Simple<{
|
|
109
|
+
name: string;
|
|
110
|
+
}>
|
|
111
|
+
import Simple`);
|
|
112
|
+
});
|
|
113
|
+
});
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Integration test for the PFM LanguagePlugin.
|
|
3
|
+
*
|
|
4
|
+
* Tests the plugin in the context of Volar's language system,
|
|
5
|
+
* verifying that VirtualCode structure, mappings, and source maps
|
|
6
|
+
* work correctly for editor features.
|
|
7
|
+
*/
|
|
8
|
+
import { describe, it, expect } from "vitest";
|
|
9
|
+
import { create_pfm_language_plugin } from "@mdsvex/language-core";
|
|
10
|
+
import type { IScriptSnapshot, CodeMapping } from "@volar/language-core";
|
|
11
|
+
|
|
12
|
+
function snap(text: string): IScriptSnapshot {
|
|
13
|
+
return {
|
|
14
|
+
getText: (s, e) => text.slice(s, e),
|
|
15
|
+
getLength: () => text.length,
|
|
16
|
+
getChangeRange: () => undefined,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const dummyCtx = { getAssociatedScript: () => undefined as any };
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Simulate what a Volar SourceMap.toGeneratedLocation does:
|
|
24
|
+
* given a source offset, find the generated offset through mappings.
|
|
25
|
+
*/
|
|
26
|
+
function toGeneratedOffset(
|
|
27
|
+
mappings: CodeMapping[],
|
|
28
|
+
sourceOffset: number,
|
|
29
|
+
): number | undefined {
|
|
30
|
+
for (const m of mappings) {
|
|
31
|
+
for (let i = 0; i < m.sourceOffsets.length; i++) {
|
|
32
|
+
const srcStart = m.sourceOffsets[i];
|
|
33
|
+
const srcLen = m.lengths[i];
|
|
34
|
+
if (sourceOffset >= srcStart && sourceOffset < srcStart + srcLen) {
|
|
35
|
+
const delta = sourceOffset - srcStart;
|
|
36
|
+
const genLen = m.generatedLengths
|
|
37
|
+
? m.generatedLengths[i]
|
|
38
|
+
: m.lengths[i];
|
|
39
|
+
// for identity mappings, delta applies directly
|
|
40
|
+
if (srcLen === genLen || !m.generatedLengths) {
|
|
41
|
+
return m.generatedOffsets[i] + delta;
|
|
42
|
+
}
|
|
43
|
+
// for non-identity, return start of generated range
|
|
44
|
+
return m.generatedOffsets[i];
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return undefined;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Simulate what a Volar SourceMap.toSourceLocation does:
|
|
53
|
+
* given a generated offset, find the source offset through mappings.
|
|
54
|
+
*/
|
|
55
|
+
function toSourceOffset(
|
|
56
|
+
mappings: CodeMapping[],
|
|
57
|
+
generatedOffset: number,
|
|
58
|
+
): number | undefined {
|
|
59
|
+
for (const m of mappings) {
|
|
60
|
+
for (let i = 0; i < m.generatedOffsets.length; i++) {
|
|
61
|
+
const genStart = m.generatedOffsets[i];
|
|
62
|
+
const genLen = m.generatedLengths
|
|
63
|
+
? m.generatedLengths[i]
|
|
64
|
+
: m.lengths[i];
|
|
65
|
+
if (
|
|
66
|
+
generatedOffset >= genStart &&
|
|
67
|
+
generatedOffset < genStart + genLen
|
|
68
|
+
) {
|
|
69
|
+
const delta = generatedOffset - genStart;
|
|
70
|
+
const srcLen = m.lengths[i];
|
|
71
|
+
if (srcLen === genLen || !m.generatedLengths) {
|
|
72
|
+
return m.sourceOffsets[i] + delta;
|
|
73
|
+
}
|
|
74
|
+
return m.sourceOffsets[i];
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return undefined;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
describe("Integration: LanguagePlugin in Volar context", () => {
|
|
82
|
+
const plugin = create_pfm_language_plugin();
|
|
83
|
+
|
|
84
|
+
it("frontmatter variable is navigable from PFM source to TS", () => {
|
|
85
|
+
const pfm = "---\ntitle: hello\n---\n\n# {title}\n";
|
|
86
|
+
const vc = plugin.createVirtualCode!(
|
|
87
|
+
"test.pfm",
|
|
88
|
+
"pfm",
|
|
89
|
+
snap(pfm),
|
|
90
|
+
dummyCtx,
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
const tsCode = vc!.embeddedCodes![0];
|
|
94
|
+
const tsText = tsCode.snapshot.getText(0, tsCode.snapshot.getLength());
|
|
95
|
+
|
|
96
|
+
// Find "title" in the PFM frontmatter
|
|
97
|
+
const pfmTitleOffset = pfm.indexOf("title:");
|
|
98
|
+
expect(pfmTitleOffset).toBeGreaterThan(0);
|
|
99
|
+
|
|
100
|
+
// Map it to TS
|
|
101
|
+
const tsOffset = toGeneratedOffset(tsCode.mappings, pfmTitleOffset);
|
|
102
|
+
expect(tsOffset).toBeDefined();
|
|
103
|
+
|
|
104
|
+
// The TS text at that offset should be "title"
|
|
105
|
+
const tsAtOffset = tsText.slice(tsOffset!, tsOffset! + 5);
|
|
106
|
+
expect(tsAtOffset).toBe("title");
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it("inline expression round-trips: PFM → TS → PFM", () => {
|
|
110
|
+
const pfm = "---\ncount: 0\n---\n\n{count + 1}\n";
|
|
111
|
+
const vc = plugin.createVirtualCode!(
|
|
112
|
+
"test.pfm",
|
|
113
|
+
"pfm",
|
|
114
|
+
snap(pfm),
|
|
115
|
+
dummyCtx,
|
|
116
|
+
);
|
|
117
|
+
|
|
118
|
+
const tsCode = vc!.embeddedCodes![0];
|
|
119
|
+
const tsText = tsCode.snapshot.getText(0, tsCode.snapshot.getLength());
|
|
120
|
+
|
|
121
|
+
// Find "count + 1" in PFM
|
|
122
|
+
const pfmExprOffset = pfm.indexOf("count + 1");
|
|
123
|
+
expect(pfmExprOffset).toBeGreaterThan(0);
|
|
124
|
+
|
|
125
|
+
// Forward: PFM → TS
|
|
126
|
+
const tsOffset = toGeneratedOffset(tsCode.mappings, pfmExprOffset);
|
|
127
|
+
expect(tsOffset).toBeDefined();
|
|
128
|
+
|
|
129
|
+
// Verify TS text
|
|
130
|
+
const tsAtOffset = tsText.slice(tsOffset!, tsOffset! + 9);
|
|
131
|
+
expect(tsAtOffset).toBe("count + 1");
|
|
132
|
+
|
|
133
|
+
// Reverse: TS → PFM
|
|
134
|
+
const pfmOffset = toSourceOffset(tsCode.mappings, tsOffset!);
|
|
135
|
+
expect(pfmOffset).toBeDefined();
|
|
136
|
+
expect(pfmOffset).toBe(pfmExprOffset);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it("import statement round-trips: PFM → TS → PFM", () => {
|
|
140
|
+
const pfm = 'import Counter from "./Counter.svelte"\n\n# Hello\n';
|
|
141
|
+
const vc = plugin.createVirtualCode!(
|
|
142
|
+
"test.pfm",
|
|
143
|
+
"pfm",
|
|
144
|
+
snap(pfm),
|
|
145
|
+
dummyCtx,
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
const tsCode = vc!.embeddedCodes![0];
|
|
149
|
+
const tsText = tsCode.snapshot.getText(0, tsCode.snapshot.getLength());
|
|
150
|
+
|
|
151
|
+
// Find "import Counter" in PFM
|
|
152
|
+
const pfmImportOffset = pfm.indexOf("import Counter");
|
|
153
|
+
expect(pfmImportOffset).toBe(0);
|
|
154
|
+
|
|
155
|
+
// Forward: PFM → TS
|
|
156
|
+
const tsOffset = toGeneratedOffset(tsCode.mappings, pfmImportOffset);
|
|
157
|
+
expect(tsOffset).toBeDefined();
|
|
158
|
+
|
|
159
|
+
// TS should contain the import
|
|
160
|
+
const tsAtOffset = tsText.slice(tsOffset!, tsOffset! + 14);
|
|
161
|
+
expect(tsAtOffset).toBe("import Counter");
|
|
162
|
+
|
|
163
|
+
// Reverse: TS → PFM
|
|
164
|
+
const pfmOffset = toSourceOffset(tsCode.mappings, tsOffset!);
|
|
165
|
+
expect(pfmOffset).toBeDefined();
|
|
166
|
+
expect(pfmOffset).toBe(pfmImportOffset);
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it("VirtualCode tree structure is correct for Volar", () => {
|
|
170
|
+
const pfm = "---\ntitle: hello\n---\n\n# {title}\n";
|
|
171
|
+
const vc = plugin.createVirtualCode!(
|
|
172
|
+
"test.pfm",
|
|
173
|
+
"pfm",
|
|
174
|
+
snap(pfm),
|
|
175
|
+
dummyCtx,
|
|
176
|
+
);
|
|
177
|
+
|
|
178
|
+
// Root VirtualCode
|
|
179
|
+
expect(vc!.id).toBe("root");
|
|
180
|
+
expect(vc!.languageId).toBe("pfm");
|
|
181
|
+
expect(vc!.snapshot.getLength()).toBe(pfm.length);
|
|
182
|
+
expect(vc!.mappings.length).toBeGreaterThan(0);
|
|
183
|
+
|
|
184
|
+
// Embedded TS VirtualCode
|
|
185
|
+
expect(vc!.embeddedCodes!.length).toBe(2); // TS + markdown
|
|
186
|
+
const tsVc = vc!.embeddedCodes![0];
|
|
187
|
+
expect(tsVc.id).toBe("ts");
|
|
188
|
+
expect(tsVc.languageId).toBe("typescript");
|
|
189
|
+
expect(tsVc.snapshot.getLength()).toBeGreaterThan(0);
|
|
190
|
+
expect(tsVc.mappings.length).toBeGreaterThan(0);
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it("verification capability is set on navigable mappings", () => {
|
|
194
|
+
const pfm = "---\ntitle: hello\n---\n\n# {title}\n";
|
|
195
|
+
const vc = plugin.createVirtualCode!(
|
|
196
|
+
"test.pfm",
|
|
197
|
+
"pfm",
|
|
198
|
+
snap(pfm),
|
|
199
|
+
dummyCtx,
|
|
200
|
+
);
|
|
201
|
+
|
|
202
|
+
const tsCode = vc!.embeddedCodes![0];
|
|
203
|
+
|
|
204
|
+
// Find a mapping that covers the "title" expression
|
|
205
|
+
const pfmExprOffset = pfm.lastIndexOf("title");
|
|
206
|
+
const mapping = tsCode.mappings.find((m) => {
|
|
207
|
+
for (let i = 0; i < m.sourceOffsets.length; i++) {
|
|
208
|
+
const start = m.sourceOffsets[i];
|
|
209
|
+
const end = start + m.lengths[i];
|
|
210
|
+
if (pfmExprOffset >= start && pfmExprOffset < end) return true;
|
|
211
|
+
}
|
|
212
|
+
return false;
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
// Should have at least verification or navigation capability
|
|
216
|
+
expect(mapping).toBeDefined();
|
|
217
|
+
const data = mapping!.data;
|
|
218
|
+
const hasCapability =
|
|
219
|
+
data.verification || data.navigation || data.semantic;
|
|
220
|
+
expect(hasCapability).toBeTruthy();
|
|
221
|
+
});
|
|
222
|
+
});
|
package/tsconfig.json
ADDED