Skip to content

Commit f32c9ee

Browse files
committed
fix(language-core): unable to get completion for the second scoped class name
1 parent 4a16e57 commit f32c9ee

File tree

17 files changed

+186
-240
lines changed

17 files changed

+186
-240
lines changed

packages/language-core/lib/codegen/script/template.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { generateStyleModules } from '../style/modules';
77
import { generateStyleScopedClasses } from '../style/scopedClasses';
88
import { createTemplateCodegenContext, type TemplateCodegenContext } from '../template/context';
99
import { generateInterpolation } from '../template/interpolation';
10-
import { generateStyleScopedClassReferences } from '../template/styleScopedClasses';
1110
import { endOfLine, generateSfcBlockSection, newLine } from '../utils';
1211
import { generateSpreadMerge } from '../utils/merge';
1312
import type { ScriptCodegenContext } from './context';
@@ -26,8 +25,7 @@ export function* generateTemplate(
2625
scriptSetupBindingNames: new Set(),
2726
});
2827

29-
yield* generateStyleScopedClasses(options, templateCodegenCtx);
30-
yield* generateStyleScopedClassReferences(templateCodegenCtx, true);
28+
yield* generateStyleScopedClasses(options);
3129
yield* generateStyleModules(options);
3230
yield* generateCssVars(options, templateCodegenCtx);
3331
yield* generateBindings(options, ctx, templateCodegenCtx);

packages/language-core/lib/codegen/style/classProperty.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ import { newLine } from '../utils';
44
import { endBoundary, startBoundary } from '../utils/boundary';
55

66
export function* generateClassProperty(
7-
styleIndex: number,
7+
source: string,
88
classNameWithDot: string,
99
offset: number,
1010
propertyType: string,
1111
): Generator<Code> {
1212
yield `${newLine} & { `;
13-
const source = 'style_' + styleIndex;
1413
const token = yield* startBoundary(source, offset, codeFeatures.navigation);
1514
yield `'`;
1615
yield [classNameWithDot.slice(1), source, offset + 1, { __combineToken: token }];

packages/language-core/lib/codegen/style/imports.ts

Lines changed: 0 additions & 31 deletions
This file was deleted.

packages/language-core/lib/codegen/style/modules.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ import { generateClassProperty } from './classProperty';
66
import { generateStyleImports } from './imports';
77

88
export function* generateStyleModules(
9-
options: ScriptCodegenOptions,
9+
{ styles, scriptSetupRanges, vueCompilerOptions }: ScriptCodegenOptions,
1010
): Generator<Code> {
11-
const styles = options.styles.map((style, i) => [style, i] as const).filter(([style]) => style.module);
12-
if (!styles.length && !options.scriptSetupRanges?.useCssModule.length) {
11+
const styleModules = styles.filter(style => style.module);
12+
if (!styleModules.length && !scriptSetupRanges?.useCssModule.length) {
1313
return;
1414
}
1515
yield `type __VLS_StyleModules = {${newLine}`;
16-
for (const [style, i] of styles) {
16+
for (const style of styleModules) {
1717
if (style.module === true) {
1818
yield `$style`;
1919
}
@@ -27,16 +27,16 @@ export function* generateStyleModules(
2727
];
2828
}
2929
yield `: `;
30-
if (!options.vueCompilerOptions.strictCssModules) {
30+
if (!vueCompilerOptions.strictCssModules) {
3131
yield `Record<string, string> & `;
3232
}
3333
yield `__VLS_PrettifyGlobal<{}`;
34-
if (options.vueCompilerOptions.resolveStyleImports) {
34+
if (vueCompilerOptions.resolveStyleImports) {
3535
yield* generateStyleImports(style);
3636
}
3737
for (const className of style.classNames) {
3838
yield* generateClassProperty(
39-
i,
39+
style.name,
4040
className.text,
4141
className.offset,
4242
'string',
Lines changed: 55 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,70 @@
1-
import type { Code } from '../../types';
1+
import type { Code, Sfc, VueCodeInformation } from '../../types';
22
import type { ScriptCodegenOptions } from '../script';
3-
import type { TemplateCodegenContext } from '../template/context';
4-
import { endOfLine } from '../utils';
3+
import { generateStyleScopedClassReference } from '../template/styleScopedClasses';
4+
import { endOfLine, newLine } from '../utils';
5+
import { endBoundary, startBoundary } from '../utils/boundary';
56
import { generateClassProperty } from './classProperty';
6-
import { generateStyleImports } from './imports';
77

88
export function* generateStyleScopedClasses(
9-
options: ScriptCodegenOptions,
10-
ctx: TemplateCodegenContext,
9+
{ vueCompilerOptions, styles }: ScriptCodegenOptions,
1110
): Generator<Code> {
12-
const option = options.vueCompilerOptions.resolveStyleClassNames;
13-
const styles = options.styles
14-
.map((style, i) => [style, i] as const)
15-
.filter(([style]) => option === true || (option === 'scoped' && style.scoped));
16-
if (!styles.length) {
11+
const { resolveStyleClassNames, resolveStyleImports } = vueCompilerOptions;
12+
if (!resolveStyleClassNames) {
1713
return;
1814
}
19-
20-
const firstClasses = new Set<string>();
15+
const scopedStyles = styles.filter(style => resolveStyleClassNames === true || style.scoped);
16+
if (!scopedStyles.length) {
17+
return;
18+
}
19+
const visited = new Set<string>();
20+
const deferredGenerations: Generator<Code>[] = [];
2121
yield `type __VLS_StyleScopedClasses = {}`;
22-
for (const [style, i] of styles) {
23-
if (options.vueCompilerOptions.resolveStyleImports) {
24-
yield* generateStyleImports(style);
22+
for (const style of scopedStyles) {
23+
if (resolveStyleImports) {
24+
yield* generateImports(style);
2525
}
2626
for (const className of style.classNames) {
27-
if (firstClasses.has(className.text)) {
28-
ctx.scopedClasses.push({
29-
source: 'style_' + i,
30-
className: className.text.slice(1),
31-
offset: className.offset + 1,
32-
});
33-
continue;
27+
if (!visited.has(className.text)) {
28+
visited.add(className.text);
29+
yield* generateClassProperty(style.name, className.text, className.offset, 'boolean');
30+
}
31+
else {
32+
deferredGenerations.push(
33+
generateStyleScopedClassReference(style, className.text.slice(1), className.offset + 1),
34+
);
3435
}
35-
firstClasses.add(className.text);
36-
yield* generateClassProperty(
37-
i,
38-
className.text,
39-
className.offset,
40-
'boolean',
41-
);
4236
}
4337
}
4438
yield endOfLine;
39+
for (const generate of deferredGenerations) {
40+
yield* generate;
41+
}
42+
}
43+
44+
function* generateImports(
45+
style: Sfc['styles'][number],
46+
): Generator<Code> {
47+
const features: VueCodeInformation = {
48+
navigation: true,
49+
verification: true,
50+
};
51+
if (typeof style.src === 'object') {
52+
yield `${newLine} & typeof import(`;
53+
const token = yield* startBoundary('main', style.src.offset, features);
54+
yield `'`;
55+
yield [style.src.text, 'main', style.src.offset, { __combineToken: token }];
56+
yield `'`;
57+
yield endBoundary(token, style.src.offset + style.src.text.length);
58+
yield `).default`;
59+
}
60+
for (const { text, offset } of style.imports) {
61+
yield `${newLine} & typeof import('`;
62+
yield [
63+
text,
64+
style.name,
65+
offset,
66+
features,
67+
];
68+
yield `').default`;
69+
}
4570
}

packages/language-core/lib/codegen/template/context.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,6 @@ export function createTemplateCodegenContext(
128128
propsVar: string;
129129
}[] = [];
130130
const blockConditions: string[] = [];
131-
const scopedClasses: {
132-
source: string;
133-
className: string;
134-
offset: number;
135-
}[] = [];
136-
const emptyClassOffsets: number[] = [];
137131
const inlayHints: InlayHintInfo[] = [];
138132
const inheritedAttrVars = new Set<string>();
139133
const templateRefs = new Map<string, {
@@ -166,8 +160,6 @@ export function createTemplateCodegenContext(
166160
dollarVars,
167161
accessExternalVariables,
168162
blockConditions,
169-
scopedClasses,
170-
emptyClassOffsets,
171163
inlayHints,
172164
inheritedAttrVars,
173165
templateRefs,

0 commit comments

Comments
 (0)