当前位置:网站首页>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);
边栏推荐
- 现在玩期货需要注意什么,在哪里开户比较安全,我第一次接触
- 报名“互联网+”大赛华为云命题,诸多礼品任你拿!
- Hcip (Huawei Senior Network Security Engineer) (Experiment 8) (MPLS basic experiment)
- Nielseniq welcomes dawn E. Norvell, head of retail lab, to accelerate the expansion of global retail strategy
- News management system based on SSM
- . Net ORM framework hisql practice - Chapter 1 - integrating hisql
- 5g has been in business for three years. Where will innovation go in the future?
- vue3 响应式数据库—— reactive
- K-line diagram interpretation and practical application skills (see position entry)
- Deep understanding of JVM (IV) - garbage collection (I)
猜你喜欢

Solve the problem of unable to connect to command metric stream and related problems in the hystrix dashboard

基于SSH的网上商城设计

Vue3 reactive database

vue3 响应式数据库—— reactive

K-line diagram interpretation and practical application skills (see position entry)

Redis (VI) - master-slave replication

AnimeSR:可学习的降质算子与新的真实世界动漫VSR数据集

Small tools (3) integration knife4j3.0.3 interface document

Deep understanding of JVM (IV) - garbage collection (I)

4 years of working experience, and you can't tell the five communication modes between multithreads. Can you believe it?
随机推荐
Deep understanding of JVM (II) - memory structure (II)
[binary tree] preorder traversal to construct binary search tree
Only black-and-white box test is required for test opening post? No, but also learn performance test
News management system based on SSM
Vue3 reactive database
Small tools (3) integration knife4j3.0.3 interface document
Post penetration file system + uploading and downloading files
巴比特 | 元宇宙每日必读:未成年人打赏后要求退款,虚拟主播称自己是大冤种,怎么看待这个监管漏洞?...
Redis (VIII) - enterprise level solution (I)
MSF后渗透总结
Daily interview 1 question - how to prevent CDN protection from being bypassed
Elastic 8.0: opening a new era of speed, scale, relevance and simplicity
IEEE TBD SCI影响因子提升至4.271,位列Q1区!
现在玩期货需要注意什么,在哪里开户比较安全,我第一次接触
Three methods of modifying time zone in MySQL
联想“双平台”运维解决方案 助力智慧医疗行业智慧管理能力全面提升
C语言结构体
ASP. Net generate verification code
[sword finger offer] sword finger offer 53 - ii Missing numbers from 0 to n-1
K-line diagram must be read for quick start