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: should generate inlay hint for <component :is> and <slot :name> #4304

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/language-core/lib/codegen/template/element.ts
Expand Up @@ -13,6 +13,7 @@ import { generateInterpolation } from './interpolation';
import { generatePropertyAccess } from './propertyAccess';
import { generateStringLiteralKey } from './stringLiteralKey';
import { generateTemplateChild } from './templateChild';
import { generateVBindShorthandInlayHint } from './vBindShorthandInlayHint';

const colonReg = /:/g;

Expand Down Expand Up @@ -50,6 +51,10 @@ export function* generateElement(
if (isComponentTag) {
for (const prop of node.props) {
if (prop.type === CompilerDOM.NodeTypes.DIRECTIVE && prop.name === 'bind' && prop.arg?.loc.source === 'is' && prop.exp) {
if (prop.arg.loc.start.offset === prop.exp.loc.start.offset) {
yield generateVBindShorthandInlayHint(prop.exp.loc, 'is');
}

dynamicTagInfo = {
exp: prop.exp.loc.source,
offset: prop.exp.loc.start.offset,
Expand Down
18 changes: 2 additions & 16 deletions packages/language-core/lib/codegen/template/elementProps.ts
Expand Up @@ -10,6 +10,7 @@ import type { TemplateCodegenOptions } from './index';
import { generateInterpolation } from './interpolation';
import { generateObjectProperty } from './objectProperty';
import { toString } from '@volar/language-core';
import { generateVBindShorthandInlayHint } from './vBindShorthandInlayHint';

export function* generateElementProps(
options: TemplateCodegenOptions,
Expand Down Expand Up @@ -263,22 +264,7 @@ function* genereatePropExp(
features,
);
if (inlayHints) {
yield [
'',
'template',
exp.loc.end.offset,
{
__hint: {
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'),
},
} as VueCodeInformation,
];
yield generateVBindShorthandInlayHint(exp.loc, propVariableName);
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion packages/language-core/lib/codegen/template/slotOutlet.ts
Expand Up @@ -6,6 +6,8 @@
import { generateElementProps } from './elementProps';
import type { TemplateCodegenOptions } from './index';
import { generateInterpolation } from './interpolation';
import { generateVBindShorthandInlayHint } from './vBindShorthandInlayHint';

Check failure on line 9 in packages/language-core/lib/codegen/template/slotOutlet.ts

View workflow job for this annotation

GitHub Actions / build (18, macos-latest)

'generateVBindShorthandInlayHint' is declared but its value is never read.

Check failure on line 9 in packages/language-core/lib/codegen/template/slotOutlet.ts

View workflow job for this annotation

GitHub Actions / build (18, ubuntu-latest)

'generateVBindShorthandInlayHint' is declared but its value is never read.
import { camelize } from '@vue/shared';

Check failure on line 10 in packages/language-core/lib/codegen/template/slotOutlet.ts

View workflow job for this annotation

GitHub Actions / build (18, macos-latest)

'camelize' is declared but its value is never read.

Check failure on line 10 in packages/language-core/lib/codegen/template/slotOutlet.ts

View workflow job for this annotation

GitHub Actions / build (18, ubuntu-latest)

'camelize' is declared but its value is never read.

export function* generateSlotOutlet(
options: TemplateCodegenOptions,
Expand Down Expand Up @@ -44,7 +46,7 @@
? `'${nameProp.value.content}'`
: nameProp?.type === CompilerDOM.NodeTypes.DIRECTIVE && nameProp.exp?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION
? nameProp.exp.content
: `('default' as const)`
: `('default' as const)`,
),
`]`,
);
Expand Down
@@ -0,0 +1,21 @@
import type * as CompilerDOM from '@vue/compiler-dom';
import type { Code, VueCodeInformation } 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'),
},
} as VueCodeInformation,
];
};
4 changes: 2 additions & 2 deletions packages/language-service/tests/inlayHint.ts
Expand Up @@ -62,14 +62,14 @@ function readFiles(dir: string) {
return filesText;
}

const inlayHintReg = /(\^*)inlayHint:\s*"(.+)"/g;
const inlayHintReg = /(\^*)inlayHint:\s*(['"])(.+)\2/g;

function findActions(text: string) {

return [...text.matchAll(inlayHintReg)].map(flag => {

const offset = flag.index!;
const label = flag[2];
const label = flag[3];

return {
offset,
Expand Down
1 change: 1 addition & 0 deletions packages/language-service/tests/utils/createTester.ts
Expand Up @@ -45,6 +45,7 @@ function createTester(rootUri: string) {
'vue.inlayHints.missingProps': true,
'vue.inlayHints.optionsWrapper': true,
'vue.inlayHints.inlineHandlerLeading': true,
'vue.inlayHints.vBindShorthand': true,
};
let currentVSCodeSettings: any;
const language = createTypeScriptLanguage(
Expand Down
@@ -0,0 +1,15 @@
<script setup lang="ts">
let foo = 0;
let name = '';
</script>

<template>
<div :foo></div>
<!-- ^inlayHint: '="foo"' -->
<slot :foo></slot>
<!-- ^inlayHint: '="foo"' -->
<slot :name></slot>
<!-- ^inlayHint: '="name"' -->
<component :is></component>
<!-- ^inlayHint: '="is"' -->
</template>