Skip to content

Commit

Permalink
Merge pull request #28 from isfopo:issue/Refresh-when-env-file-changes
Browse files Browse the repository at this point in the history
Issue/Refresh-when-env-file-changes
  • Loading branch information
isfopo authored Jul 14, 2024
2 parents 48130b3 + 2d22bea commit f712248
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 56 deletions.
89 changes: 39 additions & 50 deletions src/EnvironmentTreeviewProvider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as vscode from "vscode";
import { parseEnvironmentContent, replace } from "./helpers/parse";
import type { EnvironmentContent, EnvironmentKeyValue } from "./types";
import { EnvironmentWorkspaceFolderTreeItem } from "./classes/TreeItems/EnvironmentWorkspaceFolderTreeItem";
import { EnvironmentKeyValueTreeItem } from "./classes/TreeItems/EnvironmentKeyValueTreeItem";
import { EnvironmentFileTreeItem } from "./classes/TreeItems/EnvironmentFileTreeItem";

export class EnvironmentTreeviewProvider
implements vscode.TreeDataProvider<vscode.TreeItem>
Expand Down Expand Up @@ -40,6 +42,42 @@ export class EnvironmentTreeviewProvider
});
}

// Listen to file changes
vscode.workspace.onDidChangeTextDocument((e) => {
if (e.document.uri.path.includes(".env")) {
this.refresh();
}
});

// Listen to file creation
vscode.workspace.onDidCreateFiles((event) => {
for (const file of event.files) {
if (file.path.includes(".env")) {
this.refresh();
}
}
});

// Listen to file deletion
vscode.workspace.onDidDeleteFiles((event) => {
for (const file of event.files) {
if (file.path.includes(".env")) {
this.refresh();
}
}
});

// Listen to file renaming
vscode.workspace.onDidRenameFiles((event) => {
for (const file of event.files) {
if (
file.newUri.path.includes(".env") ||
file.oldUri.path.includes(".env")
) {
this.refresh();
}
}
});
return this;
}

Expand Down Expand Up @@ -161,52 +199,3 @@ export class EnvironmentTreeviewProvider
);
}
}

export enum EnvironmentValueType {
string = "string",
bool = "bool",
}

export class EnvironmentWorkspaceFolderTreeItem extends vscode.TreeItem {
constructor(
public readonly folder: vscode.WorkspaceFolder,
public readonly collapsibleState: vscode.TreeItemCollapsibleState = vscode
.TreeItemCollapsibleState.Collapsed
) {
super(folder.name, collapsibleState);
this.contextValue = "workspaceFolder";
this.tooltip = this.folder.uri.fsPath;
this.iconPath = vscode.ThemeIcon.Folder;
}
}

export class EnvironmentKeyValueTreeItem extends vscode.TreeItem {
constructor(
public readonly key: string,
public readonly value: EnvironmentKeyValue,
public readonly parent: EnvironmentFileTreeItem,
public readonly collapsibleState: vscode.TreeItemCollapsibleState = vscode
.TreeItemCollapsibleState.None
) {
super(key, collapsibleState);
this.contextValue = `keyValue-${this.value.type}`;
this.tooltip = this.value.value;
this.description = this.value.value;
}
}

export class EnvironmentFileTreeItem extends vscode.TreeItem {
constructor(
public readonly name: string,
public readonly uri: vscode.Uri,
public readonly content: EnvironmentContent,
public readonly collapsibleState: vscode.TreeItemCollapsibleState = vscode
.TreeItemCollapsibleState.Collapsed
) {
super(name, collapsibleState);
this.contextValue = "file";
this.uri = uri;
this.tooltip = this.uri.fsPath;
this.iconPath = vscode.ThemeIcon.File;
}
}
18 changes: 18 additions & 0 deletions src/classes/TreeItems/EnvironmentFileTreeItem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import * as vscode from "vscode";
import type { EnvironmentContent } from "../../types";

export class EnvironmentFileTreeItem extends vscode.TreeItem {
constructor(
public readonly name: string,
public readonly uri: vscode.Uri,
public readonly content: EnvironmentContent,
public readonly collapsibleState: vscode.TreeItemCollapsibleState = vscode
.TreeItemCollapsibleState.Collapsed
) {
super(name, collapsibleState);
this.contextValue = "file";
this.uri = uri;
this.tooltip = this.uri.fsPath;
this.iconPath = vscode.ThemeIcon.File;
}
}
18 changes: 18 additions & 0 deletions src/classes/TreeItems/EnvironmentKeyValueTreeItem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import * as vscode from "vscode";
import type { EnvironmentKeyValue } from "../../types";
import { EnvironmentFileTreeItem } from "./EnvironmentFileTreeItem";

export class EnvironmentKeyValueTreeItem extends vscode.TreeItem {
constructor(
public readonly key: string,
public readonly value: EnvironmentKeyValue,
public readonly parent: EnvironmentFileTreeItem,
public readonly collapsibleState: vscode.TreeItemCollapsibleState = vscode
.TreeItemCollapsibleState.None
) {
super(key, collapsibleState);
this.contextValue = `keyValue-${this.value.type}`;
this.tooltip = this.value.value;
this.description = this.value.value;
}
}
14 changes: 14 additions & 0 deletions src/classes/TreeItems/EnvironmentWorkspaceFolderTreeItem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import * as vscode from "vscode";

export class EnvironmentWorkspaceFolderTreeItem extends vscode.TreeItem {
constructor(
public readonly folder: vscode.WorkspaceFolder,
public readonly collapsibleState: vscode.TreeItemCollapsibleState = vscode
.TreeItemCollapsibleState.Collapsed
) {
super(folder.name, collapsibleState);
this.contextValue = "workspaceFolder";
this.tooltip = this.folder.uri.fsPath;
this.iconPath = vscode.ThemeIcon.Folder;
}
}
10 changes: 4 additions & 6 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import * as vscode from "vscode";
import {
EnvironmentFileTreeItem,
EnvironmentKeyValueTreeItem,
EnvironmentTreeviewProvider,
EnvironmentWorkspaceFolderTreeItem,
} from "./EnvironmentTreeviewProvider";
import { EnvironmentTreeviewProvider } from "./EnvironmentTreeviewProvider";
import { EnvironmentWorkspaceFolderTreeItem } from "./classes/TreeItems/EnvironmentWorkspaceFolderTreeItem";
import { EnvironmentFileTreeItem } from "./classes/TreeItems/EnvironmentFileTreeItem";
import { EnvironmentKeyValueTreeItem } from "./classes/TreeItems/EnvironmentKeyValueTreeItem";

export function activate(context: vscode.ExtensionContext) {
const treeDataProvider = new EnvironmentTreeviewProvider(context).register();
Expand Down

0 comments on commit f712248

Please sign in to comment.