新增propertiesAttrs的扫描,提供跳转功能
This commit is contained in:
parent
e5c43b7fb9
commit
152b27f221
|
|
@ -111,6 +111,24 @@ class OakComponentPropsLinkProvider implements vscode.DocumentLinkProvider {
|
|||
// 检查attrs是否都在定义中
|
||||
node.attrList?.forEach((attr) => {
|
||||
if (!componentInfo.formDataAttrs?.includes(attr.value as string)) {
|
||||
// 这里还需要判断在不在propertiesAttrs里面
|
||||
if (
|
||||
componentInfo.propertiesAttrs?.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 uri = vscode.Uri.file(
|
||||
join(document.uri.fsPath, '../index.ts')
|
||||
);
|
||||
const link = new vscode.DocumentLink(range, uri);
|
||||
link.tooltip = 'index.ts中的properties之一';
|
||||
documentLinks.push(link);
|
||||
return;
|
||||
}
|
||||
const startPos = document.positionAt(attr.pos.start);
|
||||
const endPos = document.positionAt(attr.pos.end);
|
||||
const range = new vscode.Range(startPos, endPos);
|
||||
|
|
@ -123,6 +141,17 @@ class OakComponentPropsLinkProvider implements vscode.DocumentLinkProvider {
|
|||
// 添加元数据
|
||||
diagnostic.source = attr.value as string;
|
||||
diagnostics.push(diagnostic);
|
||||
} else {
|
||||
// 添加文档链接
|
||||
const startPos = document.positionAt(attr.pos.start);
|
||||
const endPos = document.positionAt(attr.pos.end);
|
||||
const range = new vscode.Range(startPos, endPos);
|
||||
const uri = vscode.Uri.file(
|
||||
join(document.uri.fsPath, '../index.ts')
|
||||
);
|
||||
const link = new vscode.DocumentLink(range, uri);
|
||||
link.tooltip = 'index.ts中formData的返回值之一';
|
||||
documentLinks.push(link);
|
||||
}
|
||||
});
|
||||
|
||||
|
|
@ -141,6 +170,17 @@ class OakComponentPropsLinkProvider implements vscode.DocumentLinkProvider {
|
|||
// 添加元数据
|
||||
diagnostic.source = method.value as string;
|
||||
diagnostics.push(diagnostic);
|
||||
} else {
|
||||
// 添加文档链接
|
||||
const startPos = document.positionAt(method.pos.start);
|
||||
const endPos = document.positionAt(method.pos.end);
|
||||
const range = new vscode.Range(startPos, endPos);
|
||||
const uri = vscode.Uri.file(
|
||||
join(document.uri.fsPath, '../index.ts')
|
||||
);
|
||||
const link = new vscode.DocumentLink(range, uri);
|
||||
link.tooltip = 'index.ts中methods的方法之一';
|
||||
documentLinks.push(link);
|
||||
}
|
||||
});
|
||||
|
||||
|
|
@ -254,7 +294,6 @@ const codeActionProvider = vscode.languages.registerCodeActionsProvider(
|
|||
new OakComponentPropsCodeActionProvider()
|
||||
);
|
||||
|
||||
|
||||
// 当工作区有任何文件保存的时候,重新
|
||||
|
||||
export function activateOakComponentPropsLinkProvider(
|
||||
|
|
|
|||
|
|
@ -69,6 +69,7 @@ export type EntityComponentDef = {
|
|||
components: ComponentDef[];
|
||||
formDataAttrs?: string[];
|
||||
methodNames?: string[];
|
||||
propertiesAttrs?: string[];
|
||||
};
|
||||
|
||||
export type EnhtityComponentMap = {
|
||||
|
|
|
|||
|
|
@ -6,7 +6,11 @@ import { glob } from 'glob';
|
|||
import fs from 'fs';
|
||||
import { join } from 'path';
|
||||
import { onEntityLoaded } from './status';
|
||||
import { getAttrsFromFormData, getAttrsFromMethods } from './ts-utils';
|
||||
import {
|
||||
getAttrsFromFormData,
|
||||
getAttrsFromMethods,
|
||||
getAttrsFromProperties,
|
||||
} from './ts-utils';
|
||||
|
||||
const entityComponents: EnhtityComponentMap = new Proxy(
|
||||
{} as EnhtityComponentMap,
|
||||
|
|
@ -84,8 +88,15 @@ export const scanComponents = (scanPath: string[]): EntityComponentDef[] => {
|
|||
prop.name.getText() === 'methods'
|
||||
);
|
||||
|
||||
const property = properties.find(
|
||||
(prop) =>
|
||||
ts.isPropertyAssignment(prop) &&
|
||||
prop.name.getText() === 'properties'
|
||||
);
|
||||
|
||||
let formDataAttrs: string[] = [];
|
||||
let methodNames: string[] = [];
|
||||
let propertiesAttrs: string[] = [];
|
||||
// 获取formData下的block 下的 returnStatement 下的ObjectLiteralExpression 下的properties
|
||||
if (formData) {
|
||||
formDataAttrs = getAttrsFromFormData(formData);
|
||||
|
|
@ -95,6 +106,10 @@ export const scanComponents = (scanPath: string[]): EntityComponentDef[] => {
|
|||
methodNames = getAttrsFromMethods(method);
|
||||
}
|
||||
|
||||
if (property) {
|
||||
propertiesAttrs = getAttrsFromProperties(property);
|
||||
}
|
||||
|
||||
if (entity && isList) {
|
||||
if (
|
||||
ts.isShorthandPropertyAssignment(entity) ||
|
||||
|
|
@ -143,7 +158,12 @@ export const scanComponents = (scanPath: string[]): EntityComponentDef[] => {
|
|||
formDataAttrs: formDataAttrs.length
|
||||
? formDataAttrs
|
||||
: undefined,
|
||||
methodNames: methodNames.length ? methodNames : undefined,
|
||||
methodNames: methodNames.length
|
||||
? methodNames
|
||||
: undefined,
|
||||
propertiesAttrs: propertiesAttrs.length
|
||||
? propertiesAttrs
|
||||
: undefined,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -102,6 +102,30 @@ export const getAttrsFromMethods = (
|
|||
return attrs;
|
||||
};
|
||||
|
||||
/**
|
||||
* 获取函数的返回值的attrs
|
||||
* @param element 函数体节点
|
||||
* @returns 返回值的attrs
|
||||
*/
|
||||
export const getAttrsFromProperties = (
|
||||
element: ts.ObjectLiteralElementLike
|
||||
): string[] => {
|
||||
const attrs: string[] = [];
|
||||
ts.forEachChild(element, (child) => {
|
||||
if (ts.isObjectLiteralExpression(child)) {
|
||||
ts.forEachChild(child, (objectChild) => {
|
||||
if (
|
||||
ts.isPropertyAssignment(objectChild) ||
|
||||
ts.isShorthandPropertyAssignment(objectChild)
|
||||
) {
|
||||
attrs.push(objectChild.name.getText());
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
return attrs;
|
||||
};
|
||||
|
||||
/**
|
||||
* 获取函数的返回值的attrs
|
||||
* @param sourceFile 源文件
|
||||
|
|
@ -400,20 +424,25 @@ export async function addMethodToMethods(
|
|||
|
||||
if (methodsPos === null || insertPos === null) {
|
||||
if (!oakCreateObj) {
|
||||
vscode.window.showErrorMessage('无法在 index.ts 中找到 OakComponent 调用');
|
||||
vscode.window.showErrorMessage(
|
||||
'无法在 index.ts 中找到 OakComponent 调用'
|
||||
);
|
||||
}
|
||||
// 在oakCreateObj找到最后一个属性的位置,并中创建一个属性为methods的对象字面量
|
||||
let lastPropPos = oakCreateObj!.properties.end;
|
||||
let insertText = `\n methods: {\n ${methodName}() {},\n }`;
|
||||
if (oakCreateObj!.properties.length) {
|
||||
const lastProp = oakCreateObj!.properties[
|
||||
oakCreateObj!.properties.length - 1
|
||||
];
|
||||
const lastProp =
|
||||
oakCreateObj!.properties[oakCreateObj!.properties.length - 1];
|
||||
lastPropPos = lastProp.end;
|
||||
insertText = `,${insertText}`;
|
||||
}
|
||||
const edit = new vscode.WorkspaceEdit();
|
||||
edit.insert(indexPath, indexDocument.positionAt(lastPropPos), insertText);
|
||||
edit.insert(
|
||||
indexPath,
|
||||
indexDocument.positionAt(lastPropPos),
|
||||
insertText
|
||||
);
|
||||
await vscode.workspace.applyEdit(edit);
|
||||
//保存
|
||||
await indexDocument.save();
|
||||
|
|
|
|||
Loading…
Reference in New Issue