当前位置:网站首页>Vs Code treeview TreeView
Vs Code treeview TreeView
2022-06-30 18:14:00 【Parzivval】
##VS code Tree view treeView
Create a tree view
To create a tree view, you first need to extend it to package.json Add relevant configuration to the file :
- establish activitybar:
"contributes": {
"viewsContainers": {
"activitybar": [
{
"id": "package-explorer",
"title": "Package Explorer",
"icon": "media/dep.svg"
}
]
}
}
Create an active bar under the view container , Create... Under the activity bar id by "package-explorer" View bar for
- Create view
"views": {
"package-explorer": [
{
"id": "nodeDependencies",
"name": "Node Dependencies",
"icon": "media/dep.svg",
"contextualTitle": "Package Explorer"
}
],
}
Create trees Item
According to need , Inheritance creation TreeItem
// Node object
export class Dependency extends vscode.TreeItem {
constructor(
public readonly label: string,
private readonly version: string,
public readonly collapsibleState: vscode.TreeItemCollapsibleState,
public readonly command?: vscode.Command
) {
super(label, collapsibleState);
this.tooltip = `${
this.label}-${
this.version}`;
this.description = this.version;
}
iconPath = {
light: path.join(__filename, '..', '..', 'resources', 'light', 'dependency.svg'),
dark: path.join(__filename, '..', '..', 'resources', 'dark', 'dependency.svg')
};
contextValue = 'dependency';
}
establish DataProvider
The object is treeItem
stay getChildren Function to recursively implement the tree view .
Its parameters element Is the current object , Initial tree when null , Find its child node when it is not empty .
/** * Project dependencies are organized into a document tree , And return to his documentation site for each dependency */
export class DepNodeProvider implements vscode.TreeDataProvider<Dependency> {
private _onDidChangeTreeData: vscode.EventEmitter<Dependency | undefined | void> = new vscode.EventEmitter<Dependency | undefined | void>();
readonly onDidChangeTreeData: vscode.Event<Dependency | undefined | void> = this._onDidChangeTreeData.event;
// workspace: Project file path
constructor(private workspaceRoot: string | undefined) {
workspaceRoot = path.join(workspaceRoot, 'package.json');
console.log(workspaceRoot);
}
refresh(): void {
this._onDidChangeTreeData.fire();
}
getTreeItem(element: Dependency): vscode.TreeItem {
return element;
}
// obtain treeItem Function of
getChildren(element?: Dependency): Thenable<Dependency[]> {
// If the path is empty
if (!this.workspaceRoot) {
vscode.window.showInformationMessage('No dependency in empty workspace');
return Promise.resolve([]);
}
// If you already have the current object , Spliced path , Get its file . The way to get sub files is to recursively search .
if (element) {
return Promise.resolve(this.getDepsInPackageJson(path.join(this.workspaceRoot, 'node_modules', element.label, 'package.json')));
// If there is no current object , To determine whether there is a direct existence under the project path package.json
// This step is actually used to initialize
} else {
const packageJsonPath = path.join(this.workspaceRoot, 'package.json');
// There is a direct... Under the project path package.json, Search through this path .
if (this.pathExists(packageJsonPath)) {
return Promise.resolve(this.getDepsInPackageJson(packageJsonPath));
// If still not , Just say no
} else {
vscode.window.showInformationMessage('Workspace has no package.json');
return Promise.resolve([]);
}
}
}
/** * Get... Through the path package Inside */
private getDepsInPackageJson(packageJsonPath: string): Dependency[] {
if (this.pathExists(packageJsonPath)) {
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
const toDep = (moduleName: string, version: string): Dependency => {
if (this.pathExists(path.join(this.workspaceRoot, 'node_modules', moduleName))) {
return new Dependency(moduleName, version, vscode.TreeItemCollapsibleState.Collapsed);
} else {
return new Dependency(moduleName, version, vscode.TreeItemCollapsibleState.None, {
command: 'extension.openPackageOnNpm',
title: '',
arguments: [moduleName]
});
}
};
const deps = packageJson.dependencies
? Object.keys(packageJson.dependencies).map(dep => toDep(dep, packageJson.dependencies[dep]))
: [];
const devDeps = packageJson.devDependencies
? Object.keys(packageJson.devDependencies).map(dep => toDep(dep, packageJson.devDependencies[dep]))
: [];
return deps.concat(devDeps);
} else {
return [];
}
}
// Function to determine whether the path exists
private pathExists(p: string): boolean {
try {
fs.accessSync(p);
} catch (err) {
return false;
}
return true;
}
}
Register for view DataProvider
const nodeDependenciesProvider = new DepNodeProvider(rootPath);
vscode.window.registerTreeDataProvider('nodeDependencies', nodeDependenciesProvider);
边栏推荐
- Deep understanding of JVM (II) - memory structure (II)
- 联想“双平台”运维解决方案 助力智慧医疗行业智慧管理能力全面提升
- Oneortwo bugs in "software testing" are small things, but security vulnerabilities are big things. We must pay attention to them
- 100 examples of bug records of unity development (the first example) -- shader failure or bug after packaging
- 基于SSH的通讯网络电子计费系统
- The company was jailed for nonstandard bug during the test ~ [cartoon version]
- Ardunio esp32 obtains real-time temperature and humidity in mqtt protocol (DH11)
- Tensorflow2 深度学习十必知
- 墨天轮沙龙 | 清华乔嘉林:Apache IoTDB,源于清华,建设开源生态之路
- Several points in MySQL that are easy to ignore and forget
猜你喜欢

5g has been in business for three years. Where will innovation go in the future?

Alexnet of CNN classic network (Theory)

Vue3 reactive database

Redis (IV) - delete policy

Babbitt | yuanuniverse daily must read: minors ask for a refund after a reward. The virtual anchor says he is a big wrongdoer. How do you think of this regulatory loophole

腾讯持久化框架MMKV原理探究

The new Post-00 Software Test Engineer in 2022 is a champion

Redis (VIII) - enterprise level solution (I)

Rainbow Brackets 插件的快捷键

基于SSH的客户关系CRM管理系统
随机推荐
Shortcut keys for the rainbow brackets plug-in
[BJDCTF2020]The mystery of ip|[CISCN2019 华东南赛区]Web11|SSTI注入
C语言结构体
Deep understanding of JVM (II) - memory structure (II)
Animesr: learnable degradation operator and new real world animation VSR dataset
Redis (I) - data type
[bjdctf2020]the mystery of ip|[ciscn2019 southeast China division]web11|ssti injection
基于SSH的通讯网络电子计费系统
Php8.0 environment detailed installation tutorial
Deep understanding of JVM (VI) -- garbage collection (III)
Babbitt | yuanuniverse daily must read: minors ask for a refund after a reward. The virtual anchor says he is a big wrongdoer. How do you think of this regulatory loophole
MSF后渗透总结
Post MSF infiltration summary
ASP. Net generate verification code
Rainbow Brackets 插件的快捷键
Three methods of modifying time zone in MySQL
又一篇CVPR 2022论文被指抄袭,平安保险研究者控诉IBM苏黎世团队
分布式场景下,你知道有几种生成唯一ID的方式嘛?
Spin lock exploration
腾讯云安装mysql数据库