Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: upgrade to Volar 2.0 alpha #701

Merged
merged 27 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
aa2ddd4
updates
johnsoncodehk Nov 16, 2023
fc3c13c
updates
johnsoncodehk Nov 17, 2023
51f58dd
sync https://github.com/volarjs/volar.js/pull/91
johnsoncodehk Nov 20, 2023
f6c2c03
delete FileKind [skip ci]
johnsoncodehk Nov 21, 2023
d97ccb8
remove FileKind [skip ci]
johnsoncodehk Nov 21, 2023
3b3ee7a
fixup [skip ci]
johnsoncodehk Dec 4, 2023
2a77570
format [skip ci]
johnsoncodehk Dec 4, 2023
28911af
fix tsx mapping [skip ci]
johnsoncodehk Dec 4, 2023
e987da9
remove edge mapping
johnsoncodehk Dec 4, 2023
1fd1614
updates [skip ci]
johnsoncodehk Dec 5, 2023
7a9129a
upgrade to volar alpha
johnsoncodehk Dec 5, 2023
cdf02c4
Update Volar
johnsoncodehk Dec 7, 2023
b29e005
Merge remote-tracking branch 'upstream/main' into volar-1.12
johnsoncodehk Dec 7, 2023
4e0cc91
fileId -> fileName
johnsoncodehk Dec 7, 2023
dcd042a
format
johnsoncodehk Dec 7, 2023
312a9d1
updates
johnsoncodehk Dec 7, 2023
ace4c76
updates
johnsoncodehk Dec 7, 2023
7e79677
updates
johnsoncodehk Dec 7, 2023
6f02ba7
use `@volar/test-utils`
johnsoncodehk Dec 9, 2023
300baf2
bump services
johnsoncodehk Dec 9, 2023
d96c470
update package.json
johnsoncodehk Dec 12, 2023
b1e14df
bump deps
johnsoncodehk Dec 14, 2023
209337a
Merge remote-tracking branch 'upstream/main' into volar-1.12
johnsoncodehk Dec 14, 2023
01f8b41
fix openUntitledDocument
johnsoncodehk Dec 14, 2023
c6a5816
Update client.ts
johnsoncodehk Dec 14, 2023
ccfb062
test: update test setup to not stall
Princesseuh Dec 14, 2023
dfcea99
test: update tests
Princesseuh Dec 14, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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