Skip to content

Commit d8d65f0

Browse files
refactor(language-core): plugin api v2 (#3918)
1 parent d09ab69 commit d8d65f0

File tree

9 files changed

+34
-32
lines changed

9 files changed

+34
-32
lines changed

packages/language-core/lib/plugins.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import useVueSfcStyles from './plugins/vue-sfc-styles';
99
import useVueSfcTemplate from './plugins/vue-sfc-template';
1010
import useHtmlTemplatePlugin from './plugins/vue-template-html';
1111
import useVueTsx from './plugins/vue-tsx';
12-
import type { VueCompilerOptions, VueLanguagePlugin } from './types';
12+
import { pluginVersion, type VueCompilerOptions, type VueLanguagePlugin } from './types';
1313
import * as CompilerVue2 from './utils/vue2TemplateCompiler';
1414

1515
export function createPluginContext(
@@ -61,9 +61,9 @@ export function getDefaultVueLanguagePlugins(pluginContext: Parameters<VueLangua
6161
});
6262

6363
return pluginInstances.filter((plugin) => {
64-
const valid = plugin.version >= 2 && plugin.version < 3;
64+
const valid = plugin.version === pluginVersion;
6565
if (!valid) {
66-
console.warn(`Plugin ${JSON.stringify(plugin.name)} API version incompatible, expected 2.x but got ${JSON.stringify(plugin.version)}`);
66+
console.warn(`Plugin ${JSON.stringify(plugin.name)} API version incompatible, expected ${JSON.stringify(pluginVersion)} but got ${JSON.stringify(plugin.version)}`);
6767
}
6868
return valid;
6969
});

packages/language-core/lib/plugins/vue-sfc-customblocks.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ const plugin: VueLanguagePlugin = () => {
77

88
version: 2,
99

10-
getEmbeddedFiles(_fileName, sfc) {
10+
getEmbeddedCodes(_fileName, sfc) {
1111
return sfc.customBlocks.map((customBlock, i) => ({
1212
id: 'customBlock_' + i,
1313
lang: customBlock.lang,
1414
}));
1515
},
1616

17-
resolveEmbeddedFile(_fileName, sfc, embeddedFile) {
17+
resolveEmbeddedCode(_fileName, sfc, embeddedFile) {
1818
if (embeddedFile.id.startsWith('customBlock_')) {
1919
const index = parseInt(embeddedFile.id.slice('customBlock_'.length));
2020
const customBlock = sfc.customBlocks[index];

packages/language-core/lib/plugins/vue-sfc-scripts.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const plugin: VueLanguagePlugin = () => {
77

88
version: 2,
99

10-
getEmbeddedFiles(_fileName, sfc) {
10+
getEmbeddedCodes(_fileName, sfc) {
1111
const names: {
1212
id: string;
1313
lang: string;
@@ -21,7 +21,7 @@ const plugin: VueLanguagePlugin = () => {
2121
return names;
2222
},
2323

24-
resolveEmbeddedFile(_fileName, sfc, embeddedFile) {
24+
resolveEmbeddedCode(_fileName, sfc, embeddedFile) {
2525
const script = embeddedFile.id === 'scriptFormat' ? sfc.script
2626
: embeddedFile.id === 'scriptSetupFormat' ? sfc.scriptSetup
2727
: undefined;

packages/language-core/lib/plugins/vue-sfc-styles.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ const plugin: VueLanguagePlugin = () => {
77

88
version: 2,
99

10-
getEmbeddedFiles(_fileName, sfc) {
10+
getEmbeddedCodes(_fileName, sfc) {
1111
return sfc.styles.map((style, i) => ({
1212
id: 'style_' + i,
1313
lang: style.lang,
1414
}));
1515
},
1616

17-
resolveEmbeddedFile(_fileName, sfc, embeddedFile) {
17+
resolveEmbeddedCode(_fileName, sfc, embeddedFile) {
1818
if (embeddedFile.id.startsWith('style_')) {
1919
const index = parseInt(embeddedFile.id.slice('style_'.length));
2020
const style = sfc.styles[index];

packages/language-core/lib/plugins/vue-sfc-template.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const plugin: VueLanguagePlugin = () => {
77

88
version: 2,
99

10-
getEmbeddedFiles(_fileName, sfc) {
10+
getEmbeddedCodes(_fileName, sfc) {
1111
if (sfc.template) {
1212
return [{
1313
id: 'template',
@@ -17,7 +17,7 @@ const plugin: VueLanguagePlugin = () => {
1717
return [];
1818
},
1919

20-
resolveEmbeddedFile(_fileName, sfc, embeddedFile) {
20+
resolveEmbeddedCode(_fileName, sfc, embeddedFile) {
2121
if (embeddedFile.id === 'template' && sfc.template) {
2222
embeddedFile.content.push([
2323
sfc.template.content,

packages/language-core/lib/plugins/vue-tsx.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const plugin: VueLanguagePlugin = (ctx) => {
2020
'exactOptionalPropertyTypes',
2121
],
2222

23-
getEmbeddedFiles(fileName, sfc) {
23+
getEmbeddedCodes(fileName, sfc) {
2424

2525
const tsx = useTsx(fileName, sfc);
2626
const files: {
@@ -40,7 +40,7 @@ const plugin: VueLanguagePlugin = (ctx) => {
4040
return files;
4141
},
4242

43-
resolveEmbeddedFile(fileName, sfc, embeddedFile) {
43+
resolveEmbeddedCode(fileName, sfc, embeddedFile) {
4444

4545
const _tsx = useTsx(fileName, sfc);
4646

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

64-
embeddedFile.parentFileId = 'template';
64+
embeddedFile.parentCodeId = 'template';
6565

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

91-
embeddedFile.parentFileId = 'template';
91+
embeddedFile.parentCodeId = 'template';
9292

9393
const template = _tsx.generatedTemplate();
9494
if (template) {

packages/language-core/lib/types.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type * as CompilerDOM from '@vue/compiler-dom';
22
import type { SFCParseResult } from '@vue/compiler-sfc';
33
import type * as ts from 'typescript';
4-
import type { VueEmbeddedFile } from './virtualFile/embeddedFile';
4+
import type { VueEmbeddedCode } from './virtualFile/embeddedFile';
55
import type { CodeInformation, Segment } from '@volar/language-core';
66

77
export type { SFCParseResult } from '@vue/compiler-sfc';
@@ -57,6 +57,8 @@ export interface VueCompilerOptions {
5757
experimentalUseElementAccessInTemplate: boolean;
5858
}
5959

60+
export const pluginVersion = 2;
61+
6062
export type VueLanguagePlugin = (ctx: {
6163
modules: {
6264
typescript: typeof import('typescript');
@@ -67,7 +69,7 @@ export type VueLanguagePlugin = (ctx: {
6769
codegenStack: boolean;
6870
globalTypesHolder: string | undefined;
6971
}) => {
70-
version: 2;
72+
version: typeof pluginVersion;
7173
name?: string;
7274
order?: number;
7375
requiredCompilerOptions?: string[];
@@ -76,8 +78,8 @@ export type VueLanguagePlugin = (ctx: {
7678
resolveTemplateCompilerOptions?(options: CompilerDOM.CompilerOptions): CompilerDOM.CompilerOptions;
7779
compileSFCTemplate?(lang: string, template: string, options: CompilerDOM.CompilerOptions): CompilerDOM.CodegenResult | undefined;
7880
updateSFCTemplate?(oldResult: CompilerDOM.CodegenResult, textChange: { start: number, end: number, newText: string; }): CompilerDOM.CodegenResult | undefined;
79-
getEmbeddedFiles?(fileName: string, sfc: Sfc): { id: string; lang: string; }[];
80-
resolveEmbeddedFile?(fileName: string, sfc: Sfc, embeddedFile: VueEmbeddedFile): void;
81+
getEmbeddedCodes?(fileName: string, sfc: Sfc): { id: string; lang: string; }[];
82+
resolveEmbeddedCode?(fileName: string, sfc: Sfc, embeddedFile: VueEmbeddedCode): void;
8183
};
8284

8385
export interface SfcBlock {

packages/language-core/lib/virtualFile/computedFiles.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { VirtualCode, buildMappings, buildStacks, resolveCommonLanguageId, toStr
22
import { computed } from 'computeds';
33
import type * as ts from 'typescript';
44
import type { Sfc, SfcBlock, VueLanguagePlugin } from '../types';
5-
import { VueEmbeddedFile } from './embeddedFile';
5+
import { VueEmbeddedCode } from './embeddedFile';
66

77
export function computedFiles(
88
plugins: ReturnType<VueLanguagePlugin>[],
@@ -56,15 +56,15 @@ export function computedFiles(
5656
codegenStacks,
5757
embeddedCodes: [],
5858
});
59-
console.error('Unable to resolve embedded: ' + file.parentFileId + ' -> ' + file.id);
59+
console.error('Unable to resolve embedded: ' + file.parentCodeId + ' -> ' + file.id);
6060
}
6161

6262
return embeddedCodes;
6363

6464
function consumeRemain() {
6565
for (let i = remain.length - 1; i >= 0; i--) {
6666
const { file, snapshot, mappings, codegenStacks } = remain[i];
67-
if (!file.parentFileId) {
67+
if (!file.parentCodeId) {
6868
embeddedCodes.push({
6969
id: file.id,
7070
languageId: resolveCommonLanguageId(`/dummy.${file.lang}`),
@@ -77,7 +77,7 @@ export function computedFiles(
7777
remain.splice(i, 1);
7878
}
7979
else {
80-
const parent = findParentStructure(file.parentFileId, embeddedCodes);
80+
const parent = findParentStructure(file.parentCodeId, embeddedCodes);
8181
if (parent) {
8282
parent.embeddedCodes ??= [];
8383
parent.embeddedCodes.push({
@@ -118,13 +118,13 @@ function computedPluginFiles(
118118
nameToBlock: () => Record<string, SfcBlock>,
119119
codegenStack: boolean
120120
) {
121-
const embeddedFiles: Record<string, () => { file: VueEmbeddedFile; snapshot: ts.IScriptSnapshot; }> = {};
121+
const embeddedFiles: Record<string, () => { file: VueEmbeddedCode; snapshot: ts.IScriptSnapshot; }> = {};
122122
const files = computed(() => {
123123
try {
124-
if (!plugin.getEmbeddedFiles) {
124+
if (!plugin.getEmbeddedCodes) {
125125
return Object.values(embeddedFiles);
126126
}
127-
const fileInfos = plugin.getEmbeddedFiles(fileName, sfc);
127+
const fileInfos = plugin.getEmbeddedCodes(fileName, sfc);
128128
for (const oldId of Object.keys(embeddedFiles)) {
129129
if (!fileInfos.some(file => file.id === oldId)) {
130130
delete embeddedFiles[oldId];
@@ -134,13 +134,13 @@ function computedPluginFiles(
134134
if (!embeddedFiles[fileInfo.id]) {
135135
embeddedFiles[fileInfo.id] = computed(() => {
136136
const [content, stacks] = codegenStack ? track([]) : [[], []];
137-
const file = new VueEmbeddedFile(fileInfo.id, fileInfo.lang, content, stacks);
137+
const file = new VueEmbeddedCode(fileInfo.id, fileInfo.lang, content, stacks);
138138
for (const plugin of plugins) {
139-
if (!plugin.resolveEmbeddedFile) {
139+
if (!plugin.resolveEmbeddedCode) {
140140
continue;
141141
}
142142
try {
143-
plugin.resolveEmbeddedFile(fileName, sfc, file);
143+
plugin.resolveEmbeddedCode(fileName, sfc, file);
144144
}
145145
catch (e) {
146146
console.error(e);

packages/language-core/lib/virtualFile/embeddedFile.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import type { Mapping, StackNode } from '@volar/language-core';
22
import type { Code } from '../types';
33

4-
export class VueEmbeddedFile {
4+
export class VueEmbeddedCode {
55

6-
public parentFileId?: string;
6+
public parentCodeId?: string;
77
public linkedCodeMappings: Mapping[] = [];
8-
public embeddedFiles: VueEmbeddedFile[] = [];
8+
public embeddedCodes: VueEmbeddedCode[] = [];
99

1010
constructor(
1111
public id: string,

0 commit comments

Comments
 (0)