diff --git a/package.json b/package.json index 66145a4..5f63fc4 100644 --- a/package.json +++ b/package.json @@ -85,8 +85,12 @@ }, "viewsWelcome": [ { - "view": "environments-sidebar-view", - "contents": "No .env files found." + "view": "environments-sidebar", + "contents": "No environment files found. \n[Create file](command:environments.create)" + }, + { + "view": "environments-explorer", + "contents": "No environment files found. \n[Create file](command:environments.create)" } ], "viewsContainers": { @@ -143,4 +147,4 @@ "webpack": "^5.92.1", "webpack-cli": "^5.1.4" } -} +} \ No newline at end of file diff --git a/src/EnvironmentTreeviewProvider.ts b/src/EnvironmentTreeviewProvider.ts index 122f206..fd80378 100644 --- a/src/EnvironmentTreeviewProvider.ts +++ b/src/EnvironmentTreeviewProvider.ts @@ -7,33 +7,44 @@ export class EnvironmentTreeviewProvider { context: vscode.ExtensionContext; + private _onDidChangeTreeData: vscode.EventEmitter< + vscode.TreeItem | undefined | void + > = new vscode.EventEmitter(); + + readonly onDidChangeTreeData: vscode.Event< + vscode.TreeItem | undefined | void + > = this._onDidChangeTreeData.event; + constructor(context: vscode.ExtensionContext) { this.context = context; } register() { - vscode.window.createTreeView("environments-sidebar", { - treeDataProvider: this, - }); - - const tree = vscode.window.createTreeView("environments-explorer", { - treeDataProvider: this, - }); - - tree.onDidChangeSelection(async (e) => { - const selected = e.selection[0]; - if (selected instanceof EnvironmentKeyValueTreeItem) { - await vscode.window.showTextDocument( - await vscode.workspace.openTextDocument(selected.parent.uri) - ); - } - }); + const trees = [ + vscode.window.createTreeView("environments-sidebar", { + treeDataProvider: this, + }), + vscode.window.createTreeView("environments-explorer", { + treeDataProvider: this, + }), + ]; + + for (const tree of trees) { + tree.onDidChangeSelection(async (e): Promise => { + const selected = e.selection[0]; + if (selected instanceof EnvironmentKeyValueTreeItem) { + await vscode.window.showTextDocument( + await vscode.workspace.openTextDocument(selected.parent.uri) + ); + } + }); + } return this; } - create(workspace: string, fileName: string) { - vscode.workspace.fs.writeFile( + async create(workspace: string, fileName: string) { + await vscode.workspace.fs.writeFile( vscode.Uri.joinPath( vscode.Uri.from({ path: workspace, @@ -53,21 +64,17 @@ export class EnvironmentTreeviewProvider content += `${key}="${value}"\n`; - vscode.workspace.fs.writeFile( + await vscode.workspace.fs.writeFile( element.uri, new TextEncoder().encode(content) ); + + this.refresh(); } flip(element: EnvironmentKeyValueTreeItem) { this.edit(element, element.value.value === "true" ? "false" : "true"); } - private _onDidChangeTreeData: vscode.EventEmitter< - vscode.TreeItem | undefined | void - > = new vscode.EventEmitter(); - readonly onDidChangeTreeData: vscode.Event< - vscode.TreeItem | undefined | void - > = this._onDidChangeTreeData.event; refresh() { this._onDidChangeTreeData?.fire(); @@ -78,7 +85,7 @@ export class EnvironmentTreeviewProvider await vscode.workspace.fs.readFile(element.parent.uri) ); - vscode.workspace.fs.writeFile( + await vscode.workspace.fs.writeFile( element.parent.uri, new TextEncoder().encode(replace(content, element.key, input)) );