Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(language-core): inlay hints for <component :is> and <slot :name> #4661

Merged
merged 12 commits into from
Oct 23, 2024
Prev Previous commit
Next Next commit
refactor: use ctx.inlayHints
  • Loading branch information
KazariEX committed Aug 11, 2024
commit 3e7dc6e4673802c7263063dcb63668b082cb150a
9 changes: 4 additions & 5 deletions packages/language-core/lib/codegen/template/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ export function* generateComponent(
yield `]${endOfLine}`;
}
else if (dynamicTagInfo) {
if (dynamicTagInfo.isComponentIsShorthand) {
ctx.inlayHints.push(generateVBindShorthandInlayHint(dynamicTagInfo.exp.loc, 'is'));
}
yield `const ${var_originalComponent} = (`;
yield* generateInterpolation(
options,
Expand All @@ -121,11 +124,7 @@ export function* generateComponent(
dynamicTagInfo.offsets[0],
ctx.codeFeatures.all,
'(',
')',
dynamicTagInfo.isComponentIsShorthand ? [[
generateVBindShorthandInlayHint(dynamicTagInfo.exp.loc, 'is'),
dynamicTagInfo.exp.loc.end.offset
]] : undefined
')'
);
if (dynamicTagInfo.offsets[1] !== undefined) {
yield `,`;
Expand Down
12 changes: 1 addition & 11 deletions packages/language-core/lib/codegen/template/elementProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,17 +298,7 @@ function* genereatePropExp(
features
);
if (enableCodeFeatures) {
ctx.inlayHints.push({
blockName: 'template',
offset: exp.loc.end.offset,
setting: 'vue.inlayHints.vBindShorthand',
label: `="${propVariableName}"`,
tooltip: [
`This is a shorthand for \`${exp.loc.source}="${propVariableName}"\`.`,
'To hide this hint, set `vue.inlayHints.vBindShorthand` to `false` in IDE settings.',
'[More info](https://github.com/vuejs/core/pull/9451)',
].join('\n\n'),
});
ctx.inlayHints.push(generateVBindShorthandInlayHint(exp.loc, propVariableName));
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions packages/language-core/lib/codegen/template/interpolation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ export function* generateInterpolation(
start: number | undefined,
data: VueCodeInformation | ((offset: number) => VueCodeInformation) | undefined,
prefix: string,
suffix: string,
inlayHints: [Code, number][] = []
suffix: string
): Generator<Code> {
const code = prefix + _code + suffix;
const ast = createTsAst(options.ts, astHolder, code);
Expand Down
9 changes: 4 additions & 5 deletions packages/language-core/lib/codegen/template/slotOutlet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ export function* generateSlotOutlet(
&& nameProp.exp?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION
) {
const isShortHand = nameProp.arg?.loc.start.offset === nameProp.exp.loc.start.offset;
if (isShortHand) {
ctx.inlayHints.push(generateVBindShorthandInlayHint(nameProp.exp.loc, 'name'));
}
const slotExpVar = ctx.getInternalVariable();
yield `var ${slotExpVar} = `;
yield* generateInterpolation(
Expand All @@ -92,11 +95,7 @@ export function* generateSlotOutlet(
nameProp.exp.loc.start.offset,
ctx.codeFeatures.all,
'(',
')',
isShortHand ? [[
generateVBindShorthandInlayHint(nameProp.exp.loc, 'name'),
nameProp.exp.loc.end.offset
]] : undefined
')'
);
yield ` as const${endOfLine}`;
ctx.dynamicSlots.push({
Expand Down
31 changes: 13 additions & 18 deletions packages/language-core/lib/codegen/template/vBindShorthand.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
import type * as CompilerDOM from '@vue/compiler-dom';
import type { Code } from "../../types";
import type { InlayHintInfo } from "../types";

export function generateVBindShorthandInlayHint(loc: CompilerDOM.SourceLocation, variableName: string): Code {
return [
'',
'template',
loc.end.offset,
{
__hint: {
setting: 'vue.inlayHints.vBindShorthand',
label: `="${variableName}"`,
tooltip: [
`This is a shorthand for \`${loc.source}="${variableName}"\`.`,
'To hide this hint, set `vue.inlayHints.vBindShorthand` to `false` in IDE settings.',
'[More info](https://github.com/vuejs/core/pull/9451)',
].join('\n\n'),
},
},
];
export function generateVBindShorthandInlayHint(loc: CompilerDOM.SourceLocation, variableName: string): InlayHintInfo {
return {
blockName: 'template',
offset: loc.end.offset,
setting: 'vue.inlayHints.vBindShorthand',
label: `="${variableName}"`,
tooltip: [
`This is a shorthand for \`${loc.source}="${variableName}"\`.`,
'To hide this hint, set `vue.inlayHints.vBindShorthand` to `false` in IDE settings.',
'[More info](https://github.com/vuejs/core/pull/9451)',
].join('\n\n'),
};
};
Loading