Skip to content

Commit

Permalink
feat(typescript-plugin): create script setup block when auto import i…
Browse files Browse the repository at this point in the history
…f needed
  • Loading branch information
johnsoncodehk committed May 10, 2024
1 parent 8865234 commit 1462f5e
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 19 deletions.
13 changes: 0 additions & 13 deletions packages/language-core/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,19 +123,6 @@ export interface Sfc {
customBlocks: readonly (SfcBlock & {
type: string;
})[];

/**
* @deprecated use `template.ast` instead
*/
templateAst: CompilerDOM.RootNode | undefined;
/**
* @deprecated use `script.ast` instead
*/
scriptAst: ts.SourceFile | undefined;
/**
* @deprecated use `scriptSetup.ast` instead
*/
scriptSetupAst: ts.SourceFile | undefined;
}

export interface TextRange {
Expand Down
3 changes: 0 additions & 3 deletions packages/language-core/lib/virtualFile/computedSfc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,6 @@ export function computedSfc(
get scriptSetup() { return scriptSetup(); },
get styles() { return styles; },
get customBlocks() { return customBlocks; },
get templateAst() { return template()?.ast; },
get scriptAst() { return script()?.ast; },
get scriptSetupAst() { return scriptSetup()?.ast; },
};

function computedTemplateAst(base: SfcBlock) {
Expand Down
2 changes: 1 addition & 1 deletion packages/language-service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export function getVueLanguageServicePlugins(
}
const languageService = (created.provide as import('volar-service-typescript').Provide)['typescript/languageService']();
const vueOptions = getVueOptions(context.env);
decorateLanguageServiceForVue(context.language, languageService, vueOptions, ts, false);
decorateLanguageServiceForVue(context.language, languageService, vueOptions, ts, false, fileName => context.env.typescript!.fileNameToUri(fileName));
return created;
},
};
Expand Down
2 changes: 1 addition & 1 deletion packages/typescript-plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function createLanguageServicePlugin(): ts.server.PluginModuleFactory {
projects.set(info.project, { info, language, vueOptions });

decorateLanguageService(language, info.languageService);
decorateLanguageServiceForVue(language, info.languageService, vueOptions, ts, true);
decorateLanguageServiceForVue(language, info.languageService, vueOptions, ts, true, fileName => fileName);
decorateLanguageServiceHost(ts, language, info.languageServiceHost);
startNamedPipeServer(ts, info.project.projectKind, info.project.getCurrentDirectory());

Expand Down
30 changes: 29 additions & 1 deletion packages/typescript-plugin/lib/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export function decorateLanguageServiceForVue(
vueOptions: vue.VueCompilerOptions,
ts: typeof import('typescript'),
isTsPlugin: boolean,
getScriptId: (fileName: string) => string,
) {
const {
getCompletionsAtPosition,
Expand Down Expand Up @@ -49,6 +50,12 @@ export function decorateLanguageServiceForVue(
break;
}
}
if (item.data) {
// @ts-expect-error
item.data.__isAutoImport = {
fileName,
};
}
}
}
}
Expand All @@ -69,6 +76,27 @@ export function decorateLanguageServiceForVue(
}
}
}
// @ts-expect-error
if (args[6]?.__isAutoImport) {
// @ts-expect-error
const { fileName } = args[6]?.__isAutoImport;
const sourceScript = language.scripts.get(getScriptId(fileName));
if (sourceScript?.generated?.root instanceof vue.VueVirtualCode) {
const sfc = sourceScript.generated.root.getVueSfc();
if (!sfc?.descriptor.script && !sfc?.descriptor.scriptSetup) {
for (const codeAction of details?.codeActions ?? []) {
for (const change of codeAction.changes) {
for (const textChange of change.textChanges) {
textChange.newText = `<script setup lang="ts">${textChange.newText}</script>\n\n`;
break;
}
break;
}
break;
}
}
}
}
return details;
};
languageService.getCodeFixesAtPosition = (...args) => {
Expand All @@ -80,7 +108,7 @@ export function decorateLanguageServiceForVue(
if (isTsPlugin) {
languageService.getEncodedSemanticClassifications = (fileName, span, format) => {
const result = getEncodedSemanticClassifications(fileName, span, format);
const file = language.scripts.get(fileName);
const file = language.scripts.get(getScriptId(fileName));
if (file?.generated?.root instanceof vue.VueVirtualCode) {
const { template } = file.generated.root.sfc;
if (template) {
Expand Down

0 comments on commit 1462f5e

Please sign in to comment.