Skip to content

Commit

Permalink
fix(language-service): do not provide required props inlay hints for …
Browse files Browse the repository at this point in the history
…intrinsic elements (#5258)
  • Loading branch information
KazariEX authored Mar 7, 2025
1 parent f256f36 commit 1b25cb6
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 13 deletions.
3 changes: 3 additions & 0 deletions packages/language-server/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ connection.onInitialize(params => {
getElementAttrs(...args) {
return connection.sendRequest(options.typescript.requestForwardingCommand!, ['vue:getElementAttrs', args]);
},
getElementNames(...args) {
return connection.sendRequest(options.typescript.requestForwardingCommand!, ['vue:getElementNames', args]);
},
getImportPathForFile(...args) {
return connection.sendRequest(options.typescript.requestForwardingCommand!, ['vue:getImportPathForFile', args]);
},
Expand Down
4 changes: 4 additions & 0 deletions packages/language-service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { getComponentEvents } from '@vue/typescript-plugin/lib/requests/getCompo
import { getComponentNames } from '@vue/typescript-plugin/lib/requests/getComponentNames';
import { getComponentProps } from '@vue/typescript-plugin/lib/requests/getComponentProps';
import { getElementAttrs } from '@vue/typescript-plugin/lib/requests/getElementAttrs';
import { getElementNames } from '@vue/typescript-plugin/lib/requests/getElementNames';
import { getImportPathForFile } from '@vue/typescript-plugin/lib/requests/getImportPathForFile';
import { getPropertiesAtLocation } from '@vue/typescript-plugin/lib/requests/getPropertiesAtLocation';
import type { RequestContext } from '@vue/typescript-plugin/lib/requests/types';
Expand Down Expand Up @@ -131,6 +132,9 @@ export function getFullLanguageServicePlugins(ts: typeof import('typescript')) {
async getElementAttrs(...args) {
return await getElementAttrs.apply(requestContext, args);
},
async getElementNames(...args) {
return await getElementNames.apply(requestContext, args);
},
async getQuickInfoAtPosition(fileName, position) {
const languageService = context.getLanguageService();
const uri = context.project.typescript!.uriConverter.asUri(fileName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export function create(
},
create(context) {
const tsPluginClient = getTsPluginClient?.(context);
let intrinsicElementNames: Set<string>;

return {

Expand Down Expand Up @@ -59,6 +60,10 @@ export function create(
const components = await tsPluginClient?.getComponentNames(root.fileName) ?? [];
const componentProps: Record<string, string[]> = {};

intrinsicElementNames ??= new Set(
await tsPluginClient?.getElementNames(root.fileName) ?? []
);

let token: html.TokenType;
let current: {
unburnedRequiredProps: string[];
Expand All @@ -69,6 +74,10 @@ export function create(
while ((token = scanner.scan()) !== html.TokenType.EOS) {
if (token === html.TokenType.StartTag) {
const tagName = scanner.getTokenText();
if (intrinsicElementNames.has(tagName)) {
continue;
}

const checkTag = tagName.includes('.')
? tagName
: components.find(component => component === tagName || hyphenateTag(component) === tagName);
Expand Down
6 changes: 6 additions & 0 deletions packages/typescript-plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { getComponentEvents } from './lib/requests/getComponentEvents';
import { getComponentNames } from './lib/requests/getComponentNames';
import { getComponentProps } from './lib/requests/getComponentProps';
import { getElementAttrs } from './lib/requests/getElementAttrs';
import { getElementNames } from './lib/requests/getElementNames';
import { getImportPathForFile } from './lib/requests/getImportPathForFile';
import { getPropertiesAtLocation } from './lib/requests/getPropertiesAtLocation';
import { getQuickInfoAtPosition } from './lib/requests/getQuickInfoAtPosition';
Expand Down Expand Up @@ -120,6 +121,11 @@ const plugin = createLanguageServicePlugin(
response: getElementAttrs.apply(getRequestContext(args[0]), args),
};
});
session.addProtocolHandler('vue:getElementNames', ({ arguments: args }) => {
return {
response: getElementNames.apply(getRequestContext(args[0]), args),
};
});

projectService.logger.info('Vue specific commands are successfully added.');
}
Expand Down
3 changes: 2 additions & 1 deletion packages/typescript-plugin/lib/common.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { forEachElementNode, hyphenateTag, Language, VueCompilerOptions, VueVirtualCode } from '@vue/language-core';
import { capitalize } from '@vue/shared';
import type * as ts from 'typescript';
import { _getComponentNames, _getElementNames } from './requests/getComponentNames';
import { _getComponentNames } from './requests/getComponentNames';
import { _getElementNames } from './requests/getElementNames';
import type { RequestContext } from './requests/types';

const windowsPathReg = /\\/g;
Expand Down
12 changes: 0 additions & 12 deletions packages/typescript-plugin/lib/requests/getComponentNames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,3 @@ export function _getComponentNames(
names.push(getSelfComponentName(vueCode.fileName));
return names;
}

export function _getElementNames(
ts: typeof import('typescript'),
tsLs: ts.LanguageService,
vueCode: VueVirtualCode
) {
return getVariableType(ts, tsLs, vueCode, '__VLS_elements')
?.type
?.getProperties()
.map(c => c.name)
?? [];
}
29 changes: 29 additions & 0 deletions packages/typescript-plugin/lib/requests/getElementNames.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { VueVirtualCode } from '@vue/language-core';
import type * as ts from 'typescript';
import type { RequestContext } from './types';
import { getVariableType } from './utils';

export function getElementNames(
this: RequestContext,
fileName: string
) {
const { typescript: ts, language, languageService, getFileId } = this;
const volarFile = language.scripts.get(getFileId(fileName));
if (!(volarFile?.generated?.root instanceof VueVirtualCode)) {
return;
}
const vueCode = volarFile.generated.root;
return _getElementNames(ts, languageService, vueCode);
}

export function _getElementNames(
ts: typeof import('typescript'),
tsLs: ts.LanguageService,
vueCode: VueVirtualCode
) {
return getVariableType(ts, tsLs, vueCode, '__VLS_elements')
?.type
?.getProperties()
.map(c => c.name)
?? [];
}
1 change: 1 addition & 0 deletions packages/typescript-plugin/lib/requests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export type Requests = {
getComponentEvents: ToRequest<typeof import('./getComponentEvents.js')['getComponentEvents']>;
getComponentDirectives: ToRequest<typeof import('./getComponentDirectives.js')['getComponentDirectives']>;
getElementAttrs: ToRequest<typeof import('./getElementAttrs.js')['getElementAttrs']>;
getElementNames: ToRequest<typeof import('./getElementNames.js')['getElementNames']>;
};

0 comments on commit 1b25cb6

Please sign in to comment.