Skip to content

Commit

Permalink
fix(language-core): <style> completions and html custom data comple…
Browse files Browse the repository at this point in the history
…tions not provided in some cases

close vuejs#4092
  • Loading branch information
johnsoncodehk committed May 9, 2024
1 parent f1cf024 commit 3be60c6
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 13 deletions.
7 changes: 7 additions & 0 deletions packages/language-core/lib/codegen/template/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ const _codeFeatures = {
} as VueCodeInformation,
navigationAndCompletion: {
navigation: true,
completion: true,
} as VueCodeInformation,
navigationAndAdditionalCompletion: {
navigation: true,
completion: { isAdditional: true },
} as VueCodeInformation,
withoutHighlight: {
semantic: { shouldHighlight: () => false },
Expand Down Expand Up @@ -104,6 +109,7 @@ export function createTemplateCodegenContext(scriptSetupBindingNames: TemplateCo
const blockConditions: string[] = [];
const usedComponentCtxVars = new Set<string>();
const scopedClasses: { className: string, offset: number; }[] = [];
const emptyClassOffsets: number[] = [];

return {
slots,
Expand All @@ -114,6 +120,7 @@ export function createTemplateCodegenContext(scriptSetupBindingNames: TemplateCo
blockConditions,
usedComponentCtxVars,
scopedClasses,
emptyClassOffsets,
hasSlot: false,
accessExternalVariable(name: string, offset?: number) {
let arr = accessExternalVariables.get(name);
Expand Down
35 changes: 24 additions & 11 deletions packages/language-core/lib/codegen/template/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -549,20 +549,33 @@ function* generateReferencesForScopedCssClasses(
&& prop.value
) {
let startOffset = prop.value.loc.start.offset;
let tempClassName = '';
for (const char of (prop.value.loc.source + ' ')) {
if (char.trim() === '' || char === '"' || char === "'") {
if (tempClassName !== '') {
ctx.scopedClasses.push({ className: tempClassName, offset: startOffset });
startOffset += tempClassName.length;
tempClassName = '';
let content = prop.value.loc.source;
if (
(content.startsWith(`'`) && content.endsWith(`'`))
|| (content.startsWith(`"`) && content.endsWith(`"`))
) {
startOffset++;
content = content.slice(1, -1);
}
if (content) {
let currentClassName = '';
for (const char of (content + ' ')) {
if (char.trim() === '') {
if (currentClassName !== '') {
ctx.scopedClasses.push({ className: currentClassName, offset: startOffset });
startOffset += currentClassName.length;
currentClassName = '';
}
startOffset += char.length;
}
else {
currentClassName += char;
}
startOffset += char.length;
}
else {
tempClassName += char;
}
}
else {
ctx.emptyClassOffsets.push(startOffset);
}
}
else if (
prop.type === CompilerDOM.NodeTypes.DIRECTIVE
Expand Down
12 changes: 11 additions & 1 deletion packages/language-core/lib/codegen/template/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ export function* generateTemplate(options: TemplateCodegenOptions): Generator<Co

function* generateStyleScopedClasses(): Generator<Code> {
yield `if (typeof __VLS_styleScopedClasses === 'object' && !Array.isArray(__VLS_styleScopedClasses)) {${newLine}`;
for (const offset of ctx.emptyClassOffsets) {
yield `__VLS_styleScopedClasses['`;
yield [
'',
'template',
offset,
ctx.codeFeatures.additionalCompletion,
];
yield `']${endOfLine}`;
}
for (const { className, offset } of ctx.scopedClasses) {
yield `__VLS_styleScopedClasses[`;
yield [
Expand All @@ -99,7 +109,7 @@ export function* generateTemplate(options: TemplateCodegenOptions): Generator<Co
className,
'template',
offset,
ctx.codeFeatures.navigationAndCompletion,
ctx.codeFeatures.navigationAndAdditionalCompletion,
];
yield `'`;
yield [
Expand Down
2 changes: 1 addition & 1 deletion packages/language-core/lib/utils/parseCssClassNames.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { clearComments } from './parseCssVars';

const cssClassNameReg = /(?=([\.]{1}[a-zA-Z_]+[\w\_\-]*)[\s\.\+\{\>#\:]{1})/g;
const cssClassNameReg = /(?=([\.]{1}[a-zA-Z_]+[\w\_\-]*)[\s\.\,\+\{\>#\:]{1})/g;

export function* parseCssClassNames(styleContent: string) {
styleContent = clearComments(styleContent);
Expand Down

0 comments on commit 3be60c6

Please sign in to comment.