Skip to content

Commit

Permalink
only generate auto import virtual code for editing scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Aug 22, 2024
1 parent cdf8939 commit 3b5258a
Show file tree
Hide file tree
Showing 13 changed files with 45 additions and 54 deletions.
3 changes: 1 addition & 2 deletions packages/component-meta/lib/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ export function baseCreate(
ts.sys.useCaseSensitiveFileNames
),
projectHost.getCompilationSettings(),
commandLine.vueOptions,
true
commandLine.vueOptions
);
const language = vue.createLanguage(
[
Expand Down
2 changes: 1 addition & 1 deletion packages/language-core/lib/codegen/script/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export interface ScriptCodegenOptions {
scriptSetupRanges: ScriptSetupRanges | undefined;
templateCodegen: TemplateCodegenContext & { codes: Code[]; } | undefined;
globalTypes: boolean;
typeCheckOnly: boolean;
edited: boolean;
getGeneratedLength: () => number;
linkedCodeMappings: Mapping[];
}
Expand Down
2 changes: 1 addition & 1 deletion packages/language-core/lib/codegen/script/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function* generateTemplate(
else {
yield `function __VLS_template() {${newLine}`;
}
const templateCodegenCtx = createTemplateCodegenContext({ scriptSetupBindingNames: new Set(), typeCheckOnly: options.typeCheckOnly });
const templateCodegenCtx = createTemplateCodegenContext({ scriptSetupBindingNames: new Set(), edited: options.edited });
yield* generateCtx(options, ctx, isClassComponent);
yield* generateTemplateContext(options, templateCodegenCtx);
yield* generateExportOptions(options);
Expand Down
4 changes: 2 additions & 2 deletions packages/language-core/lib/codegen/template/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const _codeFeatures = {

export type TemplateCodegenContext = ReturnType<typeof createTemplateCodegenContext>;

export function createTemplateCodegenContext(options: Pick<TemplateCodegenOptions, 'scriptSetupBindingNames' | 'typeCheckOnly'>) {
export function createTemplateCodegenContext(options: Pick<TemplateCodegenOptions, 'scriptSetupBindingNames' | 'edited'>) {
let ignoredError = false;
let expectErrorToken: {
errors: number;
Expand Down Expand Up @@ -184,7 +184,7 @@ export function createTemplateCodegenContext(options: Pick<TemplateCodegenOption
}
},
generateAutoImportCompletion: function* (): Generator<Code> {
if (options.typeCheckOnly) {
if (!options.edited) {
return;
}
const all = [...accessExternalVariables.entries()];
Expand Down
56 changes: 27 additions & 29 deletions packages/language-core/lib/codegen/template/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,39 +137,37 @@ export function* generateComponent(
yield* generateCanonicalComponentName(
node.tag,
startTagOffset,
options.typeCheckOnly
? ctx.codeFeatures.verification
: {
// hover support
...ctx.codeFeatures.withoutHighlightAndCompletionAndNavigation,
...ctx.codeFeatures.verification,
}
{
// with hover support
...ctx.codeFeatures.withoutHighlightAndCompletionAndNavigation,
...ctx.codeFeatures.verification,
}
);
yield `${endOfLine}`;

if (!options.typeCheckOnly) {
const camelizedTag = camelize(node.tag);
if (variableNameRegex.test(camelizedTag)) {
// renaming / find references support
for (const tagOffset of tagOffsets) {
for (const shouldCapitalize of (node.tag[0] === node.tag[0].toUpperCase() ? [false] : [true, false])) {
const expectName = shouldCapitalize ? capitalize(camelizedTag) : camelizedTag;
yield `__VLS_components.`;
yield* generateCamelized(
shouldCapitalize ? capitalize(node.tag) : node.tag,
tagOffset,
{
navigation: {
resolveRenameNewName: node.tag !== expectName ? camelizeComponentName : undefined,
resolveRenameEditText: getTagRenameApply(node.tag),
},
}
);
yield `;`;
}
const camelizedTag = camelize(node.tag);
if (variableNameRegex.test(camelizedTag)) {
// renaming / find references support
for (const tagOffset of tagOffsets) {
for (const shouldCapitalize of (node.tag[0] === node.tag[0].toUpperCase() ? [false] : [true, false])) {
const expectName = shouldCapitalize ? capitalize(camelizedTag) : camelizedTag;
yield `__VLS_components.`;
yield* generateCamelized(
shouldCapitalize ? capitalize(node.tag) : node.tag,
tagOffset,
{
navigation: {
resolveRenameNewName: node.tag !== expectName ? camelizeComponentName : undefined,
resolveRenameEditText: getTagRenameApply(node.tag),
},
}
);
yield `;`;
}
yield `${newLine}`;
// auto import support
}
yield `${newLine}`;
// auto import support
if (options.edited) {
yield `// @ts-ignore${newLine}`; // #2304
yield* generateCamelized(
capitalize(node.tag),
Expand Down
2 changes: 1 addition & 1 deletion packages/language-core/lib/codegen/template/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface TemplateCodegenOptions {
template: NonNullable<Sfc['template']>;
scriptSetupBindingNames: Set<string>;
scriptSetupImportComponentNames: Set<string>;
typeCheckOnly: boolean;
edited: boolean;
hasDefineSlots?: boolean;
slotsAssignName?: string;
propsAssignName?: string;
Expand Down
10 changes: 3 additions & 7 deletions packages/language-core/lib/languagePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,14 @@ export function createVueLanguagePlugin<T>(
_getProjectVersion: (() => string) | undefined,
isRootFile: (fileName: string) => boolean,
compilerOptions: ts.CompilerOptions,
vueCompilerOptions: VueCompilerOptions,
typeCheckOnly = true
vueCompilerOptions: VueCompilerOptions
): LanguagePlugin<T, VueVirtualCode> {
return createVueLanguagePlugin2(
ts,
asFileName,
isRootFile,
compilerOptions,
vueCompilerOptions,
typeCheckOnly
vueCompilerOptions
);
}

Expand All @@ -94,8 +92,7 @@ export function createVueLanguagePlugin2<T>(
asFileName: (scriptId: T) => string,
isRootFile: (fileName: string) => boolean,
compilerOptions: ts.CompilerOptions,
vueCompilerOptions: VueCompilerOptions,
typeCheckOnly = true
vueCompilerOptions: VueCompilerOptions
): LanguagePlugin<T, VueVirtualCode> {
const pluginContext: Parameters<VueLanguagePlugin>[0] = {
modules: {
Expand All @@ -109,7 +106,6 @@ export function createVueLanguagePlugin2<T>(
},
compilerOptions,
vueCompilerOptions,
typeCheckOnly,
globalTypesHolder: undefined,
};
const plugins = createPlugins(pluginContext);
Expand Down
7 changes: 5 additions & 2 deletions packages/language-core/lib/plugins/vue-tsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import type { Code, Sfc, VueLanguagePlugin } from '../types';

export const tsCodegen = new WeakMap<Sfc, ReturnType<typeof createTsx>>();

const fileEditTimes = new Map<string, number>();

const plugin: VueLanguagePlugin = ctx => {

return {
Expand Down Expand Up @@ -91,8 +93,8 @@ function createTsx(
ts,
compilerOptions: ctx.compilerOptions,
vueCompilerOptions: ctx.vueCompilerOptions,
typeCheckOnly: ctx.typeCheckOnly,
template: _sfc.template,
edited: (fileEditTimes.get(fileName) ?? 0) >= 2,
scriptSetupBindingNames: scriptSetupBindingNames(),
scriptSetupImportComponentNames: scriptSetupImportComponentNames(),
hasDefineSlots: hasDefineSlots(),
Expand Down Expand Up @@ -152,10 +154,11 @@ function createTsx(
templateCodegen: _template,
compilerOptions: ctx.compilerOptions,
vueCompilerOptions: ctx.vueCompilerOptions,
typeCheckOnly: ctx.typeCheckOnly,
edited: (fileEditTimes.get(fileName) ?? 0) >= 2,
getGeneratedLength: () => generatedLength,
linkedCodeMappings,
});
fileEditTimes.set(fileName, (fileEditTimes.get(fileName) ?? 0) + 1);

let current = codegen.next();

Expand Down
1 change: 0 additions & 1 deletion packages/language-core/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ export type VueLanguagePlugin = (ctx: {
compilerOptions: ts.CompilerOptions;
vueCompilerOptions: VueCompilerOptions;
globalTypesHolder: string | undefined;
typeCheckOnly: boolean;
}) => VueLanguagePluginReturn | VueLanguagePluginReturn[];

export interface SfcBlock {
Expand Down
3 changes: 1 addition & 2 deletions packages/language-server/lib/initialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ export function initialize(
sys.useCaseSensitiveFileNames
),
compilerOptions,
vueCompilerOptions,
false
vueCompilerOptions
)],
setup({ project }) {
project.vue = { compilerOptions: vueCompilerOptions };
Expand Down
3 changes: 1 addition & 2 deletions packages/language-server/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ connection.onInitialize(params => {
asFileName,
() => false,
commandLine.options,
commandLine.vueOptions,
false
commandLine.vueOptions
)],
setup({ project }) {
project.vue = { compilerOptions: commandLine.vueOptions };
Expand Down
3 changes: 1 addition & 2 deletions packages/tsc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ export function run() {
options.host?.useCaseSensitiveFileNames?.() ?? false
),
options.options,
vueOptions,
true
vueOptions
);
return { languagePlugins: [vueLanguagePlugin] };
}
Expand Down
3 changes: 1 addition & 2 deletions packages/typescript-plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ const plugin = createLanguageServicePlugin(
info.languageServiceHost.useCaseSensitiveFileNames?.() ?? false
),
info.languageServiceHost.getCompilationSettings(),
vueOptions,
false
vueOptions
);

return {
Expand Down

0 comments on commit 3b5258a

Please sign in to comment.