Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
so1ve committed Apr 25, 2024
1 parent cb1c335 commit f11e9eb
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 19 deletions.
5 changes: 5 additions & 0 deletions packages/language-core/lib/codegen/template/element.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { generateElementChildren } from './elementChildren';
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 @@ export function* generateSlotOutlet(
? `'${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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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>

0 comments on commit f11e9eb

Please sign in to comment.