forked from fernandoescolar/vscode-solution-explorer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SolutionExplorerProvider.ts
179 lines (142 loc) · 5.91 KB
/
SolutionExplorerProvider.ts
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
import * as vscode from "vscode";
import * as fs from "./async/fs";
import * as path from "path";
import * as sln from "./tree";
import * as SolutionExplorerConfiguration from "./SolutionExplorerConfiguration";
import * as Utilities from "./model/Utilities";
import { SolutionFile } from "./model/Solutions";
import { IEventAggegator, EventTypes, IEvent, ISubscription, IFileEvent } from "./events";
import { ILogger, Logger } from "./log";
import { ITemplateEngine, TemplateEngine } from "./templates";
export class SolutionExplorerProvider implements vscode.TreeDataProvider<sln.TreeItem> {
private _logger: ILogger;
private _templateEngine: ITemplateEngine;
private subscription: ISubscription = null;
private children: sln.TreeItem[] = null;
private treeView: vscode.TreeView<sln.TreeItem> = null;
private _onDidChangeTreeData: vscode.EventEmitter<sln.TreeItem | undefined> = new vscode.EventEmitter<sln.TreeItem | undefined>();
readonly onDidChangeTreeData: vscode.Event<sln.TreeItem | undefined> = this._onDidChangeTreeData.event;
//onDidChangeActiveTextEditor
constructor(public workspaceRoot: string, public readonly eventAggregator: IEventAggegator) {
this._logger = new Logger(this.eventAggregator);
this._templateEngine = new TemplateEngine(workspaceRoot);
vscode.window.onDidChangeActiveTextEditor(() => this.onActiveEditorChanged());
vscode.window.onDidChangeVisibleTextEditors(data => this.onVisibleEditorsChanged(data));
}
public get logger(): ILogger {
return this._logger;
}
public get templateEngine(): ITemplateEngine {
return this._templateEngine;
}
public register() {
let showMode = SolutionExplorerConfiguration.getShowMode();
vscode.commands.executeCommand('setContext', 'solutionExplorer.loadedFlag', !false);
vscode.commands.executeCommand('setContext', 'solutionExplorer.viewInActivityBar', showMode === SolutionExplorerConfiguration.SHOW_MODE_ACTIVITYBAR);
vscode.commands.executeCommand('setContext', 'solutionExplorer.viewInExplorer', showMode === SolutionExplorerConfiguration.SHOW_MODE_EXPLORER);
vscode.commands.executeCommand('setContext', 'solutionExplorer.viewInNone', showMode === SolutionExplorerConfiguration.SHOW_MODE_NONE);
if (showMode !== SolutionExplorerConfiguration.SHOW_MODE_NONE) {
this.subscription = this.eventAggregator.subscribe(EventTypes.File, evt => this.onFileEvent(evt))
this.treeView = vscode.window.createTreeView('solutionExplorer', { treeDataProvider: this });
}
}
public unregister() {
if (SolutionExplorerConfiguration.getShowMode() !== SolutionExplorerConfiguration.SHOW_MODE_NONE) {
this.subscription.dispose();
this.subscription = null;
this.treeView.dispose();
this.treeView = null;
}
}
public refresh(item?: sln.TreeItem): void {
if (item) {
this._onDidChangeTreeData.fire(item);
} else {
this.children = null;
this._onDidChangeTreeData.fire();
}
}
public getTreeItem(element: sln.TreeItem): vscode.TreeItem {
return element;
}
public getChildren(element?: sln.TreeItem): Thenable<sln.TreeItem[]> {
if (!this.workspaceRoot) {
this.logger.log('No .sln found in workspace');
return Promise.resolve([]);
}
if (element)
return element.getChildren();
if (!element && this.children)
return Promise.resolve(this.children);
if (!element && !this.children) {
return this.createSolutionItems();
}
return null;
}
public getParent(element: sln.TreeItem): sln.TreeItem {
return element.parent;
}
private async selectFile(filepath: string): Promise<void> {
if (!this.children) return;
for(let i = 0; i < this.children.length; i ) {
let result = await this.children[i].search(filepath);
if (result) {
this.selectTreeItem(result);
return;
}
}
}
private selectTreeItem(element: sln.TreeItem): void {
if (this.treeView) {
this.treeView.reveal(element, { select: true, focus: true });
}
}
private async createSolutionItems(): Promise<sln.TreeItem[]> {
this.children = [];
let solutionPaths = await Utilities.searchFilesInDir(this.workspaceRoot, '.sln');
if (solutionPaths.length <= 0) {
let altFolders = SolutionExplorerConfiguration.getAlternativeSolutionFolders();
for (let i = 0; i < altFolders.length; i ) {
let altSolutionPaths = await Utilities.searchFilesInDir(path.join(this.workspaceRoot, altFolders[i]), '.sln');
solutionPaths = [ ...solutionPaths, ...altSolutionPaths]
}
if (solutionPaths.length <= 0) {
this.children .push(await sln.CreateNoSolution(this, this.workspaceRoot));
return this.children;
}
}
for(let i = 0; i < solutionPaths.length; i ) {
let s = solutionPaths[i];
let solution = await SolutionFile.Parse(s);
let item = await sln.CreateFromSolution(this, solution);
this.children.push(item);
}
if (this.children.length > 0) this.checkTemplatesToInstall();
return this.children;
}
private onFileEvent(event: IEvent): void {
let fileEvent = <IFileEvent> event;
if (path.dirname(fileEvent.path) == this.workspaceRoot
&& fileEvent.path.endsWith('.sln')) {
this.children = null;
this.refresh();
}
}
private async checkTemplatesToInstall(): Promise<void> {
if (SolutionExplorerConfiguration.getCreateTemplateFolderQuestion() && !(await this.templateEngine.existsTemplates())) {
let option = await vscode.window.showWarningMessage("Would you like to create the vscode-solution-explorer templates folder?", 'Yes', 'No');
if (option !== null && option !== undefined && option == 'Yes') {
await this.templateEngine.creteTemplates();
}
}
}
private onActiveEditorChanged(): void {
let shouldExecute = SolutionExplorerConfiguration.getTrackActiveItem();
if (!shouldExecute) return;
if (!vscode.window.activeTextEditor) return;
if (vscode.window.activeTextEditor.document.uri.scheme !== 'file') return;
this.selectFile(vscode.window.activeTextEditor.document.uri.fsPath);
}
private onVisibleEditorsChanged(editors: vscode.TextEditor[]): void {
}
}