Skip to content

Commit 72e2b93

Browse files
committed
Migrate to Volar
1 parent f2b4d2e commit 72e2b93

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+2729
-5967
lines changed

package-lock.json

Lines changed: 1052 additions & 1976 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/language-server/build.mts

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import { build, BuildOptions } from "esbuild";
2+
import fs from "fs/promises";
3+
import { createRequire } from "module";
24
import path from "path";
35
import { fileURLToPath } from "url";
46

7+
const require = createRequire(import.meta.url);
8+
const thisDir = path.dirname(fileURLToPath(import.meta.url));
9+
const distDir = path.join(thisDir, "dist");
10+
511
const opts: BuildOptions = {
612
bundle: true,
713
outdir: "dist",
@@ -10,31 +16,31 @@ const opts: BuildOptions = {
1016
target: ["node20"],
1117
sourcemap: "linked",
1218
entryPoints: ["src/index.ts"],
13-
absWorkingDir: path.dirname(fileURLToPath(import.meta.url)),
19+
absWorkingDir: thisDir,
1420
plugins: [
1521
{
1622
name: "external-modules",
1723
setup(build) {
1824
build.onResolve(
1925
{ filter: /^[^./]|^\.[^./]|^\.\.[^/]/ },
20-
({ path }) => ({
21-
path,
22-
external: true,
23-
})
26+
({ path }) => ({ path, external: true }),
2427
);
2528
},
2629
},
2730
],
2831
};
2932

3033
await Promise.all([
31-
build({
32-
...opts,
33-
format: "cjs",
34-
}),
35-
build({
36-
...opts,
37-
format: "esm",
38-
outExtension: { ".js": ".mjs" },
39-
}),
34+
// Copy required type definition files
35+
fs.copyFile(
36+
path.join(thisDir, "../language-tools/marko.internal.d.ts"),
37+
path.join(distDir, "marko.internal.d.ts"),
38+
),
39+
fs.copyFile(
40+
path.join(require.resolve("marko/package.json"), "../index.d.ts"),
41+
path.join(distDir, "marko.runtime.d.ts"),
42+
),
43+
// Build the JavaScript bundles
44+
build({ ...opts, format: "cjs" }),
45+
build({ ...opts, format: "esm", outExtension: { ".js": ".mjs" } }),
4046
]);

packages/language-server/package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@
1616
"prettier-plugin-marko": "^3.2.7",
1717
"relative-import-path": "^1.0.0",
1818
"typescript": "^5.8.3",
19+
"@volar/kit": "^2.4.14",
20+
"@volar/language-core": "^2.4.14",
21+
"@volar/language-server": "^2.4.14",
22+
"@volar/language-service": "^2.4.14",
23+
"@volar/typescript": "^2.4.14",
24+
"@volar/test-utils": "^2.4.14",
25+
"volar-service-css": "^0.0.64",
26+
"volar-service-emmet": "^0.0.64",
27+
"volar-service-html": "^0.0.64",
28+
"volar-service-prettier": "^0.0.64",
29+
"volar-service-typescript": "^0.0.64",
30+
"volar-service-typescript-twoslash-queries": "^0.0.64",
1931
"vscode-css-languageservice": "^6.3.7",
2032
"vscode-languageserver": "^9.0.1",
2133
"vscode-languageserver-textdocument": "^1.0.12",

packages/language-server/src/__tests__/index.test.ts

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ import { Project } from "@marko/language-tools";
22
import fs from "fs";
33
import snapshot from "mocha-snap";
44
import path from "path";
5-
import { CancellationToken, Position } from "vscode-languageserver";
6-
import { TextDocument } from "vscode-languageserver-textdocument";
5+
import { Position } from "vscode-languageserver";
76
// import { bench, run } from "mitata";
8-
import { URI } from "vscode-uri";
7+
import { TextDocument } from "vscode-languageserver-textdocument";
98

10-
import MarkoLangaugeService, { documents } from "../service";
119
import { codeFrame } from "./util/code-frame";
10+
import { getLanguageServer } from "./util/language-service";
1211

1312
Project.setDefaultTypePaths({
1413
internalTypesFile: require.resolve(
@@ -21,38 +20,33 @@ Project.setDefaultTypePaths({
2120
// const BENCHED = new Set<string>();
2221
const FIXTURE_DIR = path.join(__dirname, "fixtures");
2322

23+
after(async () => {
24+
const handle = await getLanguageServer();
25+
await handle.shutdown();
26+
});
27+
2428
for (const subdir of fs.readdirSync(FIXTURE_DIR)) {
2529
const fixtureSubdir = path.join(FIXTURE_DIR, subdir);
2630

2731
if (!fs.statSync(fixtureSubdir).isDirectory()) continue;
2832
for (const entry of fs.readdirSync(fixtureSubdir)) {
2933
it(entry, async () => {
34+
const serverHandle = await getLanguageServer();
35+
3036
const fixtureDir = path.join(fixtureSubdir, entry);
3137

3238
for (const filename of loadMarkoFiles(fixtureDir)) {
33-
const doc = documents.get(URI.file(filename).toString())!;
39+
const doc = await serverHandle.openTextDocument(filename, "marko");
3440
const code = doc.getText();
35-
const params = {
36-
textDocument: {
37-
uri: doc.uri,
38-
languageId: doc.languageId,
39-
version: doc.version,
40-
text: code,
41-
},
42-
} as const;
43-
documents.doOpen(params);
4441

4542
let results = "";
4643

4744
for (const position of getHovers(doc)) {
48-
const hoverInfo = await MarkoLangaugeService.doHover(
49-
doc,
50-
{
51-
position,
52-
textDocument: doc,
53-
},
54-
CancellationToken.None,
45+
const hoverInfo = await serverHandle.sendHoverRequest(
46+
doc.uri,
47+
position,
5548
);
49+
5650
const loc = { start: position, end: position };
5751

5852
let message = "";
@@ -87,9 +81,9 @@ for (const subdir of fs.readdirSync(FIXTURE_DIR)) {
8781
language: string;
8882
content: string;
8983
}
90-
| undefined = await MarkoLangaugeService.commands[
91-
"$/showScriptOutput"
92-
](doc.uri);
84+
| undefined = await serverHandle.sendExecuteCommandRequest(
85+
"marko.debug.showScriptOutput",
86+
);
9387
if (scriptOutput) {
9488
await snapshot(scriptOutput.content, {
9589
file: path.relative(
@@ -108,8 +102,8 @@ for (const subdir of fs.readdirSync(FIXTURE_DIR)) {
108102
language: string;
109103
content: string;
110104
}
111-
| undefined = await MarkoLangaugeService.commands["$/showHtmlOutput"](
112-
doc.uri,
105+
| undefined = await serverHandle.sendExecuteCommandRequest(
106+
"marko.debug.showHtmlOutput",
113107
);
114108
if (htmlOutput) {
115109
await snapshot(htmlOutput.content, {
@@ -121,12 +115,23 @@ for (const subdir of fs.readdirSync(FIXTURE_DIR)) {
121115
});
122116
}
123117

124-
const errors = await MarkoLangaugeService.doValidate(doc);
125-
126-
if (errors && errors.length) {
118+
const diagnosticReport =
119+
await serverHandle.sendDocumentDiagnosticRequest(doc.uri);
120+
if (
121+
diagnosticReport.kind === "full" &&
122+
diagnosticReport.items &&
123+
diagnosticReport.items.length
124+
) {
127125
results += "## Diagnostics\n";
128126

129-
for (const error of errors) {
127+
diagnosticReport.items.sort((a, b) => {
128+
const lineDiff = a.range.start.line - b.range.start.line;
129+
if (lineDiff === 0) {
130+
return a.range.start.character - b.range.start.character;
131+
}
132+
return lineDiff;
133+
});
134+
for (const error of diagnosticReport.items) {
130135
const loc = {
131136
start: error.range.start,
132137
end: error.range.end,
@@ -137,7 +142,7 @@ for (const subdir of fs.readdirSync(FIXTURE_DIR)) {
137142
}
138143
}
139144

140-
documents.doClose(params);
145+
await serverHandle.closeTextDocument(doc.uri);
141146

142147
await snapshot(results, {
143148
file: path.relative(fixtureDir, filename.replace(/\.marko$/, ".md")),

0 commit comments

Comments
 (0)