-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtree.ts
More file actions
405 lines (351 loc) · 14.3 KB
/
tree.ts
File metadata and controls
405 lines (351 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
import path from 'path';
import * as vscode from 'vscode';
import fs from 'fs';
import { getWorkspaceFolder, getExtensionPath } from '../api';
import { DecorationProvider } from './fileDecorationProvider';
/*
* contexType -> contextValue as following value:
* project_root,
* project_group,
* project_file,
* project_bsp
*/
// 全局变量记录当前选中的project_bsp项目
let currentSelectedBspItem: ProjectTreeItem | null = null;
// 添加树视图刷新事件发射器
let _onDidChangeTreeData: vscode.EventEmitter<ProjectTreeItem | undefined> | null = null;
// 设置当前选中的BSP项目(用于初始化时设置)
export function setCurrentSelectedBspItem(fn: string) {
// Create a minimal ProjectTreeItem for tracking
currentSelectedBspItem = new ProjectTreeItem(
path.basename(fn),
vscode.TreeItemCollapsibleState.None,
'project_bsp',
fn
);
}
export class ProjectTreeItem extends vscode.TreeItem {
children: ProjectTreeItem[];
fn: string = '';
name: string = '';
constructor(
public readonly label: string,
public readonly collapsibleState: vscode.TreeItemCollapsibleState,
public contextType: string,
fn?: string,
children?: ProjectTreeItem[]
) {
super(label, collapsibleState);
if (children) {
this.children = children;
}
else {
this.children = [];
}
this.contextValue = contextType;
this.name = label;
if (fn) {
if (path.isAbsolute(fn)) {
this.fn = fn;
}
else {
const workspaceFolders = vscode.workspace.workspaceFolders;
if (workspaceFolders) {
this.fn = path.join(workspaceFolders[0].uri.fsPath, fn);
}
}
if (contextType === 'project_file') {
this.command = {
title: this.name,
command: 'extension.clickProject',
tooltip: this.name,
arguments: [
this
]
};
}
else if (contextType === 'project_bsp') {
this.command = {
title: this.name,
command: 'extension.handleTreeItemClick',
tooltip: this.name,
arguments: [
this
]
};
// with resourceUri, the item will be rendered as a file icon
// then the item can be marked with a decoration
this.resourceUri = vscode.Uri.file(this.fn);
}
}
}
getChildren(): ProjectTreeItem[] | Thenable<ProjectTreeItem[]> {
if (this.children) {
return this.children;
}
return Promise.resolve([]);
}
}
/**
* Get the icon on the item left
* @param isDir
* @param value
* @returns
*/
export function getTreeIcon(isDir: boolean, value: string): string {
var icon = 'default_file.svg';
if (isDir) {
icon = "default_folder.svg";
} else {
if (value === "project") {
icon = "chip";
}
else if (value.endsWith(".c")) {
icon = "file_type_c.svg";
} else if (value.endsWith(".cpp") || value.endsWith(".cc") || value.endsWith(".cxx")) {
icon = "file_type_cpp.svg";
} else if (value.endsWith(".h")) {
icon = "file_type_cheader.svg";
} else if (value.endsWith(".s") || value.endsWith(".S")) {
icon = "file_type_assembly.png";
} else if (value.endsWith(".py") || value.endsWith("SConscript") || value.endsWith("SConstruct")) {
icon = "file_type_python.svg";
} else if (value.endsWith(".txt")) {
icon = "file_type_text.svg";
} else if (value.endsWith("Kconfig")) {
icon = "file_type_config.svg";
} else if (value.endsWith(".md")) {
icon = "file_type_markdown.svg";
} else if (value.endsWith(".cmake")) {
icon = "file_type_cmake.svg";
} else if (value.endsWith(".xmake")) {
icon = "file_type_xmake.svg";
} else if (value.endsWith(".js")) {
icon = "file_type_js.svg";
} else if (value.endsWith(".json")) {
icon = "file_type_json.svg";
} else if (value.endsWith(".patch")) {
icon = "file_type_patch.svg";
} else if (value.endsWith(".yaml")) {
icon = "file_type_yaml.svg";
} else if (value.endsWith(".bin")) {
icon = "file_type_binary.svg";
} else if (value.endsWith(".elf") || value.endsWith(".axf")) {
icon = "rt-project-elffile.svg";
} else if (value.endsWith(".jpg") || value.endsWith(".png") || value.endsWith(".ico") || value.endsWith(".jpeg")) {
icon = "file_type_image.svg";
} else if (value.endsWith(".lds") || value.endsWith(".icf") || value.endsWith(".sct")) {
icon = "rt-project-linkfile.svg";
}
}
return icon;
}
export function buildGroupsTree(node: any): ProjectTreeItem[] {
const projectItems: ProjectTreeItem[] = [];
let extensionPath:string = getExtensionPath() || ".";
let groups : Array<any> = node['Groups'];
groups.forEach((item, index) => {
let name = item['name'];
const treeItem = new ProjectTreeItem(name, vscode.TreeItemCollapsibleState.Collapsed, "project_group");
treeItem.iconPath = {
light: vscode.Uri.file(path.join(extensionPath, 'resources', 'images', "file_type_package.svg")),
dark: vscode.Uri.file(path.join(extensionPath, 'resources', 'images', "file_type_package.svg"))
};
let files: Array<any> = item['files'];
files.forEach((fn, index) => {
if (fn.startsWith('..')) {
let workspacePath = getWorkspaceFolder() || "";
fn = path.resolve(workspacePath, fn);
}
const fileItem = new ProjectTreeItem(path.basename(fn), vscode.TreeItemCollapsibleState.None, "project_file", fn);
let icon = getTreeIcon(false, fn);
fileItem.iconPath = {
light: vscode.Uri.file(path.join(extensionPath, 'resources', 'images', icon)),
dark: vscode.Uri.file(path.join(extensionPath, 'resources', 'images', icon))
};
fileItem.tooltip = fn;
treeItem.children.push(fileItem);
});
projectItems.push(treeItem);
});
return projectItems;
}
export function buildProjectTree(node: any): ProjectTreeItem[] {
const projectItems: ProjectTreeItem[] = [];
let extensionPath:string = getExtensionPath() || ".";
const rtthreadItem = new ProjectTreeItem('RT-Thread', vscode.TreeItemCollapsibleState.Expanded, 'project_root', node['RT-Thread']);
rtthreadItem.iconPath = new vscode.ThemeIcon('folder');
rtthreadItem.iconPath = {
light: vscode.Uri.file(path.join(extensionPath, 'resources', 'images', "default_folder.svg")),
dark: vscode.Uri.file(path.join(extensionPath, 'resources', 'images', "default_folder.svg"))
};
rtthreadItem.tooltip = rtthreadItem.fn;
listFolderTreeItem(rtthreadItem);
projectItems.push(rtthreadItem);
return projectItems;
}
export function buildBSPTree(node: any) {
let workspacePath = getWorkspaceFolder() || "";
let bspFolder = path.join(workspacePath, node['bsps'].folder);
return listStarsTreeItem(bspFolder, node['bsps']);
}
export function buildEmptyProjectTree() {
const projectItems: ProjectTreeItem[] = [];
const treeItem = new ProjectTreeItem("No .vscode/project.json found.", vscode.TreeItemCollapsibleState.None, "project_root");
treeItem.iconPath = new vscode.ThemeIcon("warning");
treeItem.tooltip = "Please update vscode settings to generate project.json file.";
projectItems.push(treeItem);
return projectItems;
}
export function listFolderTreeItem(treeItem: ProjectTreeItem) {
let children: ProjectTreeItem[] = [];
let parentPath = treeItem.fn;
const childFiles = fs.readdirSync(parentPath);
let items: any[] = [];
let dirs: any[] = [];
let files: any[] = [];
childFiles.forEach(function (item, index) {
let value = String(item);
if (parentPath.endsWith("rt-thread") && (value.startsWith("bsp") || value.startsWith("libcpu"))){
;
}
else {
const sufix = value.substring(value.lastIndexOf('.') + 1);
// filter files and dirs
let sufixs = ["__pycache__", "pyc", "ewp", "eww", "ewd", "uvopt", "uvoptx", "uvproj", "uvprojx", "bat", "sh", "targets"];
if (!(value.startsWith(".") || sufixs.indexOf(sufix) > -1)) {
let fPath = path.join(parentPath, item);
let stat = fs.statSync(fPath);
if (stat.isDirectory()) {
dirs.push(item);
} else {
files.push(item);
}
}
}
});
// push folders first
dirs.forEach(function (item, index) {
items.push(item);
}
);
// push files next
files.forEach(function (item, index) {
items.push(item);
}
);
items.forEach(function (item, index) {
let value = String(item);
let fPath = path.join(parentPath, item);
let stat = fs.statSync(fPath);
let icon = getTreeIcon(stat.isDirectory(), value);
let extensionPath:string = getExtensionPath() || ".";
if (fPath.startsWith('..')) {
fPath = path.resolve(fPath);
}
if (stat.isDirectory() === true)
{
let childItem = new ProjectTreeItem(value, vscode.TreeItemCollapsibleState.Collapsed, "project_folder", fPath);
childItem.iconPath = {
light: vscode.Uri.file(path.join(extensionPath, 'resources', 'images', icon)),
dark: vscode.Uri.file(path.join(extensionPath, 'resources', 'images', icon))
};
childItem.tooltip = fPath;
children.push(childItem);
}
else {
let childItem = new ProjectTreeItem(value, vscode.TreeItemCollapsibleState.None, "project_file", fPath);
childItem.iconPath = {
light: vscode.Uri.file(path.join(extensionPath, 'resources', 'images', icon)),
dark: vscode.Uri.file(path.join(extensionPath, 'resources', 'images', icon))
};
childItem.tooltip = fPath;
children.push(childItem);
}
});
treeItem.children = children;
}
export function listStarsTreeItem(bspFolder:string, node: any) {
let children: ProjectTreeItem[] = [];
node.stars.forEach(function (item: string) {
let parentPath = path.join(bspFolder, item);
let fPath = path.join(parentPath, "rtconfig.h");
if (fs.existsSync(fPath)) {
let parent = children;
let value = String(item);
let items = item.split(/[\/\\]/);
if (items.length >= 2) {
for (let i = 0; i < children.length; i++) {
if (children[i].label === items[0]) {
parent = children[i].children;
break;
}
}
// not found, create a new parent node
if (parent === children) {
const pnode: ProjectTreeItem = new ProjectTreeItem(items[0], vscode.TreeItemCollapsibleState.Collapsed, "project_folder", path.join(bspFolder, items[0]));
children.push(pnode);
parent = pnode.children;
}
// rename the node name
value = items.slice(1, items.length).join(path.sep);
}
let fn = parentPath;
let childItem = new ProjectTreeItem(value, vscode.TreeItemCollapsibleState.None, "project_bsp", fn);
childItem.iconPath = new vscode.ThemeIcon("chip");
childItem.tooltip = fn;
parent.push(childItem);
}
});
return children;
}
import {fastBuildProject, configProject, openTerminalProject, openProjectInNewWindow, setCurrentProject} from './cmd';
export function initProjectTree(context:vscode.ExtensionContext) {
vscode.commands.registerCommand('extension.fastBuildProject', (arg) => {
fastBuildProject(arg);
});
vscode.commands.registerCommand('extension.configProject', (arg) => {
configProject(arg);
});
vscode.commands.registerCommand('extension.openTerminalProject', (arg) => {
openTerminalProject(arg);
});
vscode.commands.registerCommand('extension.openProjectInNewWindow', (arg) => {
openProjectInNewWindow(arg);
});
// Add double-clicked
let lastClickTime = 0;
let lastClickedItem: ProjectTreeItem | null = null;
vscode.commands.registerCommand('extension.handleTreeItemClick', (item: ProjectTreeItem) => {
const currentTime = Date.now();
const doubleClickThreshold = 500; // 500ms内的两次点击被认为是双击
if (item.contextType === 'project_bsp') {
if (lastClickedItem === item && (currentTime - lastClickTime) < doubleClickThreshold) {
if (currentSelectedBspItem && currentSelectedBspItem.fn === item.fn) {
return;
}
// double clicked
if (currentSelectedBspItem && currentSelectedBspItem.fn !== item.fn) {
DecorationProvider.getInstance().unmarkFile(vscode.Uri.file(currentSelectedBspItem.fn));
}
currentSelectedBspItem = item;
DecorationProvider.getInstance().markFile(vscode.Uri.file(item.fn));
setCurrentProject(item);
if (_onDidChangeTreeData) {
_onDidChangeTreeData.fire(undefined);
}
// reset status
lastClickTime = 0;
lastClickedItem = null;
} else {
// one-clicked, just record it.
lastClickTime = currentTime;
lastClickedItem = item;
}
}
});
}
// 导出函数用于设置树视图刷新事件发射器
export function setTreeDataChangeEmitter(emitter: vscode.EventEmitter<ProjectTreeItem | undefined>) {
_onDidChangeTreeData = emitter;
}