From 65e0ed541fcf0d89c90aa8819d8cdab02ac64e5f Mon Sep 17 00:00:00 2001 From: Isaac Poole <55164207+isfopo@users.noreply.github.com> Date: Sun, 14 Jul 2024 11:42:42 -0400 Subject: [PATCH 1/3] refactor --- src/EnvironmentTreeviewProvider.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/EnvironmentTreeviewProvider.ts b/src/EnvironmentTreeviewProvider.ts index 122f206..4c17610 100644 --- a/src/EnvironmentTreeviewProvider.ts +++ b/src/EnvironmentTreeviewProvider.ts @@ -7,6 +7,14 @@ 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; } @@ -20,7 +28,7 @@ export class EnvironmentTreeviewProvider treeDataProvider: this, }); - tree.onDidChangeSelection(async (e) => { + tree.onDidChangeSelection(async (e): Promise => { const selected = e.selection[0]; if (selected instanceof EnvironmentKeyValueTreeItem) { await vscode.window.showTextDocument( @@ -62,12 +70,6 @@ export class EnvironmentTreeviewProvider 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(); From 4f9fc8440a26c6fd4d10fb5910660a15b76b45c1 Mon Sep 17 00:00:00 2001 From: Isaac Poole <55164207+isfopo@users.noreply.github.com> Date: Sun, 14 Jul 2024 11:54:09 -0400 Subject: [PATCH 2/3] shows create file button if no files --- package.json | 10 +++++++--- src/EnvironmentTreeviewProvider.ts | 10 ++++++---- 2 files changed, 13 insertions(+), 7 deletions(-) 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 4c17610..3b9970b 100644 --- a/src/EnvironmentTreeviewProvider.ts +++ b/src/EnvironmentTreeviewProvider.ts @@ -40,8 +40,8 @@ export class EnvironmentTreeviewProvider 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, @@ -61,10 +61,12 @@ 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) { @@ -80,7 +82,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)) ); From 6249a7c911bfeff09ede9ca18427a61900c54c01 Mon Sep 17 00:00:00 2001 From: Isaac Poole <55164207+isfopo@users.noreply.github.com> Date: Sun, 14 Jul 2024 11:56:38 -0400 Subject: [PATCH 3/3] opens file from sidebar view --- src/EnvironmentTreeviewProvider.ts | 35 ++++++++++++++++-------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/src/EnvironmentTreeviewProvider.ts b/src/EnvironmentTreeviewProvider.ts index 3b9970b..fd80378 100644 --- a/src/EnvironmentTreeviewProvider.ts +++ b/src/EnvironmentTreeviewProvider.ts @@ -20,22 +20,25 @@ export class EnvironmentTreeviewProvider } register() { - vscode.window.createTreeView("environments-sidebar", { - treeDataProvider: this, - }); - - const tree = vscode.window.createTreeView("environments-explorer", { - treeDataProvider: this, - }); - - 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) - ); - } - }); + 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; }