Skip to content

Commit

Permalink
feat: upgrade to volar 2.2 alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Mar 24, 2024
1 parent 5e6da11 commit 1b9946c
Show file tree
Hide file tree
Showing 34 changed files with 407 additions and 393 deletions.
2 changes: 1 addition & 1 deletion extensions/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@
"devDependencies": {
"@types/semver": "^7.5.3",
"@types/vscode": "^1.82.0",
"@volar/vscode": "~2.1.5",
"@volar/vscode": "~2.2.0-alpha.0",
"@vue/language-core": "2.0.7",
"@vue/language-server": "2.0.7",
"@vue/typescript-plugin": "2.0.7",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"devDependencies": {
"@lerna-lite/cli": "latest",
"@lerna-lite/publish": "latest",
"@volar/language-service": "~2.1.5",
"@volar/language-service": "~2.2.0-alpha.0",
"@volar/tsl-config": "latest",
"tsl": "latest",
"typescript": "latest",
Expand Down
30 changes: 13 additions & 17 deletions packages/component-meta/lib/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type * as ts from 'typescript';
import * as path from 'path-browserify';
import { code as typeHelpersCode } from 'vue-component-type-helpers';
import { code as vue2TypeHelpersCode } from 'vue-component-type-helpers/vue2';
import { createLanguage } from '@volar/typescript';
import { createTypeScriptLanguage } from '@volar/typescript';

import type {
MetaCheckerOptions,
Expand Down Expand Up @@ -71,7 +71,9 @@ function createCheckerWorker(
let projectVersion = 0;

const scriptSnapshots = new Map<string, ts.IScriptSnapshot>();
const _host: vue.TypeScriptProjectHost = {
const projectHost: vue.TypeScriptProjectHost = {
...ts.sys,
configFileName,
getCurrentDirectory: () => rootPath,
getProjectVersion: () => projectVersion.toString(),
getCompilationSettings: () => parsedCommandLine.options,
Expand All @@ -92,10 +94,12 @@ function createCheckerWorker(
}
return vue.resolveCommonLanguageId(fileName);
},
scriptIdToFileName: id => id,
fileNameToScriptId: id => id,
};

return {
...baseCreate(ts, configFileName, _host, parsedCommandLine.vueOptions, checkerOptions, globalComponentName),
...baseCreate(ts, projectHost, parsedCommandLine.vueOptions, checkerOptions, globalComponentName),
updateFile(fileName: string, text: string) {
fileName = fileName.replace(windowsPathReg, '/');
scriptSnapshots.set(fileName, ts.ScriptSnapshot.fromString(text));
Expand All @@ -120,7 +124,6 @@ function createCheckerWorker(

export function baseCreate(
ts: typeof import('typescript'),
configFileName: string | undefined,
host: vue.TypeScriptProjectHost,
vueCompilerOptions: vue.VueCompilerOptions,
checkerOptions: MetaCheckerOptions,
Expand Down Expand Up @@ -174,16 +177,10 @@ export function baseCreate(
host.getCompilationSettings(),
vueCompilerOptions,
);
const language = createLanguage(
const language = createTypeScriptLanguage(
ts,
ts.sys,
[vueLanguagePlugin],
configFileName,
host,
{
fileIdToFileName: id => id,
fileNameToFileId: id => id,
},
);
const { languageServiceHost } = language.typescript!;
const tsLs = ts.createLanguageService(languageServiceHost);
Expand Down Expand Up @@ -326,7 +323,7 @@ ${vueCompilerOptions.target < 3 ? vue2TypeHelpersCode : typeHelpersCode}
const printer = ts.createPrinter(checkerOptions.printer);
const snapshot = host.getScriptSnapshot(componentPath)!;

const vueFile = language.files.get(componentPath)?.generated?.code;
const vueFile = language.scripts.get(componentPath)?.generated?.root;
const vueDefaults = vueFile && exportName === 'default'
? (vueFile instanceof vue.VueGeneratedCode ? readVueComponentDefaultProps(vueFile, printer, ts, vueCompilerOptions) : {})
: {};
Expand Down Expand Up @@ -473,7 +470,7 @@ function createSchemaResolvers(
symbolNode: ts.Expression,
{ rawType, schema: options, noDeclarations }: MetaCheckerOptions,
ts: typeof import('typescript'),
context: vue.LanguageContext,
language: vue.Language,
) {
const visited = new Set<ts.Type>();

Expand Down Expand Up @@ -665,12 +662,11 @@ function createSchemaResolvers(
}
function getDeclaration(declaration: ts.Declaration): Declaration | undefined {
const fileName = declaration.getSourceFile().fileName;
const sourceFile = context.files.get(fileName);
const sourceFile = language.scripts.get(fileName);
if (sourceFile?.generated) {
const script = sourceFile.generated.languagePlugin.typescript?.getScript(sourceFile.generated.code);
const script = sourceFile.generated.languagePlugin.typescript?.getServiceScript(sourceFile.generated.root);
if (script) {
const maps = context.files.getMaps(script.code);
for (const [source, [_, map]] of maps) {
for (const [source, [_, map]] of language.maps.forEach(script.code)) {
const start = map.getSourceOffset(declaration.getStart());
const end = map.getSourceOffset(declaration.getEnd());
if (start && end) {
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 @@ -12,7 +12,7 @@
"directory": "packages/component-meta"
},
"dependencies": {
"@volar/typescript": "~2.1.5",
"@volar/typescript": "~2.2.0-alpha.0",
"@vue/language-core": "2.0.7",
"path-browserify": "^1.0.1",
"vue-component-type-helpers": "2.0.7"
Expand Down
51 changes: 26 additions & 25 deletions packages/language-core/lib/languageModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,37 +113,38 @@ export function createVueLanguagePlugin(
code.update(snapshot);
return code;
},
disposeVirtualCode(fileId, code, files) {
const isGlobalTypesHolder = code.fileName === pluginContext.globalTypesHolder;
const fileRegistry = getFileRegistry(isGlobalTypesHolder);
fileRegistry.delete(fileId);
if (isGlobalTypesHolder) {
pluginContext.globalTypesHolder = undefined;
const fileRegistry2 = getFileRegistry(false);
for (const [fileId, code] of fileRegistry2) {
if (isValidGlobalTypesHolder(code.fileName)) {
pluginContext.globalTypesHolder = code.fileName;
fileRegistry2.delete(fileId);
// force dirty
files?.delete(fileId);
files?.set(
fileId,
code.languageId,
code.snapshot,
);
break;
}
}
}
},
// TODO: when global types holder deleted, move global types to another file
// disposeVirtualCode(fileId, code) {
// const isGlobalTypesHolder = code.fileName === pluginContext.globalTypesHolder;
// const fileRegistry = getFileRegistry(isGlobalTypesHolder);
// fileRegistry.delete(fileId);
// if (isGlobalTypesHolder) {
// pluginContext.globalTypesHolder = undefined;
// const fileRegistry2 = getFileRegistry(false);
// for (const [fileId, code] of fileRegistry2) {
// if (isValidGlobalTypesHolder(code.fileName)) {
// pluginContext.globalTypesHolder = code.fileName;
// fileRegistry2.delete(fileId);
// // force dirty
// files?.delete(fileId);
// files?.set(
// fileId,
// code.languageId,
// code.snapshot,
// );
// break;
// }
// }
// }
// },
typescript: {
extraFileExtensions: vueCompilerOptions.extensions.map<ts.FileExtensionInfo>(ext => ({
extension: ext.slice(1),
isMixedContent: true,
scriptKind: 7 satisfies ts.ScriptKind.Deferred,
})),
getScript(rootVirtualCode) {
for (const code of forEachEmbeddedCode(rootVirtualCode)) {
getServiceScript(root) {
for (const code of forEachEmbeddedCode(root)) {
if (code.id.startsWith('script_')) {
const lang = code.id.substring('script_'.length);
return {
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 @@ -12,7 +12,7 @@
"directory": "packages/language-core"
},
"dependencies": {
"@volar/language-core": "~2.1.5",
"@volar/language-core": "~2.2.0-alpha.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-plugin-pug/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"@vue/language-core": "2.0.7"
},
"dependencies": {
"@volar/source-map": "~2.1.5",
"volar-service-pug": "0.0.34"
"@volar/source-map": "~2.2.0-alpha.0",
"volar-service-pug": "0.0.35"
}
}
4 changes: 2 additions & 2 deletions packages/language-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"directory": "packages/language-server"
},
"dependencies": {
"@volar/language-core": "~2.1.5",
"@volar/language-server": "~2.1.5",
"@volar/language-core": "~2.2.0-alpha.0",
"@volar/language-server": "~2.2.0-alpha.0",
"@vue/language-core": "2.0.7",
"@vue/language-service": "2.0.7",
"@vue/typescript-plugin": "2.0.7",
Expand Down
92 changes: 46 additions & 46 deletions packages/language-service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,29 @@ export * from '@vue/language-core';
export * from './lib/ideFeatures/nameCasing';
export * from './lib/types';

import type { ServiceContext, ServiceEnvironment, ServicePlugin } from '@volar/language-service';
import type { ServiceContext, ServiceEnvironment, LanguageServicePlugin } from '@volar/language-service';
import type { VueCompilerOptions } from './lib/types';

import { create as createEmmetServicePlugin } from 'volar-service-emmet';
import { create as createJsonServicePlugin } from 'volar-service-json';
import { create as createPugFormatServicePlugin } from 'volar-service-pug-beautify';
import { create as createTypeScriptServicePlugins } from 'volar-service-typescript';
import { create as createTypeScriptTwoslashQueriesServicePlugin } from 'volar-service-typescript-twoslash-queries';
import { create as createTypeScriptDocCommentTemplateServicePlugin } from 'volar-service-typescript/lib/plugins/docCommentTemplate';
import { create as createTypeScriptSyntacticServicePlugin } from 'volar-service-typescript/lib/plugins/syntactic';
import { create as createCssServicePlugin } from './lib/plugins/css';
import { create as createVueAutoDotValueServicePlugin } from './lib/plugins/vue-autoinsert-dotvalue';
import { create as createVueAutoWrapParenthesesServicePlugin } from './lib/plugins/vue-autoinsert-parentheses';
import { create as createVueAutoAddSpaceServicePlugin } from './lib/plugins/vue-autoinsert-space';
import { create as createVueReferencesCodeLensServicePlugin } from './lib/plugins/vue-codelens-references';
import { create as createVueDirectiveCommentsServicePlugin } from './lib/plugins/vue-directive-comments';
import { create as createVueDocumentDropServicePlugin } from './lib/plugins/vue-document-drop';
import { create as createVueExtractFileServicePlugin } from './lib/plugins/vue-extract-file';
import { create as createVueSfcServicePlugin } from './lib/plugins/vue-sfc';
import { create as createVueTemplateServicePlugin } from './lib/plugins/vue-template';
import { create as createVueToggleVBindServicePlugin } from './lib/plugins/vue-toggle-v-bind-codeaction';
import { create as createVueTwoslashQueriesServicePlugin } from './lib/plugins/vue-twoslash-queries';
import { create as createVueVisualizeHiddenCallbackParamServicePlugin } from './lib/plugins/vue-visualize-hidden-callback-param';
import { create as createEmmetPlugin } from 'volar-service-emmet';
import { create as createJsonPlugin } from 'volar-service-json';
import { create as createPugFormatPlugin } from 'volar-service-pug-beautify';
import { create as createTypeScriptPlugins } from 'volar-service-typescript';
import { create as createTypeScriptTwoslashQueriesPlugin } from 'volar-service-typescript-twoslash-queries';
import { create as createTypeScriptDocCommentTemplatePlugin } from 'volar-service-typescript/lib/plugins/docCommentTemplate';
import { create as createTypeScriptSyntacticPlugin } from 'volar-service-typescript/lib/plugins/syntactic';
import { create as createCssPlugin } from './lib/plugins/css';
import { create as createVueAutoDotValuePlugin } from './lib/plugins/vue-autoinsert-dotvalue';
import { create as createVueAutoWrapParenthesesPlugin } from './lib/plugins/vue-autoinsert-parentheses';
import { create as createVueAutoAddSpacePlugin } from './lib/plugins/vue-autoinsert-space';
import { create as createVueReferencesCodeLensPlugin } from './lib/plugins/vue-codelens-references';
import { create as createVueDirectiveCommentsPlugin } from './lib/plugins/vue-directive-comments';
import { create as createVueDocumentDropPlugin } from './lib/plugins/vue-document-drop';
import { create as createVueExtractFilePlugin } from './lib/plugins/vue-extract-file';
import { create as createVueSfcPlugin } from './lib/plugins/vue-sfc';
import { create as createVueTemplatePlugin } from './lib/plugins/vue-template';
import { create as createVueToggleVBindPlugin } from './lib/plugins/vue-toggle-v-bind-codeaction';
import { create as createVueTwoslashQueriesPlugin } from './lib/plugins/vue-twoslash-queries';
import { create as createVueVisualizeHiddenCallbackParamPlugin } from './lib/plugins/vue-visualize-hidden-callback-param';

import { decorateLanguageServiceForVue } from '@vue/typescript-plugin/lib/common';
import { collectExtractProps } from '@vue/typescript-plugin/lib/requests/collectExtractProps';
Expand All @@ -38,10 +38,10 @@ export function createVueServicePlugins(
getVueOptions: (env: ServiceEnvironment) => VueCompilerOptions,
getTsPluginClient = createDefaultGetTsPluginClient(ts, getVueOptions),
hybridMode = false,
): ServicePlugin[] {
const plugins: ServicePlugin[] = [];
): LanguageServicePlugin[] {
const plugins: LanguageServicePlugin[] = [];
if (!hybridMode) {
plugins.push(...createTypeScriptServicePlugins(ts));
plugins.push(...createTypeScriptPlugins(ts));
for (let i = 0; i < plugins.length; i++) {
const plugin = plugins[i];
if (plugin.name === 'typescript-semantic') {
Expand All @@ -54,7 +54,7 @@ export function createVueServicePlugins(
}
const languageService = (created.provide as import('volar-service-typescript').Provide)['typescript/languageService']();
const vueOptions = getVueOptions(context.env);
decorateLanguageServiceForVue(context.language.files, languageService, vueOptions, ts, false);
decorateLanguageServiceForVue(context.language, languageService, vueOptions, ts, false);
return created;
},
};
Expand All @@ -64,29 +64,29 @@ export function createVueServicePlugins(
}
else {
plugins.push(
createTypeScriptSyntacticServicePlugin(ts),
createTypeScriptDocCommentTemplateServicePlugin(ts),
createTypeScriptSyntacticPlugin(ts),
createTypeScriptDocCommentTemplatePlugin(ts),
);
}
plugins.push(
createTypeScriptTwoslashQueriesServicePlugin(ts),
createCssServicePlugin(),
createPugFormatServicePlugin(),
createJsonServicePlugin(),
createVueTemplateServicePlugin('html', ts, getVueOptions, getTsPluginClient),
createVueTemplateServicePlugin('pug', ts, getVueOptions, getTsPluginClient),
createVueSfcServicePlugin(),
createVueTwoslashQueriesServicePlugin(ts, getTsPluginClient),
createVueReferencesCodeLensServicePlugin(),
createVueDocumentDropServicePlugin(ts),
createVueAutoDotValueServicePlugin(ts, getTsPluginClient),
createVueAutoWrapParenthesesServicePlugin(ts),
createVueAutoAddSpaceServicePlugin(),
createVueVisualizeHiddenCallbackParamServicePlugin(),
createVueDirectiveCommentsServicePlugin(),
createVueExtractFileServicePlugin(ts, getTsPluginClient),
createVueToggleVBindServicePlugin(ts),
createEmmetServicePlugin(),
createTypeScriptTwoslashQueriesPlugin(ts),
createCssPlugin(),
createPugFormatPlugin(),
createJsonPlugin(),
createVueTemplatePlugin('html', ts, getVueOptions, getTsPluginClient),
createVueTemplatePlugin('pug', ts, getVueOptions, getTsPluginClient),
createVueSfcPlugin(),
createVueTwoslashQueriesPlugin(ts, getTsPluginClient),
createVueReferencesCodeLensPlugin(),
createVueDocumentDropPlugin(ts),
createVueAutoDotValuePlugin(ts, getTsPluginClient),
createVueAutoWrapParenthesesPlugin(ts),
createVueAutoAddSpacePlugin(),
createVueVisualizeHiddenCallbackParamPlugin(),
createVueDirectiveCommentsPlugin(),
createVueExtractFilePlugin(ts, getTsPluginClient),
createVueToggleVBindPlugin(ts),
createEmmetPlugin(),
);
return plugins;
}
Expand All @@ -101,7 +101,7 @@ export function createDefaultGetTsPluginClient(
}
const requestContext = {
typescript: ts,
files: context.language.files,
language: context.language,
languageService: context.inject<(import('volar-service-typescript').Provide), 'typescript/languageService'>('typescript/languageService'),
vueOptions: getVueOptions(context.env),
isTsPlugin: false,
Expand Down
10 changes: 5 additions & 5 deletions packages/language-service/lib/ideFeatures/nameCasing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ export async function convertTagName(
tsPluginClient: typeof import('@vue/typescript-plugin/lib/client') | undefined,
) {

const sourceFile = context.language.files.get(uri);
const sourceFile = context.language.scripts.get(uri);
if (!sourceFile) {
return;
}

const rootCode = sourceFile?.generated?.code;
const rootCode = sourceFile?.generated?.root;
if (!(rootCode instanceof VueGeneratedCode)) {
return;
}
Expand Down Expand Up @@ -61,12 +61,12 @@ export async function convertAttrName(
tsPluginClient?: typeof import('@vue/typescript-plugin/lib/client'),
) {

const sourceFile = context.language.files.get(uri);
const sourceFile = context.language.scripts.get(uri);
if (!sourceFile) {
return;
}

const rootCode = sourceFile?.generated?.code;
const rootCode = sourceFile?.generated?.root;
if (!(rootCode instanceof VueGeneratedCode)) {
return;
}
Expand Down Expand Up @@ -137,7 +137,7 @@ export async function detect(
attr: AttrNameCasing[],
}> {

const rootFile = context.language.files.get(uri)?.generated?.code;
const rootFile = context.language.scripts.get(uri)?.generated?.root;
if (!(rootFile instanceof VueGeneratedCode)) {
return {
tag: [],
Expand Down
Loading

0 comments on commit 1b9946c

Please sign in to comment.