Skip to content

Commit

Permalink
feat: upgrade to Volar 2.0 alpha (#701)
Browse files Browse the repository at this point in the history
* updates

* updates

* sync volarjs/volar.js#91

* delete FileKind [skip ci]

* remove FileKind [skip ci]

* fixup [skip ci]

* format [skip ci]

* fix tsx mapping [skip ci]

* remove edge mapping

* updates [skip ci]

* upgrade to volar alpha

* Update Volar

* fileId -> fileName

* format

* updates

* updates

* updates

* use `@volar/test-utils`

* bump services

* update package.json

* bump deps

* fix openUntitledDocument

* Update client.ts

* test: update test setup to not stall

* test: update tests

---------

Co-authored-by: Princesseuh <3019731+Princesseuh@users.noreply.github.com>
  • Loading branch information
johnsoncodehk and Princesseuh committed Dec 15, 2023
1 parent 937b929 commit 7de75d3
Show file tree
Hide file tree
Showing 35 changed files with 996 additions and 1,046 deletions.
8 changes: 4 additions & 4 deletions packages/astro-check/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,20 @@ export async function check(flags: Partial<Flags>): Promise<boolean | void> {
// Dynamically get the list of extensions to watch from the files already included in the project
const checkedExtensions = Array.from(
new Set(
checker.project.languageHost.getScriptFileNames().map((fileName) => path.extname(fileName))
checker.linter.languageHost.getScriptFileNames().map((fileName) => path.extname(fileName))
)
);
createWatcher(workspaceRoot, checkedExtensions)
.on('add', (fileName) => {
checker.project.fileCreated(fileName);
checker.linter.fileCreated(fileName);
update();
})
.on('unlink', (fileName) => {
checker.project.fileDeleted(fileName);
checker.linter.fileDeleted(fileName);
update();
})
.on('change', (fileName) => {
checker.project.fileUpdated(fileName);
checker.linter.fileUpdated(fileName);
update();
});
}
Expand Down
25 changes: 12 additions & 13 deletions packages/language-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,18 @@
"dependencies": {
"@astrojs/compiler": "^2.2.2",
"@jridgewell/sourcemap-codec": "^1.4.15",
"@volar/kit": "~1.10.9",
"@volar/language-core": "~1.10.9",
"@volar/language-server": "~1.10.9",
"@volar/language-service": "~1.10.9",
"@volar/source-map": "~1.10.9",
"@volar/typescript": "~1.10.9",
"@volar/kit": "2.0.0-alpha.6",
"@volar/language-core": "2.0.0-alpha.6",
"@volar/language-server": "2.0.0-alpha.6",
"@volar/language-service": "2.0.0-alpha.6",
"@volar/typescript": "2.0.0-alpha.6",
"fast-glob": "^3.2.12",
"muggle-string": "^0.3.1",
"volar-service-css": "0.0.16",
"volar-service-emmet": "0.0.16",
"volar-service-html": "0.0.16",
"volar-service-prettier": "0.0.16",
"volar-service-typescript": "0.0.16",
"volar-service-typescript-twoslash-queries": "0.0.16",
"volar-service-css": "0.0.21",
"volar-service-emmet": "0.0.21",
"volar-service-html": "0.0.21",
"volar-service-prettier": "0.0.21",
"volar-service-typescript": "0.0.21",
"volar-service-typescript-twoslash-queries": "0.0.21",
"vscode-html-languageservice": "^5.1.0",
"vscode-uri": "^3.0.8"
},
Expand All @@ -51,6 +49,7 @@
"@types/chai": "^4.3.5",
"@types/mocha": "^10.0.1",
"@types/node": "^18.17.8",
"@volar/test-utils": "2.0.0-alpha.6",
"astro": "^3.3.0",
"chai": "^4.3.7",
"mocha": "^10.2.0",
Expand Down
33 changes: 11 additions & 22 deletions packages/language-server/src/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ export interface CheckResult {

export class AstroCheck {
private ts!: typeof import('typescript/lib/tsserverlibrary.js');
public project!: ReturnType<typeof kit.createProject>;
private linter!: ReturnType<typeof kit.createLinter>;
public linter!: ReturnType<(typeof kit)['createTypeScriptChecker']>;

constructor(
private readonly workspacePath: string,
Expand Down Expand Up @@ -61,7 +60,7 @@ export class AstroCheck {
| undefined;
}): Promise<CheckResult> {
const files =
fileNames !== undefined ? fileNames : this.project.languageHost.getScriptFileNames();
fileNames !== undefined ? fileNames : this.linter.languageHost.getScriptFileNames();

const result: CheckResult = {
status: undefined,
Expand Down Expand Up @@ -98,7 +97,7 @@ export class AstroCheck {
console.info(errorText);
}

const fileSnapshot = this.project.languageHost.getScriptSnapshot(file);
const fileSnapshot = this.linter.languageHost.getScriptSnapshot(file);
const fileContent = fileSnapshot?.getText(0, fileSnapshot.getLength());

result.fileResult.push({
Expand Down Expand Up @@ -131,38 +130,28 @@ export class AstroCheck {
const tsconfigPath = this.getTsconfig();

const astroInstall = getAstroInstall([this.workspacePath]);
const config: kit.Config = {
languages: {
astro: getLanguageModule(
typeof astroInstall === 'string' ? undefined : astroInstall,
this.ts
),
svelte: getSvelteLanguageModule(),
vue: getVueLanguageModule(),
},
services: {
typescript: createTypeScriptService(),
astro: createAstroService(),
},
};
const languages = [
getLanguageModule(typeof astroInstall === 'string' ? undefined : astroInstall, this.ts),
getSvelteLanguageModule(),
getVueLanguageModule(),
];
const services = [createTypeScriptService(this.ts), createAstroService(this.ts)];

if (tsconfigPath) {
this.project = kit.createProject(tsconfigPath, [
this.linter = kit.createTypeScriptChecker(languages, services, tsconfigPath, [
{ extension: 'astro', isMixedContent: true, scriptKind: 7 },
{ extension: 'vue', isMixedContent: true, scriptKind: 7 },
{ extension: 'svelte', isMixedContent: true, scriptKind: 7 },
]);
} else {
this.project = kit.createInferredProject(this.workspacePath, () => {
this.linter = kit.createTypeScriptInferredChecker(languages, services, () => {
return fg.sync('**/*.astro', {
cwd: this.workspacePath,
ignore: ['node_modules'],
absolute: true,
});
});
}

this.linter = kit.createLinter(config, this.project.languageHost);
}

private getTsconfig() {
Expand Down
63 changes: 26 additions & 37 deletions packages/language-server/src/core/astro2tsx.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { convertToTSX } from '@astrojs/compiler/sync';
import type { ConvertToTSXOptions, TSXResult } from '@astrojs/compiler/types';
import { decode } from '@jridgewell/sourcemap-codec';
import { FileKind, FileRangeCapabilities, VirtualFile } from '@volar/language-core';
import type { CodeInformation, VirtualFile } from '@volar/language-core';
import { HTMLDocument, TextDocument } from 'vscode-html-languageservice';
import { patchTSX } from './utils.js';

Expand Down Expand Up @@ -59,8 +59,8 @@ function getVirtualFileTSX(
): VirtualFile {
tsx.code = patchTSX(tsx.code, fileName);
const v3Mappings = decode(tsx.map.mappings);
const sourcedDoc = TextDocument.create(fileName, 'astro', 0, input);
const genDoc = TextDocument.create(fileName + '.tsx', 'typescriptreact', 0, tsx.code);
const sourcedDoc = TextDocument.create('file://' + fileName, 'astro', 0, input);
const genDoc = TextDocument.create('file://' + fileName + '.tsx', 'typescriptreact', 0, tsx.code);

const mappings: VirtualFile['mappings'] = [];

Expand Down Expand Up @@ -93,33 +93,37 @@ function getVirtualFileTSX(
const lastMapping = mappings.length ? mappings[mappings.length - 1] : undefined;
if (
lastMapping &&
lastMapping.generatedRange[1] === current.genOffset &&
lastMapping.sourceRange[1] === current.sourceOffset
lastMapping.generatedOffsets[0] + lastMapping.lengths[0] === current.genOffset &&
lastMapping.sourceOffsets[0] + lastMapping.lengths[0] === current.sourceOffset
) {
lastMapping.generatedRange[1] = current.genOffset + length;
lastMapping.sourceRange[1] = current.sourceOffset + length;
lastMapping.lengths[0] += length;
} else {
// Disable features inside script tags. This is a bit annoying to do, I wonder if maybe leaving script tags
// unmapped would be better.
const node = htmlDocument.findNodeAt(current.sourceOffset);
const rangeCapabilities: FileRangeCapabilities =
const rangeCapabilities: CodeInformation =
node.tag !== 'script'
? FileRangeCapabilities.full
? {
verification: true,
completion: true,
semantic: true,
navigation: true,
structure: true,
format: true,
}
: {
verification: false,
completion: false,
definition: false,
diagnostic: false,
displayWithLink: false,
hover: false,
references: false,
referencesCodeLens: false,
rename: false,
semanticTokens: false,
semantic: false,
navigation: false,
structure: false,
format: false,
};

mappings.push({
sourceRange: [current.sourceOffset, current.sourceOffset + length],
generatedRange: [current.genOffset, current.genOffset + length],
sourceOffsets: [current.sourceOffset],
generatedOffsets: [current.genOffset],
lengths: [length],
data: rangeCapabilities,
});
}
Expand All @@ -136,27 +140,12 @@ function getVirtualFileTSX(
}
}

const ast = ts.createSourceFile('/a.tsx', tsx.code, ts.ScriptTarget.ESNext);
if (ast.statements[0]) {
mappings.push({
sourceRange: [0, input.length],
generatedRange: [ast.statements[0].getStart(ast), tsx.code.length],
data: {},
});
}

return {
fileName: fileName + '.tsx',
kind: FileKind.TypeScriptHostFile,
capabilities: {
codeAction: true,
documentFormatting: false,
diagnostic: true,
documentSymbol: true,
inlayHint: true,
foldingRange: true,
languageId: 'typescriptreact',
typescript: {
scriptKind: ts.ScriptKind.TSX,
},
codegenStacks: [],
snapshot: {
getText: (start, end) => tsx.code.substring(start, end),
getLength: () => tsx.code.length,
Expand Down
Loading

0 comments on commit 7de75d3

Please sign in to comment.