Skip to content

Commit

Permalink
feat: upgrade to Volar 2.1 (#3906)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk authored Feb 28, 2024
1 parent 549d036 commit cd3532d
Show file tree
Hide file tree
Showing 24 changed files with 319 additions and 320 deletions.
85 changes: 13 additions & 72 deletions extensions/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -283,67 +283,6 @@
"type": "object",
"title": "Volar",
"properties": {
"volar.format.initialIndent": {
"type": "object",
"description": "Whether to have initial indent.",
"default": {
"html": true
},
"properties": {
"html": {
"type": "boolean",
"default": true
},
"typescript": {
"type": "boolean",
"default": false
},
"javascript": {
"type": "boolean",
"default": false
},
"typescriptreact": {
"type": "boolean",
"default": false
},
"javascriptreact": {
"type": "boolean",
"default": false
},
"css": {
"type": "boolean",
"default": false
},
"scss": {
"type": "boolean",
"default": false
},
"less": {
"type": "boolean",
"default": false
},
"sass": {
"type": "boolean",
"default": false
},
"jade": {
"type": "boolean",
"default": false
},
"json": {
"type": "boolean",
"default": false
},
"jsonc": {
"type": "boolean",
"default": false
},
"json5": {
"type": "boolean",
"default": false
}
}
},
"vue.trace.server": {
"scope": "window",
"type": "string",
Expand Down Expand Up @@ -406,11 +345,6 @@
"default": null,
"description": "Set --max-old-space-size option on server process. If you have problem on frequently \"Request textDocument/** failed.\" error, try setting higher memory(MB) on it."
},
"vue.server.reverseConfigFilePriority": {
"type": "boolean",
"default": false,
"description": "Reverse priority for tsconfig pickup."
},
"vue.server.additionalExtensions": {
"type": "array",
"items": {
Expand All @@ -419,11 +353,6 @@
"default": [ ],
"description": "List any additional file extensions that should be processed as Vue files (requires restart)."
},
"vue.server.fullCompletionList": {
"type": "boolean",
"default": false,
"description": "Enable this option if you want to get complete CompletionList in language client. (Disable for better performance)"
},
"vue.doctor.status": {
"type": "boolean",
"default": true,
Expand Down Expand Up @@ -537,6 +466,18 @@
"type": "boolean",
"default": false,
"description": "Show inlay hints for component options wrapper for type support."
},
"vue.format.template.initialIndent": {
"type": "boolean",
"default": true
},
"vue.format.style.initialIndent": {
"type": "boolean",
"default": false
},
"vue.format.script.initialIndent": {
"type": "boolean",
"default": false
}
}
},
Expand Down Expand Up @@ -643,7 +584,7 @@
"devDependencies": {
"@types/semver": "^7.5.3",
"@types/vscode": "^1.82.0",
"@volar/vscode": "~2.0.2",
"@volar/vscode": "~2.1.0",
"@vue/language-core": "1.8.27",
"@vue/language-server": "1.8.27",
"esbuild": "latest",
Expand Down
38 changes: 18 additions & 20 deletions extensions/vscode/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,25 +143,23 @@ export function getDocumentSelector(): lsp.DocumentFilter[] {

async function getInitializationOptions(
context: vscode.ExtensionContext,
options: VueInitializationOptions = {},
) {
// volar
options.diagnosticModel = config.server.diagnosticModel === 'pull' ? DiagnosticModel.Pull : DiagnosticModel.Push;
options.typescript = { tsdk: (await getTsdk(context)).tsdk };
options.reverseConfigFilePriority = config.server.reverseConfigFilePriority;
options.maxFileSize = config.server.maxFileSize;
options.semanticTokensLegend = {
tokenTypes: ['component'],
tokenModifiers: [],
): Promise<VueInitializationOptions> {
return {
// volar
diagnosticModel: config.server.diagnosticModel === 'pull' ? DiagnosticModel.Pull : DiagnosticModel.Push,
typescript: { tsdk: (await getTsdk(context)).tsdk },
maxFileSize: config.server.maxFileSize,
semanticTokensLegend: {
tokenTypes: ['component'],
tokenModifiers: [],
},
vue: {
hybridMode: true,
additionalExtensions: [
...config.server.additionalExtensions,
...!config.server.petiteVue.supportHtmlFile ? [] : ['html'],
...!config.server.vitePress.supportMdFile ? [] : ['md'],
],
},
};
options.fullCompletionList = config.server.fullCompletionList;
options.vue = {
hybridMode: true,
additionalExtensions: [
...config.server.additionalExtensions,
...!config.server.petiteVue.supportHtmlFile ? [] : ['html'],
...!config.server.vitePress.supportMdFile ? [] : ['md'],
],
};
return options;
}
2 changes: 0 additions & 2 deletions extensions/vscode/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ export const config = {
runtime: 'node' | 'bun';
maxOldSpaceSize: number;
maxFileSize: number;
reverseConfigFilePriority: boolean;
diagnosticModel: 'push' | 'pull';
additionalExtensions: string[];
fullCompletionList: boolean;
vitePress: {
supportMdFile: boolean;
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"devDependencies": {
"@lerna-lite/cli": "latest",
"@lerna-lite/publish": "latest",
"@volar/language-service": "~2.0.2",
"@volar/language-service": "~2.1.0",
"typescript": "latest",
"vite": "latest",
"vitest": "latest"
Expand Down
2 changes: 1 addition & 1 deletion packages/component-meta/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"directory": "packages/component-meta"
},
"dependencies": {
"@volar/typescript": "~2.0.2",
"@volar/typescript": "~2.1.0",
"@vue/language-core": "1.8.27",
"path-browserify": "^1.0.1",
"vue-component-type-helpers": "1.8.27"
Expand Down
2 changes: 1 addition & 1 deletion packages/language-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"directory": "packages/language-core"
},
"dependencies": {
"@volar/language-core": "~2.0.2",
"@volar/language-core": "~2.1.0",
"@vue/compiler-dom": "^3.4.0",
"@vue/shared": "^3.4.0",
"computeds": "^0.0.1",
Expand Down
4 changes: 2 additions & 2 deletions packages/language-core/src/generators/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,8 +480,8 @@ export function* generate(
content,
start,
lines.length <= 1 ? formatBrackets.curly : [
formatBrackets.curly[0],
lines[lines.length - 1].trim() === '' ? '' : formatBrackets.curly[1],
lines[0].trim() === '' ? '(' : formatBrackets.curly[0],
lines[lines.length - 1].trim() === '' ? ')' : formatBrackets.curly[1],
],
);
}
Expand Down
3 changes: 2 additions & 1 deletion packages/language-core/src/virtualFile/computedFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export function computedFiles(
else {
const parent = findParentStructure(file.parentFileId, embeddedCodes);
if (parent) {
parent.embeddedCodes ??= [];
parent.embeddedCodes.push({
id: file.id,
languageId: resolveCommonLanguageId(`/dummy.${file.lang}`),
Expand All @@ -98,7 +99,7 @@ export function computedFiles(
if (child.id === id) {
return child;
}
let parent = findParentStructure(id, child.embeddedCodes);
let parent = findParentStructure(id, child.embeddedCodes ?? []);
if (parent) {
return parent;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/language-plugin-pug/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"@vue/language-core": "1.8.27"
},
"dependencies": {
"@volar/source-map": "~2.0.2",
"volar-service-pug": "0.0.28"
"@volar/source-map": "~2.1.0",
"volar-service-pug": "0.0.31"
}
}
4 changes: 2 additions & 2 deletions packages/language-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"directory": "packages/language-server"
},
"dependencies": {
"@volar/language-core": "~2.0.2",
"@volar/language-server": "~2.0.2",
"@volar/language-core": "~2.1.0",
"@volar/language-server": "~2.1.0",
"@vue/language-core": "1.8.27",
"@vue/language-service": "1.8.27",
"vscode-languageserver-protocol": "^3.17.5",
Expand Down
35 changes: 16 additions & 19 deletions packages/language-server/src/nodeServer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createConnection, createServer, createSimpleProjectProvider, createTypeScriptProjectProvider } from '@volar/language-server/node';
import { createConnection, createServer, createSimpleProjectProviderFactory, createTypeScriptProjectProviderFactory, loadTsdkByPath } from '@volar/language-server/node';
import { ServerProject } from '@volar/language-server';
import * as vue2 from '@vue/language-core';
import { VueCompilerOptions } from '@vue/language-core';
Expand All @@ -13,11 +13,16 @@ const server = createServer(connection);
const checkers = new WeakMap<ServerProject, componentMeta.ComponentMetaChecker>();
const envToVueOptions = new WeakMap<vue.ServiceEnvironment, VueCompilerOptions>();

let tsdk: ReturnType<typeof loadTsdkByPath>;

connection.listen();

connection.onInitialize(params => {

const options: VueInitializationOptions = params.initializationOptions;

tsdk = loadTsdkByPath(options.typescript.tsdk!, params.locale);

const vueFileExtensions: string[] = ['vue'];

if (options.vue?.additionalExtensions) {
Expand All @@ -28,20 +33,20 @@ connection.onInitialize(params => {

return server.initialize(
params,
options.vue?.hybridMode ? createSimpleProjectProvider : createTypeScriptProjectProvider,
options.vue?.hybridMode
? createSimpleProjectProviderFactory()
: createTypeScriptProjectProviderFactory(tsdk.typescript, tsdk.diagnosticMessages),
{
watchFileExtensions: ['js', 'cjs', 'mjs', 'ts', 'cts', 'mts', 'jsx', 'tsx', 'json', ...vueFileExtensions],
getServicePlugins() {
const ts = getTsLib();
const services = vue.resolveServices({}, ts, env => envToVueOptions.get(env)!);
const services = vue.resolveServices({}, tsdk.typescript, env => envToVueOptions.get(env)!);

return Object.values(services);
},
async getLanguagePlugins(serviceEnv, projectContext) {
const ts = getTsLib();
const [commandLine, vueOptions] = await parseCommandLine();
const resolvedVueOptions = vue.resolveVueCompilerOptions(vueOptions);
const languages = vue.resolveLanguages({}, ts, serviceEnv.typescript!.uriToFileName, commandLine?.options ?? {}, resolvedVueOptions, options.codegenStack);
const languages = vue.resolveLanguages({}, tsdk.typescript, serviceEnv.typescript!.uriToFileName, commandLine?.options ?? {}, resolvedVueOptions, options.codegenStack);

envToVueOptions.set(serviceEnv, resolvedVueOptions);

Expand All @@ -62,7 +67,7 @@ connection.onInitialize(params => {
while (sysVersion !== newSysVersion) {
sysVersion = newSysVersion;
if (projectContext.typescript.configFileName) {
commandLine = vue2.createParsedCommandLine(ts, sys, projectContext.typescript.configFileName);
commandLine = vue2.createParsedCommandLine(tsdk.typescript, sys, projectContext.typescript.configFileName);
}
newSysVersion = await sys.sync();
}
Expand Down Expand Up @@ -99,14 +104,14 @@ connection.onRequest(ParseSFCRequest.type, params => {
connection.onRequest(DetectNameCasingRequest.type, async params => {
const languageService = await getService(params.textDocument.uri);
if (languageService) {
return nameCasing.detect(getTsLib(), languageService.context, params.textDocument.uri, envToVueOptions.get(languageService.context.env)!);
return nameCasing.detect(tsdk.typescript, languageService.context, params.textDocument.uri, envToVueOptions.get(languageService.context.env)!);
}
});

connection.onRequest(GetConvertTagCasingEditsRequest.type, async params => {
const languageService = await getService(params.textDocument.uri);
if (languageService) {
return nameCasing.convertTagName(getTsLib(), languageService.context, params.textDocument.uri, params.casing, envToVueOptions.get(languageService.context.env)!);
return nameCasing.convertTagName(tsdk.typescript, languageService.context, params.textDocument.uri, params.casing, envToVueOptions.get(languageService.context.env)!);
}
});

Expand All @@ -115,7 +120,7 @@ connection.onRequest(GetConvertAttrCasingEditsRequest.type, async params => {
if (languageService) {
const vueOptions = envToVueOptions.get(languageService.context.env);
if (vueOptions) {
return nameCasing.convertAttrName(getTsLib(), languageService.context, params.textDocument.uri, params.casing, envToVueOptions.get(languageService.context.env)!);
return nameCasing.convertAttrName(tsdk.typescript, languageService.context, params.textDocument.uri, params.casing, envToVueOptions.get(languageService.context.env)!);
}
}
});
Expand All @@ -128,7 +133,7 @@ connection.onRequest(GetComponentMeta.type, async params => {
let checker = checkers.get(project);
if (!checker) {
checker = componentMeta.baseCreate(
getTsLib(),
tsdk.typescript,
langaugeService.context.language.typescript!.configFileName,
langaugeService.context.language.typescript!.projectHost,
envToVueOptions.get(langaugeService.context.env)!,
Expand All @@ -140,14 +145,6 @@ connection.onRequest(GetComponentMeta.type, async params => {
return checker?.getComponentMeta(langaugeService.context.env.typescript!.uriToFileName(params.uri));
});

function getTsLib() {
const ts = server.modules.typescript;
if (!ts) {
throw 'typescript not found';
}
return ts;
}

async function getService(uri: string) {
return (await server.projects.getProject(uri)).getLanguageService();
}
4 changes: 4 additions & 0 deletions packages/language-server/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { InitializationOptions } from "@volar/language-server";

export type VueInitializationOptions = InitializationOptions & {
typescript: {
tsdk?: string;
tsdkUrl?: string;
}
vue?: {
/**
* @example ['vue1', 'vue2']
Expand Down
Loading

0 comments on commit cd3532d

Please sign in to comment.