美文网首页
创建VS Code扩展插件(十二)为TreeView选择项增加编

创建VS Code扩展插件(十二)为TreeView选择项增加编

作者: 寻找无名的特质 | 来源:发表于2023-12-22 07:06 被阅读0次

当选择TreeView中的节点时,通常需要对节点进行某种操作,比如删除、编辑等。这时,需要为选择项关联一个命令。
首先,定义一个Command来响应编辑事件:
{
"command": "tempuploader.editTemplate",
"title": "Edit",
"icon": {
"light": "resources/light/edit.svg",
"dark": "resources/dark/edit.svg"
}
}
然后,将这个命令与选择的节点相关联,在menus中定义:
"view/item/context": [
{
"command": "tempuploader.editTemplate",
"when": "view == templateTree && viewItem == tempNode",
"group": "inline"
}
]
这里需要注意viewItem == tempNode,说明选中的节点contextvalue是tempNode,这个定义在节点的Class中:

export class TempNode extends TempNodeBasic {
    constructor(
        public readonly label: string,
        public readonly tempName:string,
        public readonly isFolder:boolean,
        public children: any,
        public readonly collapsibleState: vscode.TreeItemCollapsibleState
    ) {
        super(label, tempName,isFolder,children, collapsibleState);
        this.tooltip = `${this.label}`;
        this.description = this.label;
    }

    contextValue = 'tempNode';
}

然后,在extensions中注册命令:

    vscode.commands.registerCommand('tempuploader.editTemplate', (tempNode: TempNode) => {
            vscode.window.showInformationMessage("编辑模板:" + tempNode.label);
        }

相关文章

网友评论

      本文标题:创建VS Code扩展插件(十二)为TreeView选择项增加编

      本文链接:https://www.haomeiwen.com/subject/bcrugdtx.html