@mdsvex/language-server 0.1.0-next.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 +20 -0
- package/dist/server.cjs +57158 -22135
- package/package.json +2 -2
- package/src/clean_hover.ts +9 -0
- package/src/compile_diagnostics.ts +36 -0
- package/src/completions.ts +163 -0
- package/src/definitions.ts +31 -0
- package/src/index.ts +79 -16
- package/src/root_code.ts +16 -0
- package/test/clean_hover.spec.ts +17 -0
- package/test/compile_diagnostics.spec.ts +60 -0
- package/test/definitions.spec.ts +34 -0
- package/test/server.spec.ts +250 -0
- package/vite.config.build.ts +2 -1
|
@@ -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
|
+
});
|
package/vite.config.build.ts
CHANGED
|
@@ -13,7 +13,8 @@ export default defineConfig({
|
|
|
13
13
|
ssr: true,
|
|
14
14
|
minify: false,
|
|
15
15
|
rollupOptions: {
|
|
16
|
-
|
|
16
|
+
// the vite plugin half of mdsvex loads vite lazily, the editor never reaches it
|
|
17
|
+
external: ["typescript", "vite"],
|
|
17
18
|
output: {
|
|
18
19
|
entryFileNames: "server.cjs",
|
|
19
20
|
},
|