简体   繁体   English

如何使用自定义 JSON 构建 VS Code TreeView?

[英]How to build VS Code TreeView with custom JSON?

I'd like to build a TreeView based on a JSON file / REST Call and have an icon for each type of my objects.我想基于 JSON 文件/REST 调用构建一个 TreeView,并为我的每种类型的对象都有一个图标。 I have only three types of objects: Server, Host and Group.我只有三种类型的对象:服务器、主机和组。 For each type I'd like to have a custom icon and configuration.menu similar to what I found here dynamic context menu .对于每种类型,我都希望有一个自定义图标和 configuration.menu,类似于我在这里找到的动态上下文菜单

I'm new to VS Code extensions building.我是 VS Code 扩展构建的新手。 I've tried to modify the constructor(){this.data ...} to get the json file and tinkered with the class class TreeItem extends vscode.TreeItem , however I'm not yet fully grasping the behavior I need of the class and code I need to change.我试图修改constructor(){this.data ...}来获取 json 文件并修改类class TreeItem extends vscode.TreeItem ,但是我还没有完全掌握我需要的类的行为和我需要更改的代码。

My current code:我当前的代码:

import * as vscode from 'vscode';
import * as json from 'jsonc-parser';
import * as path from 'path';


export class CtmInfraProvider implements vscode.TreeDataProvider<TreeItem> {
  onDidChangeTreeData?: vscode.Event<TreeItem | null | undefined> | undefined;

  data: TreeItem[];

  constructor() {
    this.data = [new TreeItem('Agents', [
      new TreeItem(
        'datacenter', [new TreeItem('xyz.domain.name'), new TreeItem('xyz.domain.name'), new TreeItem('xyz.domain.name')])
    ]),
    new TreeItem('Groups', [
      new TreeItem(
        'abcd', [new TreeItem('xyz.domain.name'), new TreeItem('abc.domain.name'), new TreeItem('123.domain.name')]),
      new TreeItem(
        'wxyz', [new TreeItem('efg.domain.name'), new TreeItem('456.domain.name')])
    ])

    ];
  }

  getTreeItem(element: TreeItem): vscode.TreeItem | Thenable<vscode.TreeItem> {
    return element;
  }

  getChildren(element?: TreeItem | undefined): vscode.ProviderResult<TreeItem[]> {
    if (element === undefined) {
      return this.data;
    }
    return element.children;
  }

}

class TreeItem extends vscode.TreeItem {

  constructor(
    public readonly label: string,
    public children?: TreeItem[],
    public readonly command?: vscode.Command,
    public iconPath?: { light: string, dark: string }
  ) {
    super(
      label,
      children === undefined ? vscode.TreeItemCollapsibleState.None :
        vscode.TreeItemCollapsibleState.Expanded);
    this.children = children;
    // this.type = type;
    this.tooltip = `Agent Details:\n - Datacenter: demo\n - OS: Linux \n - Version: 20.22.04.00\n - Status: Available`;
    // this.description = 'Hello Desc';
  }

  setConfiguredIcon(): void {
    let newLightIcon: any;
    let newDarkIcon: any;

    newLightIcon = path.join(__filename, '..', '..', 'resources', 'light', 'dna.svg');
    newDarkIcon = path.join(__filename, '..', '..', 'resources', 'dark', 'dna.svg');

    if (this.iconPath === undefined) {
      this.iconPath = {
        light: newLightIcon,
        dark: newDarkIcon
      };
    }
    else {
      this.iconPath = {
        light: newLightIcon,
        dark: newDarkIcon
      };
    }
  }

}

The desired result, missing icon and menu: TreeView based on hard-coded json想要的结果,缺少图标和菜单:基于硬编码 json 的 TreeView

Here is a copy of the json file I'm looking at.这是我正在查看的 json 文件的副本。 If there is a better format, the json file can be adjusted anytime.如果有更好的格式,可以随时调整json文件。

{
    "inventory": {
        "servers": [
            {
                "server": "abcd",
                "host": "abcd.domain.name",
                "nodes": [
                    {
                        "nodeid": "xyz.domain.name",
                        "operating_system": "Microsoft Windows Server 2016  (Build 14393)",
                        "status": "Available",
                        "version": "20.22.04.00"
                    },
                    {
                        "nodeid": "xyz.domain.name",
                        "operating_system": "Microsoft Windows Server 2016  (Build 14393)",
                        "status": "Available",
                        "version": "20.22.04.00"
                    },
                    {
                        "nodeid": "xyz.domain.name",
                        "operating_system": "Microsoft Windows Server 2016  (Build 14393)",
                        "status": "Available",
                        "version": "20.22.04.00"
                    }
                ],
                "groups": [
                    {
                        "groupid": "abcd",
                        "nodes": "xyz.domain.name,abc.domain.name,123.domain.name"
                    },
                    {
                        "groupid": "wxyz",
                        "nodes": "efg.domain.name,456.domain.name"
                    }
                ]
            },
            {
                "server": "1234",
                "host": "1234.domain.name",
                "nodes": null
            }
        ]
    }
}

Thank for your help.感谢您的帮助。

I've managed to get the vscode.treeview populated with my json data.我已经设法让 vscode.treeview 填充了我的 json 数据。

custom treeview the source code will be open sourced and hosted on git soon.自定义树视图源代码将很快开源并托管在 git 上。 The code is quite large to accommodate for all the different objects.代码非常大,可以容纳所有不同的对象。 Now I have to figured out how to update the treeview when the refresh button is clicked and how to invoke a custom command on a treeview item.现在我必须弄清楚如何在单击刷新按钮时更新树视图以及如何在树视图项上调用自定义命令。 I will create new questions for this to better track it.我将为此创建新问题以更好地跟踪它。

Regards.问候。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM