如何在树视图项目的上下文中调用自定义命令?

发布于 2025-01-28 16:54:05 字数 756 浏览 1 评论 0原文

我想调用自定义的“ ping”,并移交我的VS代码扩展程序所选树视图对象的详细信息。查看了几个示例后,我无法正确构建和注册命令。我可以调用命令,但是我缺少所选项目的信息。

    let InfrastructureInfo = vscode.commands.registerCommand(
        'infrastructure.info',
        async (resource) => {
            vscode.window.showInformationMessage('Ping Host Infrastructure. ....');
            let strMessage: string = PingAgent(resource);
            MessageUtils.showInfoMessage(strMessage);
        }
    );
    context.subscriptions.push(ctmInfrastructureInfo);

该功能被调用。如何获取对象的详细信息? 请参阅图片。

主机上下文中的菜单”>

I want to invoke a custom "ping" and hand over the details of the selected treeview object of my VS Code Extension. After looking at several examples, I was not able to build and register the command properly. I can invoke the command, but I'm missing the information of the selected item.

    let InfrastructureInfo = vscode.commands.registerCommand(
        'infrastructure.info',
        async (resource) => {
            vscode.window.showInformationMessage('Ping Host Infrastructure. ....');
            let strMessage: string = PingAgent(resource);
            MessageUtils.showInfoMessage(strMessage);
        }
    );
    context.subscriptions.push(ctmInfrastructureInfo);

The function is being called. How do I get the details of the object?
See picture.

TreeView Items, menu in context of host

Regards,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑脸一如从前 2025-02-04 16:54:05

自定义命令正在使用呼叫发送的树节点ID。
由于TreeView的JSON保存在我的全局扩展存储中,因此我获取JSON数据并找到节点相应的节点。由于该命令取决于一些其他信息,因此我也必须从JSON获得。

工作方式:

export function pingAgent(node: vscode.TreeItem, context: vscode.ExtensionContext) {

    if (processNumber < 9999) {
        processNumber++;
    }
    else {
        processNumber = 1;
    }
    let procNumString: string = strPrefix + processNumber.toString().padStart(4, "0");
    let strMessage: string = "";
    let responseMessage: string | undefined;
    let responseMessageError: string | undefined;
    let responseMessageBase: string | undefined;

    let ctmInfrastructureCache: string = context.globalState.get('ctmInfrastructureCache');
    let tree: json.Node = json.parseTree(ctmInfrastructureCache);
    let nodeKey: string | undefined = "";

    // this is the node # within the TreeView 
    let offsetTemp: number = Number(node.toString());
    // this is the node # with additional information, relative to the inital node # 
    let offset: number = Number(offsetTemp) + 10;

    // Get parent
    const pathTemp = json.getLocation(ctmInfrastructureCache, offsetTemp).path;
    const valueNodeTemp = json.findNodeAtLocation(tree, pathTemp);
    let ctmResourceType = getResourceType(valueNodeTemp);

    // Get CTM node name
    const path = json.getLocation(ctmInfrastructureCache, offset).path;
    const valueNode = json.findNodeAtLocation(tree, path);
    const nodeid = valueNode.value.toString();

    if (ctmResourceType === "ctm.agent") {
        let ctmDatacenterName = getDatacenterName(valueNodeTemp);

        try {
            nodeKey = valueNode.parent.children[0].value;
        } catch (error) {
            nodeKey = null;
        }

        strMessage = "Awaiting Agent Status of: '" + nodeid + "' managed by: " + ctmDatacenterName;
        OutputUtils.getOutputChannel().appendLine(procNumString + ' Message  : ' + strMessage);

        let jsonStrA = CtmTools.cmdPingAgent(ctmDatacenterName, nodeid);
        let jsonStrB = json.parse(jsonStrA.replace(/\n/g, ''));


        try {
            responseMessageBase = jsonStrB["message"].toString();
            // eslint-disable-next-line no-empty
        } catch { }

        try {
            responseMessageError = jsonStrB["errors"][0]["message"];
            // eslint-disable-next-line no-empty
        } catch { }

        if (responseMessageBase) {
            responseMessage = responseMessageBase;
        } else if (responseMessageError) {
            responseMessage = responseMessageError;
        }

        let reStrA = "unavailable";
        let reStrB = "available";
        let reStrC = "Failed";

        if (responseMessage.includes(reStrA)) {
            strMessage = 'Status : ' + responseMessage;
            MessageUtils.showWarningMessage(strMessage);
        } else if (responseMessage.includes(reStrB)) {
            strMessage = 'Status : ' + responseMessage;
            MessageUtils.showInfoMessage(strMessage);
        } else if (responseMessage.includes(reStrC)) {
            strMessage = 'Status : ' + responseMessage;
            MessageUtils.showErrorMessage(strMessage);
        } else {
            strMessage = 'Status : ' + responseMessage;
            MessageUtils.showWarningMessage(strMessage);
        }

        OutputUtils.getOutputChannel().appendLine(procNumString + ' Message  : ' + responseMessage);

    }
}

可能有更好的方法可以做到这一点,这就是我的

the custom command is making use of the tree node id which the call is sending.
As the JSON of the TreeView is saved in my global extension storage, I fetch the json data and find the node corresponding node. Since the command depends on some additional information, I have to get that from the json as well.

There might be a better way to do it, however, this is how I've got it to work:

export function pingAgent(node: vscode.TreeItem, context: vscode.ExtensionContext) {

    if (processNumber < 9999) {
        processNumber++;
    }
    else {
        processNumber = 1;
    }
    let procNumString: string = strPrefix + processNumber.toString().padStart(4, "0");
    let strMessage: string = "";
    let responseMessage: string | undefined;
    let responseMessageError: string | undefined;
    let responseMessageBase: string | undefined;

    let ctmInfrastructureCache: string = context.globalState.get('ctmInfrastructureCache');
    let tree: json.Node = json.parseTree(ctmInfrastructureCache);
    let nodeKey: string | undefined = "";

    // this is the node # within the TreeView 
    let offsetTemp: number = Number(node.toString());
    // this is the node # with additional information, relative to the inital node # 
    let offset: number = Number(offsetTemp) + 10;

    // Get parent
    const pathTemp = json.getLocation(ctmInfrastructureCache, offsetTemp).path;
    const valueNodeTemp = json.findNodeAtLocation(tree, pathTemp);
    let ctmResourceType = getResourceType(valueNodeTemp);

    // Get CTM node name
    const path = json.getLocation(ctmInfrastructureCache, offset).path;
    const valueNode = json.findNodeAtLocation(tree, path);
    const nodeid = valueNode.value.toString();

    if (ctmResourceType === "ctm.agent") {
        let ctmDatacenterName = getDatacenterName(valueNodeTemp);

        try {
            nodeKey = valueNode.parent.children[0].value;
        } catch (error) {
            nodeKey = null;
        }

        strMessage = "Awaiting Agent Status of: '" + nodeid + "' managed by: " + ctmDatacenterName;
        OutputUtils.getOutputChannel().appendLine(procNumString + ' Message  : ' + strMessage);

        let jsonStrA = CtmTools.cmdPingAgent(ctmDatacenterName, nodeid);
        let jsonStrB = json.parse(jsonStrA.replace(/\n/g, ''));


        try {
            responseMessageBase = jsonStrB["message"].toString();
            // eslint-disable-next-line no-empty
        } catch { }

        try {
            responseMessageError = jsonStrB["errors"][0]["message"];
            // eslint-disable-next-line no-empty
        } catch { }

        if (responseMessageBase) {
            responseMessage = responseMessageBase;
        } else if (responseMessageError) {
            responseMessage = responseMessageError;
        }

        let reStrA = "unavailable";
        let reStrB = "available";
        let reStrC = "Failed";

        if (responseMessage.includes(reStrA)) {
            strMessage = 'Status : ' + responseMessage;
            MessageUtils.showWarningMessage(strMessage);
        } else if (responseMessage.includes(reStrB)) {
            strMessage = 'Status : ' + responseMessage;
            MessageUtils.showInfoMessage(strMessage);
        } else if (responseMessage.includes(reStrC)) {
            strMessage = 'Status : ' + responseMessage;
            MessageUtils.showErrorMessage(strMessage);
        } else {
            strMessage = 'Status : ' + responseMessage;
            MessageUtils.showWarningMessage(strMessage);
        }

        OutputUtils.getOutputChannel().appendLine(procNumString + ' Message  : ' + responseMessage);

    }
}

Regards

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文