Skip to content

refactor(language-core): plugin api v2 #3918

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

Merged
merged 1 commit into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 3 additions & 3 deletions packages/language-core/lib/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import useVueSfcStyles from './plugins/vue-sfc-styles';
import useVueSfcTemplate from './plugins/vue-sfc-template';
import useHtmlTemplatePlugin from './plugins/vue-template-html';
import useVueTsx from './plugins/vue-tsx';
import type { VueCompilerOptions, VueLanguagePlugin } from './types';
import { pluginVersion, type VueCompilerOptions, type VueLanguagePlugin } from './types';
import * as CompilerVue2 from './utils/vue2TemplateCompiler';

export function createPluginContext(
Expand Down Expand Up @@ -61,9 +61,9 @@ export function getDefaultVueLanguagePlugins(pluginContext: Parameters<VueLangua
});

return pluginInstances.filter((plugin) => {
const valid = plugin.version >= 2 && plugin.version < 3;
const valid = plugin.version === pluginVersion;
if (!valid) {
console.warn(`Plugin ${JSON.stringify(plugin.name)} API version incompatible, expected 2.x but got ${JSON.stringify(plugin.version)}`);
console.warn(`Plugin ${JSON.stringify(plugin.name)} API version incompatible, expected ${JSON.stringify(pluginVersion)} but got ${JSON.stringify(plugin.version)}`);
}
return valid;
});
Expand Down
4 changes: 2 additions & 2 deletions packages/language-core/lib/plugins/vue-sfc-customblocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ const plugin: VueLanguagePlugin = () => {

version: 2,

getEmbeddedFiles(_fileName, sfc) {
getEmbeddedCodes(_fileName, sfc) {
return sfc.customBlocks.map((customBlock, i) => ({
id: 'customBlock_' + i,
lang: customBlock.lang,
}));
},

resolveEmbeddedFile(_fileName, sfc, embeddedFile) {
resolveEmbeddedCode(_fileName, sfc, embeddedFile) {
if (embeddedFile.id.startsWith('customBlock_')) {
const index = parseInt(embeddedFile.id.slice('customBlock_'.length));
const customBlock = sfc.customBlocks[index];
Expand Down
4 changes: 2 additions & 2 deletions packages/language-core/lib/plugins/vue-sfc-scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const plugin: VueLanguagePlugin = () => {

version: 2,

getEmbeddedFiles(_fileName, sfc) {
getEmbeddedCodes(_fileName, sfc) {
const names: {
id: string;
lang: string;
Expand All @@ -21,7 +21,7 @@ const plugin: VueLanguagePlugin = () => {
return names;
},

resolveEmbeddedFile(_fileName, sfc, embeddedFile) {
resolveEmbeddedCode(_fileName, sfc, embeddedFile) {
const script = embeddedFile.id === 'scriptFormat' ? sfc.script
: embeddedFile.id === 'scriptSetupFormat' ? sfc.scriptSetup
: undefined;
Expand Down
4 changes: 2 additions & 2 deletions packages/language-core/lib/plugins/vue-sfc-styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ const plugin: VueLanguagePlugin = () => {

version: 2,

getEmbeddedFiles(_fileName, sfc) {
getEmbeddedCodes(_fileName, sfc) {
return sfc.styles.map((style, i) => ({
id: 'style_' + i,
lang: style.lang,
}));
},

resolveEmbeddedFile(_fileName, sfc, embeddedFile) {
resolveEmbeddedCode(_fileName, sfc, embeddedFile) {
if (embeddedFile.id.startsWith('style_')) {
const index = parseInt(embeddedFile.id.slice('style_'.length));
const style = sfc.styles[index];
Expand Down
4 changes: 2 additions & 2 deletions packages/language-core/lib/plugins/vue-sfc-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const plugin: VueLanguagePlugin = () => {

version: 2,

getEmbeddedFiles(_fileName, sfc) {
getEmbeddedCodes(_fileName, sfc) {
if (sfc.template) {
return [{
id: 'template',
Expand All @@ -17,7 +17,7 @@ const plugin: VueLanguagePlugin = () => {
return [];
},

resolveEmbeddedFile(_fileName, sfc, embeddedFile) {
resolveEmbeddedCode(_fileName, sfc, embeddedFile) {
if (embeddedFile.id === 'template' && sfc.template) {
embeddedFile.content.push([
sfc.template.content,
Expand Down
8 changes: 4 additions & 4 deletions packages/language-core/lib/plugins/vue-tsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const plugin: VueLanguagePlugin = (ctx) => {
'exactOptionalPropertyTypes',
],

getEmbeddedFiles(fileName, sfc) {
getEmbeddedCodes(fileName, sfc) {

const tsx = useTsx(fileName, sfc);
const files: {
Expand All @@ -40,7 +40,7 @@ const plugin: VueLanguagePlugin = (ctx) => {
return files;
},

resolveEmbeddedFile(fileName, sfc, embeddedFile) {
resolveEmbeddedCode(fileName, sfc, embeddedFile) {

const _tsx = useTsx(fileName, sfc);

Expand All @@ -61,7 +61,7 @@ const plugin: VueLanguagePlugin = (ctx) => {
}
else if (embeddedFile.id === 'template_format') {

embeddedFile.parentFileId = 'template';
embeddedFile.parentCodeId = 'template';

const template = _tsx.generatedTemplate();
if (template) {
Expand All @@ -88,7 +88,7 @@ const plugin: VueLanguagePlugin = (ctx) => {
}
else if (embeddedFile.id === 'template_style') {

embeddedFile.parentFileId = 'template';
embeddedFile.parentCodeId = 'template';

const template = _tsx.generatedTemplate();
if (template) {
Expand Down
10 changes: 6 additions & 4 deletions packages/language-core/lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type * as CompilerDOM from '@vue/compiler-dom';
import type { SFCParseResult } from '@vue/compiler-sfc';
import type * as ts from 'typescript';
import type { VueEmbeddedFile } from './virtualFile/embeddedFile';
import type { VueEmbeddedCode } from './virtualFile/embeddedFile';
import type { CodeInformation, Segment } from '@volar/language-core';

export type { SFCParseResult } from '@vue/compiler-sfc';
Expand Down Expand Up @@ -57,6 +57,8 @@ export interface VueCompilerOptions {
experimentalUseElementAccessInTemplate: boolean;
}

export const pluginVersion = 2;

export type VueLanguagePlugin = (ctx: {
modules: {
typescript: typeof import('typescript');
Expand All @@ -67,7 +69,7 @@ export type VueLanguagePlugin = (ctx: {
codegenStack: boolean;
globalTypesHolder: string | undefined;
}) => {
version: 2;
version: typeof pluginVersion;
name?: string;
order?: number;
requiredCompilerOptions?: string[];
Expand All @@ -76,8 +78,8 @@ export type VueLanguagePlugin = (ctx: {
resolveTemplateCompilerOptions?(options: CompilerDOM.CompilerOptions): CompilerDOM.CompilerOptions;
compileSFCTemplate?(lang: string, template: string, options: CompilerDOM.CompilerOptions): CompilerDOM.CodegenResult | undefined;
updateSFCTemplate?(oldResult: CompilerDOM.CodegenResult, textChange: { start: number, end: number, newText: string; }): CompilerDOM.CodegenResult | undefined;
getEmbeddedFiles?(fileName: string, sfc: Sfc): { id: string; lang: string; }[];
resolveEmbeddedFile?(fileName: string, sfc: Sfc, embeddedFile: VueEmbeddedFile): void;
getEmbeddedCodes?(fileName: string, sfc: Sfc): { id: string; lang: string; }[];
resolveEmbeddedCode?(fileName: string, sfc: Sfc, embeddedFile: VueEmbeddedCode): void;
};

export interface SfcBlock {
Expand Down
20 changes: 10 additions & 10 deletions packages/language-core/lib/virtualFile/computedFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { VirtualCode, buildMappings, buildStacks, resolveCommonLanguageId, toStr
import { computed } from 'computeds';
import type * as ts from 'typescript';
import type { Sfc, SfcBlock, VueLanguagePlugin } from '../types';
import { VueEmbeddedFile } from './embeddedFile';
import { VueEmbeddedCode } from './embeddedFile';

export function computedFiles(
plugins: ReturnType<VueLanguagePlugin>[],
Expand Down Expand Up @@ -56,15 +56,15 @@ export function computedFiles(
codegenStacks,
embeddedCodes: [],
});
console.error('Unable to resolve embedded: ' + file.parentFileId + ' -> ' + file.id);
console.error('Unable to resolve embedded: ' + file.parentCodeId + ' -> ' + file.id);
}

return embeddedCodes;

function consumeRemain() {
for (let i = remain.length - 1; i >= 0; i--) {
const { file, snapshot, mappings, codegenStacks } = remain[i];
if (!file.parentFileId) {
if (!file.parentCodeId) {
embeddedCodes.push({
id: file.id,
languageId: resolveCommonLanguageId(`/dummy.${file.lang}`),
Expand All @@ -77,7 +77,7 @@ export function computedFiles(
remain.splice(i, 1);
}
else {
const parent = findParentStructure(file.parentFileId, embeddedCodes);
const parent = findParentStructure(file.parentCodeId, embeddedCodes);
if (parent) {
parent.embeddedCodes ??= [];
parent.embeddedCodes.push({
Expand Down Expand Up @@ -118,13 +118,13 @@ function computedPluginFiles(
nameToBlock: () => Record<string, SfcBlock>,
codegenStack: boolean
) {
const embeddedFiles: Record<string, () => { file: VueEmbeddedFile; snapshot: ts.IScriptSnapshot; }> = {};
const embeddedFiles: Record<string, () => { file: VueEmbeddedCode; snapshot: ts.IScriptSnapshot; }> = {};
const files = computed(() => {
try {
if (!plugin.getEmbeddedFiles) {
if (!plugin.getEmbeddedCodes) {
return Object.values(embeddedFiles);
}
const fileInfos = plugin.getEmbeddedFiles(fileName, sfc);
const fileInfos = plugin.getEmbeddedCodes(fileName, sfc);
for (const oldId of Object.keys(embeddedFiles)) {
if (!fileInfos.some(file => file.id === oldId)) {
delete embeddedFiles[oldId];
Expand All @@ -134,13 +134,13 @@ function computedPluginFiles(
if (!embeddedFiles[fileInfo.id]) {
embeddedFiles[fileInfo.id] = computed(() => {
const [content, stacks] = codegenStack ? track([]) : [[], []];
const file = new VueEmbeddedFile(fileInfo.id, fileInfo.lang, content, stacks);
const file = new VueEmbeddedCode(fileInfo.id, fileInfo.lang, content, stacks);
for (const plugin of plugins) {
if (!plugin.resolveEmbeddedFile) {
if (!plugin.resolveEmbeddedCode) {
continue;
}
try {
plugin.resolveEmbeddedFile(fileName, sfc, file);
plugin.resolveEmbeddedCode(fileName, sfc, file);
}
catch (e) {
console.error(e);
Expand Down
6 changes: 3 additions & 3 deletions packages/language-core/lib/virtualFile/embeddedFile.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { Mapping, StackNode } from '@volar/language-core';
import type { Code } from '../types';

export class VueEmbeddedFile {
export class VueEmbeddedCode {

public parentFileId?: string;
public parentCodeId?: string;
public linkedCodeMappings: Mapping[] = [];
public embeddedFiles: VueEmbeddedFile[] = [];
public embeddedCodes: VueEmbeddedCode[] = [];

constructor(
public id: string,
Expand Down