@mdsvex/language-server 0.0.0 → 0.1.0-next.1
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 +32 -0
- package/LICENSE +21 -0
- package/bin/pfm-language-server.js +3 -0
- package/dist/server.cjs +184093 -0
- package/package.json +36 -7
- package/src/clean_hover.ts +106 -0
- package/src/compile_diagnostics.ts +36 -0
- package/src/completions.ts +163 -0
- package/src/definitions.ts +31 -0
- package/src/index.ts +249 -0
- package/src/root_code.ts +16 -0
- package/test/clean_hover.spec.ts +130 -0
- package/test/compile_diagnostics.spec.ts +60 -0
- package/test/definitions.spec.ts +34 -0
- package/test/integration.spec.ts +222 -0
- package/test/server.spec.ts +250 -0
- package/tsconfig.json +11 -0
- package/vite.config.build.ts +27 -0
- package/README.md +0 -3
|
@@ -0,0 +1,130 @@
|
|
|
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("strips the $on/$set alias of a runes component", () => {
|
|
50
|
+
const input = `\`\`\`typescript
|
|
51
|
+
(alias) type h2 = {
|
|
52
|
+
$on?(type: string, callback: (e: any) => void): () => void;
|
|
53
|
+
$set?(props: Partial<$$ComponentProps>): void;
|
|
54
|
+
}
|
|
55
|
+
(alias) const h2: Component<$$ComponentProps, {}, "">
|
|
56
|
+
export h2
|
|
57
|
+
\`\`\``;
|
|
58
|
+
|
|
59
|
+
const result = clean_svelte_hover(input);
|
|
60
|
+
expect(result).not.toContain("$on?");
|
|
61
|
+
expect(result).not.toContain("type h2");
|
|
62
|
+
expect(result).toContain('(alias) const h2: Component<$$ComponentProps, {}, "">');
|
|
63
|
+
expect(result).toContain("export h2");
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("returns unchanged markdown when no marker present", () => {
|
|
67
|
+
const input = "```typescript\nconst x: number\n```";
|
|
68
|
+
expect(clean_svelte_hover(input)).toBe(input);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("handles generic prop types", () => {
|
|
72
|
+
const input = `(alias) const Foo: __sveltets_2_IsomorphicComponent<{
|
|
73
|
+
items: Array<string>;
|
|
74
|
+
map: Map<string, number>;
|
|
75
|
+
}, {}, {}, {}, string>
|
|
76
|
+
import Foo`;
|
|
77
|
+
|
|
78
|
+
const result = clean_svelte_hover(input);
|
|
79
|
+
expect(result).toContain("const Foo<{");
|
|
80
|
+
expect(result).toContain("items: Array<string>;");
|
|
81
|
+
expect(result).toContain("map: Map<string, number>;");
|
|
82
|
+
expect(result).not.toContain("__sveltets_2_IsomorphicComponent");
|
|
83
|
+
expect(result).toContain("import Foo");
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("strips SvelteComponent type alias from import hover", () => {
|
|
87
|
+
const input = `\`\`\`typescript
|
|
88
|
+
(alias) type Test = SvelteComponent<{
|
|
89
|
+
value: number;
|
|
90
|
+
label?: string | undefined;
|
|
91
|
+
count?: number | undefined;
|
|
92
|
+
onClick?: (() => void) | undefined;
|
|
93
|
+
}, {
|
|
94
|
+
[evt: string]: CustomEvent<any>;
|
|
95
|
+
}, {}> & {
|
|
96
|
+
$$bindings?: string | undefined;
|
|
97
|
+
}
|
|
98
|
+
(alias) const Test<{
|
|
99
|
+
value: number;
|
|
100
|
+
label?: string | undefined;
|
|
101
|
+
count?: number | undefined;
|
|
102
|
+
onClick?: (() => void) | undefined;
|
|
103
|
+
}>
|
|
104
|
+
import Test
|
|
105
|
+
\`\`\``;
|
|
106
|
+
|
|
107
|
+
const result = clean_svelte_hover(input);
|
|
108
|
+
// Should remove the SvelteComponent type alias entirely
|
|
109
|
+
expect(result).not.toContain("SvelteComponent");
|
|
110
|
+
expect(result).not.toContain("$$bindings");
|
|
111
|
+
expect(result).not.toContain("CustomEvent");
|
|
112
|
+
// Should keep the cleaned const + import
|
|
113
|
+
expect(result).toContain("const Test<{");
|
|
114
|
+
expect(result).toContain("onClick?: (() => void) | undefined;");
|
|
115
|
+
expect(result).toContain("import Test");
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it("handles props with no callback types", () => {
|
|
119
|
+
const input = `(alias) const Simple: __sveltets_2_IsomorphicComponent<{
|
|
120
|
+
name: string;
|
|
121
|
+
}, {}, {}, {}, string>
|
|
122
|
+
import Simple`;
|
|
123
|
+
|
|
124
|
+
const result = clean_svelte_hover(input);
|
|
125
|
+
expect(result).toBe(`(alias) const Simple<{
|
|
126
|
+
name: string;
|
|
127
|
+
}>
|
|
128
|
+
import Simple`);
|
|
129
|
+
});
|
|
130
|
+
});
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { URI } from "vscode-uri";
|
|
3
|
+
import { create_compile_diagnostics } from "../src/compile_diagnostics";
|
|
4
|
+
|
|
5
|
+
const SOURCE = "::nope[y]\n<img use:a />\n";
|
|
6
|
+
|
|
7
|
+
/** the slice of a volar context the plugin reads */
|
|
8
|
+
function context(diagnostics: unknown) {
|
|
9
|
+
const root = { diagnostics };
|
|
10
|
+
return {
|
|
11
|
+
decodeEmbeddedDocumentUri: (uri: URI) =>
|
|
12
|
+
uri.scheme === "volar-embedded-content"
|
|
13
|
+
? [URI.file("/doc.svx"), "root"]
|
|
14
|
+
: undefined,
|
|
15
|
+
language: { scripts: { get: () => ({ generated: { root } }) } },
|
|
16
|
+
} as any;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function document(languageId: string) {
|
|
20
|
+
return {
|
|
21
|
+
uri: "volar-embedded-content://root/doc.svx",
|
|
22
|
+
languageId,
|
|
23
|
+
positionAt(offset: number) {
|
|
24
|
+
const before = SOURCE.slice(0, offset).split("\n");
|
|
25
|
+
return { line: before.length - 1, character: before.at(-1)!.length };
|
|
26
|
+
},
|
|
27
|
+
} as any;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
describe("compile diagnostics", () => {
|
|
31
|
+
const plugin = create_compile_diagnostics();
|
|
32
|
+
|
|
33
|
+
it("reports the root diagnostics as errors and warnings from mdsvex", () => {
|
|
34
|
+
const instance = plugin.create(
|
|
35
|
+
context([
|
|
36
|
+
{ start: 0, end: 6, message: "no component renders ::nope", severity: "error" },
|
|
37
|
+
{ start: 10, end: 14, message: "<img> stays an element", severity: "warning" },
|
|
38
|
+
]),
|
|
39
|
+
);
|
|
40
|
+
expect(instance.provideDiagnostics!(document("pfm"), {} as any)).toEqual([
|
|
41
|
+
{
|
|
42
|
+
range: { start: { line: 0, character: 0 }, end: { line: 0, character: 6 } },
|
|
43
|
+
message: "no component renders ::nope",
|
|
44
|
+
severity: 1,
|
|
45
|
+
source: "mdsvex",
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
range: { start: { line: 1, character: 0 }, end: { line: 1, character: 4 } },
|
|
49
|
+
message: "<img> stays an element",
|
|
50
|
+
severity: 2,
|
|
51
|
+
source: "mdsvex",
|
|
52
|
+
},
|
|
53
|
+
]);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("leaves embedded typescript, css and markdown to their services", () => {
|
|
57
|
+
const instance = plugin.create(context([]));
|
|
58
|
+
expect(instance.provideDiagnostics!(document("typescript"), {} as any)).toBeUndefined();
|
|
59
|
+
});
|
|
60
|
+
});
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { without_self } from "../src/definitions";
|
|
3
|
+
|
|
4
|
+
const range = (line: number, from: number, to: number) => ({
|
|
5
|
+
start: { line, character: from },
|
|
6
|
+
end: { line, character: to },
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
describe("without_self", () => {
|
|
10
|
+
const at = { line: 3, character: 1 };
|
|
11
|
+
|
|
12
|
+
it("drops a location holding the request when another target exists", () => {
|
|
13
|
+
const self = { uri: "file:///doc.svx", range: range(3, 0, 10) };
|
|
14
|
+
const component = { uri: "file:///Heading.svelte", range: range(0, 0, 0) };
|
|
15
|
+
expect(without_self([self, component], "file:///doc.svx", at)).toEqual([component]);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("keeps a lone self location and other places in the document", () => {
|
|
19
|
+
const self = { uri: "file:///doc.svx", range: range(3, 0, 10) };
|
|
20
|
+
const elsewhere = { uri: "file:///doc.svx", range: range(9, 0, 4) };
|
|
21
|
+
expect(without_self([self], "file:///doc.svx", at)).toEqual([self]);
|
|
22
|
+
expect(without_self([self, elsewhere], "file:///doc.svx", at)).toEqual([elsewhere]);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("reads location links by their target", () => {
|
|
26
|
+
const link = {
|
|
27
|
+
targetUri: "file:///doc.svx",
|
|
28
|
+
targetRange: range(3, 0, 10),
|
|
29
|
+
targetSelectionRange: range(3, 0, 2),
|
|
30
|
+
};
|
|
31
|
+
const other = { ...link, targetUri: "file:///Heading.svelte" };
|
|
32
|
+
expect(without_self([link, other], "file:///doc.svx", at)).toEqual([other]);
|
|
33
|
+
});
|
|
34
|
+
});
|
|
@@ -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
|
+
});
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
import { dirname, join, resolve } from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
|
|
6
|
+
import { startLanguageServer } from "@volar/test-utils";
|
|
7
|
+
import type { LanguageServerHandle } from "@volar/test-utils";
|
|
8
|
+
import type { TextDocument } from "vscode-languageserver-textdocument";
|
|
9
|
+
import { URI } from "vscode-uri";
|
|
10
|
+
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
|
11
|
+
|
|
12
|
+
const HERE = dirname(fileURLToPath(import.meta.url));
|
|
13
|
+
const SERVER = resolve(HERE, "../dist/server.cjs");
|
|
14
|
+
const require = createRequire(import.meta.url);
|
|
15
|
+
|
|
16
|
+
const svelte = (props: string, module = "") =>
|
|
17
|
+
[
|
|
18
|
+
...(module ? ['<script module lang="ts">', module, "</script>"] : []),
|
|
19
|
+
'<script lang="ts">',
|
|
20
|
+
" import type { Snippet } from 'svelte';",
|
|
21
|
+
` let p: { ${props} } = $props();`,
|
|
22
|
+
"</script>",
|
|
23
|
+
"<div></div>",
|
|
24
|
+
"",
|
|
25
|
+
].join("\n");
|
|
26
|
+
|
|
27
|
+
const FILES: Record<string, string> = {
|
|
28
|
+
"src/lib/Docs.svelte": svelte(
|
|
29
|
+
"title: string; draft?: boolean; level?: 'beginner' | 'advanced'; children: Snippet",
|
|
30
|
+
" export { default as h2 } from './Heading.svelte';\n export * as directives from './directives.ts';",
|
|
31
|
+
),
|
|
32
|
+
"src/lib/Plain.svelte": svelte("children: Snippet"),
|
|
33
|
+
"src/lib/Heading.svelte": svelte("children?: Snippet"),
|
|
34
|
+
"src/lib/Callout.svelte": svelte("tone?: 'info' | 'warn'; children?: Snippet"),
|
|
35
|
+
"src/lib/directives.ts": "export { default as Callout } from './Callout.svelte';\n",
|
|
36
|
+
"src/doc.svx": [
|
|
37
|
+
"---",
|
|
38
|
+
"draft: yes",
|
|
39
|
+
"template: docs",
|
|
40
|
+
"---",
|
|
41
|
+
"",
|
|
42
|
+
"## Heading",
|
|
43
|
+
"",
|
|
44
|
+
":::Callout[x](tone=urgent)",
|
|
45
|
+
":::",
|
|
46
|
+
"",
|
|
47
|
+
"::missing[y]",
|
|
48
|
+
"",
|
|
49
|
+
].join("\n"),
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
describe.skipIf(!existsSync(SERVER))("the language server", { timeout: 30_000 }, () => {
|
|
53
|
+
let root: string;
|
|
54
|
+
let server: LanguageServerHandle;
|
|
55
|
+
let doc: TextDocument;
|
|
56
|
+
const at = (needle: string, delta = 0) =>
|
|
57
|
+
doc.positionAt(doc.getText().indexOf(needle) + delta);
|
|
58
|
+
|
|
59
|
+
beforeAll(async () => {
|
|
60
|
+
root = mkdtempSync(join(HERE, ".tmp-server-"));
|
|
61
|
+
for (const [file, content] of Object.entries(FILES)) {
|
|
62
|
+
mkdirSync(dirname(join(root, file)), { recursive: true });
|
|
63
|
+
writeFileSync(join(root, file), content);
|
|
64
|
+
}
|
|
65
|
+
const lib = root + "/src/lib/";
|
|
66
|
+
mkdirSync(join(root, "node_modules/.mdsvex"), { recursive: true });
|
|
67
|
+
writeFileSync(
|
|
68
|
+
join(root, "node_modules/.mdsvex/manifest.json"),
|
|
69
|
+
JSON.stringify({
|
|
70
|
+
version: 1,
|
|
71
|
+
root,
|
|
72
|
+
extensions: [".svx"],
|
|
73
|
+
component_mode: "markdown",
|
|
74
|
+
frontmatter_parse: false,
|
|
75
|
+
directive_plugins: false,
|
|
76
|
+
select_template: false,
|
|
77
|
+
templates: {
|
|
78
|
+
docs: {
|
|
79
|
+
id: "mdsvex:template/docs",
|
|
80
|
+
file: lib + "Docs.svelte",
|
|
81
|
+
components: ["h2"],
|
|
82
|
+
extra: null,
|
|
83
|
+
directives: [
|
|
84
|
+
{
|
|
85
|
+
id: "mdsvex:template-directives/docs",
|
|
86
|
+
file: lib + "directives.ts",
|
|
87
|
+
names: ["Callout"],
|
|
88
|
+
},
|
|
89
|
+
],
|
|
90
|
+
},
|
|
91
|
+
plain: {
|
|
92
|
+
id: "mdsvex:template/plain",
|
|
93
|
+
file: lib + "Plain.svelte",
|
|
94
|
+
components: [],
|
|
95
|
+
extra: null,
|
|
96
|
+
directives: [],
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
components: [],
|
|
100
|
+
directives: [],
|
|
101
|
+
documents: {},
|
|
102
|
+
}),
|
|
103
|
+
);
|
|
104
|
+
// windows can not remove a directory a live process works in
|
|
105
|
+
server = startLanguageServer(SERVER, HERE);
|
|
106
|
+
await server.initialize(URI.file(root).toString(), {
|
|
107
|
+
typescript: { tsdk: dirname(require.resolve("typescript")) },
|
|
108
|
+
});
|
|
109
|
+
doc = await server.openTextDocument(join(root, "src/doc.svx"), "pfm");
|
|
110
|
+
// the first request loads the typescript project, slow on windows
|
|
111
|
+
await server.sendHoverRequest(doc.uri, at("## Heading"));
|
|
112
|
+
}, 60_000);
|
|
113
|
+
|
|
114
|
+
afterAll(async () => {
|
|
115
|
+
if (server) {
|
|
116
|
+
const exited = new Promise((done) => server.process.once("exit", done));
|
|
117
|
+
await server.shutdown();
|
|
118
|
+
server.process.kill();
|
|
119
|
+
await exited;
|
|
120
|
+
}
|
|
121
|
+
rmSync(root, { recursive: true, force: true, maxRetries: 10, retryDelay: 100 });
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it("hovers and jumps straight to the component replacing a heading", async () => {
|
|
125
|
+
const hover = await server.sendHoverRequest(doc.uri, at("## Heading"));
|
|
126
|
+
expect((hover?.contents as { value: string }).value).toContain("const h2");
|
|
127
|
+
const definitions = (await server.sendDefinitionRequest(
|
|
128
|
+
doc.uri,
|
|
129
|
+
at("## Heading"),
|
|
130
|
+
)) as { uri: string }[];
|
|
131
|
+
expect(definitions.map((d) => d.uri.split("/").pop())).toEqual([
|
|
132
|
+
"Heading.svelte",
|
|
133
|
+
]);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it("hovers a directive and the prop its arg becomes", async () => {
|
|
137
|
+
const name = await server.sendHoverRequest(doc.uri, at("Callout["));
|
|
138
|
+
expect((name?.contents as { value: string }).value).toContain("const Callout");
|
|
139
|
+
const arg = await server.sendHoverRequest(doc.uri, at("tone="));
|
|
140
|
+
expect((arg?.contents as { value: string }).value).toContain('"info" | "warn"');
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it("types the template key as the union of template names", async () => {
|
|
144
|
+
const hover = await server.sendHoverRequest(doc.uri, at("template:"));
|
|
145
|
+
expect((hover?.contents as { value: string }).value).toContain(
|
|
146
|
+
'(property) template: false | "docs" | "plain"',
|
|
147
|
+
);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it("completes the template names and false", async () => {
|
|
151
|
+
const list = await server.sendCompletionRequest(doc.uri, at("template: ", 10));
|
|
152
|
+
expect(list?.items.map((i) => [i.label, i.detail])).toEqual([
|
|
153
|
+
["docs", "./lib/Docs.svelte"],
|
|
154
|
+
["plain", "./lib/Plain.svelte"],
|
|
155
|
+
["false", "no template"],
|
|
156
|
+
]);
|
|
157
|
+
expect(list?.items[0].textEdit).toMatchObject({
|
|
158
|
+
newText: "docs",
|
|
159
|
+
range: { start: at("template: ", 10), end: at("docs\n", 4) },
|
|
160
|
+
});
|
|
161
|
+
expect(await server.sendCompletionRequest(doc.uri, at("## Heading", 3))).toMatchObject({
|
|
162
|
+
items: expect.not.arrayContaining([expect.objectContaining({ label: "plain" })]),
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
/** labels of the completions at the cursor marked | in a document beside doc.svx */
|
|
167
|
+
async function complete(name: string, marked: string) {
|
|
168
|
+
const at = marked.indexOf("|");
|
|
169
|
+
const uri = URI.file(join(root, "src", name)).toString();
|
|
170
|
+
const opened = await server.openInMemoryDocument(
|
|
171
|
+
uri,
|
|
172
|
+
"pfm",
|
|
173
|
+
marked.replace("|", ""),
|
|
174
|
+
);
|
|
175
|
+
const list = await server.sendCompletionRequest(uri, opened.positionAt(at));
|
|
176
|
+
await server.closeTextDocument(uri);
|
|
177
|
+
return (list?.items ?? []).map((i) => [
|
|
178
|
+
i.label,
|
|
179
|
+
i.textEdit && "newText" in i.textEdit ? i.textEdit.newText : i.label,
|
|
180
|
+
]);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
it("completes frontmatter keys from the template props, while the yaml is half typed", async () => {
|
|
184
|
+
expect(
|
|
185
|
+
await complete("keys.svx", "---\ntitle: x\nd|\ntemplate: docs\n---\n\n# x\n"),
|
|
186
|
+
).toEqual([
|
|
187
|
+
["draft", "draft: "],
|
|
188
|
+
["level", "level: "],
|
|
189
|
+
]);
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it("completes a frontmatter value from the literals its prop allows", async () => {
|
|
193
|
+
expect(
|
|
194
|
+
await complete("values.svx", "---\nlevel: |\ntemplate: docs\n---\n\n# x\n"),
|
|
195
|
+
).toEqual([
|
|
196
|
+
["beginner", "beginner"],
|
|
197
|
+
["advanced", "advanced"],
|
|
198
|
+
]);
|
|
199
|
+
expect(
|
|
200
|
+
await complete("flags.svx", "---\ndraft: t|\ntemplate: docs\n---\n\n# x\n"),
|
|
201
|
+
).toEqual([
|
|
202
|
+
["false", "false"],
|
|
203
|
+
["true", "true"],
|
|
204
|
+
]);
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
it("completes directive arg names and values from the component props", async () => {
|
|
208
|
+
const head = "---\ntemplate: docs\n---\n\n";
|
|
209
|
+
expect(await complete("arg-keys.svx", head + ":::Callout[x](|)\n:::\n")).toEqual([
|
|
210
|
+
["tone", "tone="],
|
|
211
|
+
]);
|
|
212
|
+
expect(
|
|
213
|
+
await complete("arg-values.svx", head + ":::Callout[x](tone=|)\n:::\n"),
|
|
214
|
+
).toEqual([
|
|
215
|
+
["info", "info"],
|
|
216
|
+
["warn", "warn"],
|
|
217
|
+
]);
|
|
218
|
+
expect(
|
|
219
|
+
await complete("arg-used.svx", head + ":::Callout[x](tone=info, |)\n:::\n"),
|
|
220
|
+
).toEqual([]);
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
it("does not link directive args as urls", async () => {
|
|
224
|
+
const links = (await server.sendDocumentLinkRequest(doc.uri)) ?? [];
|
|
225
|
+
const text = doc.getText();
|
|
226
|
+
expect(
|
|
227
|
+
links.map((l) =>
|
|
228
|
+
text.slice(doc.offsetAt(l.range.start), doc.offsetAt(l.range.end)),
|
|
229
|
+
),
|
|
230
|
+
).toEqual([]);
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
it("reports type errors and compile errors where they come from", async () => {
|
|
234
|
+
const report = (await server.sendDocumentDiagnosticRequest(doc.uri)) as {
|
|
235
|
+
items: { range: { start: unknown; end: unknown }; message: string }[];
|
|
236
|
+
};
|
|
237
|
+
const text = doc.getText();
|
|
238
|
+
const found = report.items
|
|
239
|
+
.map((d) => [
|
|
240
|
+
text.slice(doc.offsetAt(d.range.start as never), doc.offsetAt(d.range.end as never)),
|
|
241
|
+
d.message.split(". ")[0],
|
|
242
|
+
])
|
|
243
|
+
.sort();
|
|
244
|
+
expect(found).toEqual([
|
|
245
|
+
["::missing", "no component renders the directive ::missing at 11:1"],
|
|
246
|
+
["draft", "Type 'string' is not assignable to type 'boolean'."],
|
|
247
|
+
["tone", `Type '"urgent"' is not assignable to type '"info" | "warn"'.`],
|
|
248
|
+
]);
|
|
249
|
+
});
|
|
250
|
+
});
|