当前位置:网站首页>VS code 树视图 treeView
VS code 树视图 treeView
2022-06-30 16:41:00 【Parzivval】
##VS code 树视图 treeView
创建树视图
创建树视图首先需要到扩展到package.json文件中去添加相关配置:
- 创建activitybar:
"contributes": {
"viewsContainers": {
"activitybar": [
{
"id": "package-explorer",
"title": "Package Explorer",
"icon": "media/dep.svg"
}
]
}
}
在视图容器下创建活动栏,在活动栏下面创建 id为"package-explorer"的视图栏
- 创建视图
"views": {
"package-explorer": [
{
"id": "nodeDependencies",
"name": "Node Dependencies",
"icon": "media/dep.svg",
"contextualTitle": "Package Explorer"
}
],
}
创建树Item
根据需要,继承创建TreeItem
// 节点对象
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';
}
创建DataProvider
对象为treeItem
在getChildren函数里面递归实现树视图。
其参数element是当前对象,为空时初始树,非空时找寻其孩子节点。
/** * 项目依赖整理成文件树形式,并且对每一个依赖返回他的文档网站 */
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:项目文件路径
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;
}
// 获取treeItem的函数
getChildren(element?: Dependency): Thenable<Dependency[]> {
// 如果路径为空
if (!this.workspaceRoot) {
vscode.window.showInformationMessage('No dependency in empty workspace');
return Promise.resolve([]);
}
// 如果已经有当前对象,拼接好路径,获取其文件。获取子文件的方式是递归搜索。
if (element) {
return Promise.resolve(this.getDepsInPackageJson(path.join(this.workspaceRoot, 'node_modules', element.label, 'package.json')));
// 如果没有当前对象,之间判断该项目路径下有没有直接存在的package.json
// 这一步的作用其实是初始化
} else {
const packageJsonPath = path.join(this.workspaceRoot, 'package.json');
// 项目路径下存在直接的package.json,则通过该路径进行搜索。
if (this.pathExists(packageJsonPath)) {
return Promise.resolve(this.getDepsInPackageJson(packageJsonPath));
// 如果依然没有,就说没有
} else {
vscode.window.showInformationMessage('Workspace has no package.json');
return Promise.resolve([]);
}
}
}
/** * 通过路径获取package里面的 */
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 [];
}
}
// 判断路径是否存在的函数
private pathExists(p: string): boolean {
try {
fs.accessSync(p);
} catch (err) {
return false;
}
return true;
}
}
为视图注册DataProvider
const nodeDependenciesProvider = new DepNodeProvider(rootPath);
vscode.window.registerTreeDataProvider('nodeDependencies', nodeDependenciesProvider);
边栏推荐
- [zero basic IOT pwn] environment construction
- Importing alicloud ECS locally to solve deployment problems
- Radio and television 5g officially set sail, attracting attention on how to apply the golden band
- MIT科技评论2022年35岁以下创新者名单发布,含AlphaFold作者等
- Advanced Mathematics (Seventh Edition) Tongji University General exercises one person solution
- MIT science and Technology Review released the list of innovators under the age of 35 in 2022, including alphafold authors, etc
- Exch: repair the missing system mailbox
- Send the injured baby for emergency medical treatment. Didi's driver ran five red lights in a row
- The secondary menu of the magic article system v5.4.0 supports the optimization of form display
- 基于SSM的新闻管理系统
猜你喜欢

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

Canvas mouse control gravity JS effect

K-line diagram must be read for quick start

流批一体在京东的探索与实践
![leetcode:1042. Do not plant flowers adjacent to each other [randomly fill in qualified + no contradiction will be formed behind + set.pop]](/img/eb/3264776ff46e4bff628fb84d94db88.png)
leetcode:1042. Do not plant flowers adjacent to each other [randomly fill in qualified + no contradiction will be formed behind + set.pop]

Word中添加代码块(转载)

Advanced Mathematics (Seventh Edition) Tongji University General exercises one person solution

Inventory in the first half of 2022: summary of major updates and technical points of 20+ mainstream databases

MIT science and Technology Review released the list of innovators under the age of 35 in 2022, including alphafold authors, etc

Plane intersection and plane equation
随机推荐
Redis (VIII) - enterprise level solution (I)
Do fresh students get a job or choose a job after graduation?
vue3 响应式数据库—— reactive
Splitting. JS text title slow loading JS effect
News management system based on SSM
编译生成busybox文件系统
MySQL之零碎知识点
The secondary menu of the magic article system v5.4.0 supports the optimization of form display
NFT铸造交易平台开发详情
Redis (II) -- persistence
【架构】1366- 如何画出一张优秀的架构图
【义修换届大礼包】
[sword finger offer] 53 - I. find the number I in the sorted array
【剑指Offer】53 - I. 在排序数组中查找数字 I
Redis (VII) - sentry
. Net ORM framework hisql practice - Chapter 1 - integrating hisql
Hyper-V: enable SR-IOV in virtual network
Small Tools(3) 集成Knife4j3.0.3接口文档
Ardunio esp32 DH11 real time uploading temperature and humidity Alibaba cloud self built mqtt
Canvas cloud shape animation