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

feat(language-service): support global directives completion #4989

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/language-core/lib/codegen/globalTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ export function generateGlobalTypes(lib: string, target: number, strictTemplates
>
>;
type __VLS_PrettifyGlobal<T> = { [K in keyof T]: T[K]; } & {};
type __VLS_PickDirectives<T> = {
[K in keyof T as K extends \`v\${infer N}\` ? N extends Capitalize<N> ? K : never : never]: T[K];
};
type __VLS_PickFunctionalComponentCtx<T, K> = NonNullable<__VLS_PickNotAny<
'__ctx' extends keyof __VLS_PickNotAny<K, {}> ? K extends { __ctx?: infer Ctx } ? Ctx : never : any
, T extends (props: any, ctx: infer Ctx) => any ? Ctx : any
Expand Down
2 changes: 1 addition & 1 deletion packages/language-core/lib/codegen/script/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export function* generateTemplateDirectives(options: ScriptCodegenOptions): Gene
}

exps.push(`{} as NonNullable<typeof __VLS_self extends { directives: infer D } ? D : {}>`);
exps.push(`__VLS_ctx`);
exps.push(`{} as __VLS_PickDirectives<typeof __VLS_ctx>`);

yield `const __VLS_localDirectives = {${newLine}`;
for (const type of exps) {
Expand Down
5 changes: 4 additions & 1 deletion packages/language-service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { create as createVueTwoslashQueriesPlugin } from './lib/plugins/vue-twos
import { parse, VueCompilerOptions } from '@vue/language-core';
import { proxyLanguageServiceForVue } from '@vue/typescript-plugin/lib/common';
import { collectExtractProps } from '@vue/typescript-plugin/lib/requests/collectExtractProps';
import { getComponentEvents, getComponentNames, getComponentProps, getElementAttrs, getTemplateContextProps } from '@vue/typescript-plugin/lib/requests/componentInfos';
import { getComponentDirectives, getComponentEvents, getComponentNames, getComponentProps, getElementAttrs, getTemplateContextProps } from '@vue/typescript-plugin/lib/requests/componentInfos';
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 @@ -118,6 +118,9 @@ export function getFullLanguageServicePlugins(
async getComponentEvents(...args) {
return await getComponentEvents.apply(requestContext, args);
},
async getComponentDirectives(...args) {
return await getComponentDirectives.apply(requestContext, args);
},
async getComponentNames(...args) {
return await getComponentNames.apply(requestContext, args);
},
Expand Down
42 changes: 14 additions & 28 deletions packages/language-service/lib/plugins/vue-template.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Disposable, LanguageServiceContext, LanguageServicePluginInstance } from '@volar/language-service';
import { VueCompilerOptions, VueVirtualCode, hyphenateAttr, hyphenateTag, parseScriptSetupRanges, tsCodegen } from '@vue/language-core';
import { VueCompilerOptions, VueVirtualCode, hyphenateAttr, hyphenateTag, parseScriptSetupRanges } from '@vue/language-core';
import { camelize, capitalize } from '@vue/shared';
import { getComponentSpans } from '@vue/typescript-plugin/lib/common';
import { create as createHtmlService } from 'volar-service-html';
Expand Down Expand Up @@ -471,11 +471,11 @@ export function create(
attrs: string[];
propsInfo: { name: string, commentMarkdown: string; }[];
events: string[];
directives: string[];
}>();

let version = 0;
let components: string[] | undefined;
let templateContextProps: string[] | undefined;

tsDocumentations.clear();

Expand Down Expand Up @@ -542,46 +542,25 @@ export function create(
const attrs = await tsPluginClient?.getElementAttrs(vueCode.fileName, tag) ?? [];
const propsInfo = await tsPluginClient?.getComponentProps(vueCode.fileName, tag) ?? [];
const events = await tsPluginClient?.getComponentEvents(vueCode.fileName, tag) ?? [];
const directives = await tsPluginClient?.getComponentDirectives(vueCode.fileName) ?? [];
tagInfos.set(tag, {
attrs,
propsInfo: propsInfo.filter(prop =>
!prop.name.startsWith('ref_')
),
events,
directives: directives.filter(name =>
!['vBind', 'vIf', 'vOn', 'VOnce', 'vShow', 'VSlot'].includes(name)
),
});
version++;
})());
return [];
}

const { attrs, propsInfo, events } = tagInfo;
const { attrs, propsInfo, events, directives } = tagInfo;
const props = propsInfo.map(prop => prop.name);
const attributes: html.IAttributeData[] = [];
const _tsCodegen = tsCodegen.get(vueCode._sfc);

if (_tsCodegen) {
if (!templateContextProps) {
promises.push((async () => {
templateContextProps = await tsPluginClient?.getTemplateContextProps(vueCode.fileName) ?? [];
version++;
})());
return [];
}
let ctxVars = [
..._tsCodegen.scriptRanges.get()?.bindings.map(binding => vueCode._sfc.script!.content.slice(binding.start, binding.end)) ?? [],
..._tsCodegen.scriptSetupRanges.get()?.bindings.map(binding => vueCode._sfc.scriptSetup!.content.slice(binding.start, binding.end)) ?? [],
...templateContextProps,
];
ctxVars = [...new Set(ctxVars)];
const dirs = ctxVars.map(hyphenateAttr).filter(v => v.startsWith('v-'));
for (const dir of dirs) {
attributes.push(
{
name: dir,
}
);
}
}

const propsSet = new Set(props);

Expand Down Expand Up @@ -657,6 +636,13 @@ export function create(
);
}

for (const directive of directives) {
const name = hyphenateAttr(directive);
attributes.push({
name
});
}

const models: [boolean, string][] = [];

for (const prop of [...props, ...attrs]) {
Expand Down
9 changes: 9 additions & 0 deletions packages/typescript-plugin/lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ export function getComponentEvents(
});
}

export function getComponentDirectives(
...args: Parameters<typeof import('./requests/componentInfos.js')['getComponentDirectives']>
) {
return sendRequest<ReturnType<typeof import('./requests/componentInfos')['getComponentDirectives']>>({
type: 'getComponentDirectives',
args,
});
}

export function getTemplateContextProps(
...args: Parameters<typeof import('./requests/componentInfos.js')['getTemplateContextProps']>
) {
Expand Down
22 changes: 21 additions & 1 deletion packages/typescript-plugin/lib/requests/componentInfos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,24 @@ export function getComponentEvents(
return [...result];
}

export function getComponentDirectives(
this: RequestContext,
fileName: string
) {
const { typescript: ts, language, languageService, getFileId } = this;
const volarFile = language.scripts.get(getFileId(fileName));
if (!(volarFile?.generated?.root instanceof vue.VueVirtualCode)) {
return;
}
const vueCode = volarFile.generated.root;
const directives = getVariableType(ts, languageService, vueCode, '__VLS_directives');
if (!directives) {
return [];
}

return directives.type.getProperties().map(({ name }) => name);
}

export function getTemplateContextProps(
this: RequestContext,
fileName: string
Expand Down Expand Up @@ -225,7 +243,9 @@ export function getElementAttrs(

if (tsSourceFile = program.getSourceFile(fileName)) {

const typeNode = tsSourceFile.statements.find((node): node is ts.TypeAliasDeclaration => ts.isTypeAliasDeclaration(node) && node.name.getText() === '__VLS_IntrinsicElementsCompletion');
const typeNode = tsSourceFile.statements
.filter(ts.isTypeAliasDeclaration)
.find(node => node.name.getText() === '__VLS_IntrinsicElementsCompletion');
const checker = program.getTypeChecker();

if (checker && typeNode) {
Expand Down
7 changes: 6 additions & 1 deletion packages/typescript-plugin/lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as fs from 'node:fs';
import * as net from 'node:net';
import type * as ts from 'typescript';
import { collectExtractProps } from './requests/collectExtractProps';
import { getComponentEvents, getComponentNames, getComponentProps, getElementAttrs, getTemplateContextProps } from './requests/componentInfos';
import { getComponentDirectives, getComponentEvents, getComponentNames, getComponentProps, getElementAttrs, getTemplateContextProps } from './requests/componentInfos';
import { getImportPathForFile } from './requests/getImportPathForFile';
import { getPropertiesAtLocation } from './requests/getPropertiesAtLocation';
import { getQuickInfoAtPosition } from './requests/getQuickInfoAtPosition';
Expand All @@ -20,6 +20,7 @@ export interface Request {
// Component Infos
| 'getComponentProps'
| 'getComponentEvents'
| 'getComponentDirectives'
| 'getTemplateContextProps'
| 'getComponentNames'
| 'getElementAttrs';
Expand Down Expand Up @@ -92,6 +93,10 @@ export async function startNamedPipeServer(
const result = getComponentEvents.apply(requestContext, request.args as any);
sendResponse(result);
}
else if (request.type === 'getComponentDirectives') {
const result = getComponentDirectives.apply(requestContext, request.args as any);
sendResponse(result);
}
else if (request.type === 'getTemplateContextProps') {
const result = getTemplateContextProps.apply(requestContext, request.args as any);
sendResponse(result);
Expand Down
Loading