支持了data中定义attr在组件中使用

This commit is contained in:
pqcqaq 2024-10-24 11:33:01 +08:00
parent 571ab5b8d9
commit 9ba464b7bf
4 changed files with 75 additions and 0 deletions

View File

@ -161,6 +161,40 @@ class OakComponentPropsLinkProvider implements vscode.DocumentLinkProvider {
link.tooltip = 'index.ts中的properties之一';
documentLinks.push(link);
return;
} else {
// 再判断在不在data里面
if (
componentInfo.datas
?.map((i) => i.value)
.includes(attr.value as string)
) {
// 创建文档链接
const startPos = document.positionAt(attr.pos.start);
const endPos = document.positionAt(attr.pos.end);
const range = new vscode.Range(startPos, endPos);
const toStart = componentInfo.datas?.find(
(i) => i.value === attr.value
)?.pos.start;
const toEnd = componentInfo.datas?.find(
(i) => i.value === attr.value
)?.pos.end;
const args = {
filePath: join(document.uri.fsPath, '../index.ts'),
start: toStart,
end: toEnd,
};
const link = new vscode.DocumentLink(
range,
vscode.Uri.parse(
`command:oak-assistant.jumpToPosition?${encodeURIComponent(
JSON.stringify(args)
)}`
)
);
link.tooltip = 'index.ts中的data之一';
documentLinks.push(link);
return;
}
}
const startPos = document.positionAt(attr.pos.start);
const endPos = document.positionAt(attr.pos.end);

View File

@ -77,6 +77,7 @@ export type EntityComponentDef = {
isList: boolean;
components: ComponentDef[];
formDataAttrs?: DocumentValue[];
datas?: DocumentValue[];
methodNames?: DocumentValue[];
propertiesAttrs?: DocumentValue[];
mpConfig?: MPConfig;

View File

@ -12,6 +12,7 @@ import fs from 'fs';
import { join } from 'path';
import { onEntityLoaded } from './status';
import {
getAttrsFromDatas,
getAttrsFromFormData,
getAttrsFromMethods,
getAttrsFromProperties,
@ -105,6 +106,12 @@ export const scanComponents = (scanPath: string[]): EntityComponentDef[] => {
prop.name.getText() === 'properties'
);
const datas = properties.find(
(prop) =>
ts.isPropertyAssignment(prop) &&
prop.name.getText() === 'data'
);
let mpConfig: MPConfig | undefined;
const configPath = join(path, '../index.json');
@ -121,6 +128,7 @@ export const scanComponents = (scanPath: string[]): EntityComponentDef[] => {
let formDataAttrs: DocumentValue[] = [];
let methodNames: DocumentValue[] = [];
let propertiesAttrs: DocumentValue[] = [];
let datasAttrs: DocumentValue[] = [];
// 获取formData下的block 下的 returnStatement 下的ObjectLiteralExpression 下的properties
if (formData) {
formDataAttrs = getAttrsFromFormData(formData);
@ -134,6 +142,10 @@ export const scanComponents = (scanPath: string[]): EntityComponentDef[] => {
propertiesAttrs = getAttrsFromProperties(property);
}
if (datas) {
datasAttrs = getAttrsFromDatas(datas);
}
if (entity && isList) {
if (
ts.isShorthandPropertyAssignment(entity) ||
@ -188,6 +200,7 @@ export const scanComponents = (scanPath: string[]): EntityComponentDef[] => {
propertiesAttrs: propertiesAttrs.length
? propertiesAttrs
: undefined,
datas: datasAttrs.length ? datasAttrs : undefined,
mpConfig,
});
} else {
@ -206,6 +219,7 @@ export const scanComponents = (scanPath: string[]): EntityComponentDef[] => {
propertiesAttrs: propertiesAttrs.length
? propertiesAttrs
: undefined,
datas: datasAttrs.length ? datasAttrs : undefined,
mpConfig,
});
}

View File

@ -137,6 +137,32 @@ export const getAttrsFromMethods = (
return attrs;
};
export const getAttrsFromDatas = (
element: ts.ObjectLiteralElementLike
): DocumentValue[] => {
const attrs: DocumentValue[] = [];
ts.forEachChild(element, (child) => {
if (ts.isObjectLiteralExpression(child)) {
ts.forEachChild(child, (objectChild) => {
if (
ts.isMethodDeclaration(objectChild) ||
ts.isShorthandPropertyAssignment(objectChild) ||
ts.isPropertyAssignment(objectChild)
) {
attrs.push({
value: objectChild.name.getText(),
pos: {
start: objectChild.name.getStart(),
end: objectChild.name.getEnd(),
},
});
}
});
}
});
return attrs;
};
/**
* attrs
* @param element