Skip to content

Commit

Permalink
refactor(workspace-tree): Refine event and dispose handling (#420)
Browse files Browse the repository at this point in the history
Less dependencies for BazelWorkspaceTreeProvider, making future
testing changes simpler.
  • Loading branch information
cwahbong authored Oct 20, 2024
1 parent d567e21 commit b01fdbc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 25 deletions.
4 changes: 3 additions & 1 deletion src/extension/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ import { activateWrapperCommands } from "./bazel_wrapper_commands";
* @param context The extension context.
*/
export async function activate(context: vscode.ExtensionContext) {
const workspaceTreeProvider = new BazelWorkspaceTreeProvider(context);
const workspaceTreeProvider = new BazelWorkspaceTreeProvider();
context.subscriptions.push(workspaceTreeProvider);

const codeLensProvider = new BazelBuildCodeLensProvider(context);
const buildifierDiagnostics = new BuildifierDiagnosticsManager();
const completionItemProvider = new BazelCompletionItemProvider();
Expand Down
43 changes: 19 additions & 24 deletions src/workspace-tree/bazel_workspace_tree_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,49 +22,38 @@ import { BazelWorkspaceFolderTreeItem } from "./bazel_workspace_folder_tree_item
* interface.
*/
export class BazelWorkspaceTreeProvider
implements vscode.TreeDataProvider<IBazelTreeItem>
implements vscode.TreeDataProvider<IBazelTreeItem>, vscode.Disposable
{
public onDidChangeTreeData: vscode.Event<IBazelTreeItem | void>;

/** Fired when BUILD files change in the workspace. */
private onDidChangeTreeDataEmitter =
private readonly onDidChangeTreeDataEmitter =
new vscode.EventEmitter<IBazelTreeItem | void>();
public readonly onDidChangeTreeData = this.onDidChangeTreeDataEmitter.event;

/** The cached toplevel items. */
private workspaceFolderTreeItems: BazelWorkspaceFolderTreeItem[] | undefined;

private disposables: vscode.Disposable[] = [];

/**
* Initializes a new tree provider with the given extension context.
*
* @param context The VS Code extension context.
*/
constructor(private context: vscode.ExtensionContext) {
this.onDidChangeTreeData = this.onDidChangeTreeDataEmitter.event;

const buildWatcher = vscode.workspace.createFileSystemWatcher(
constructor() {
const buildFilesWatcher = vscode.workspace.createFileSystemWatcher(
"**/{BUILD,BUILD.bazel}",
false,
false,
false,
);
buildWatcher.onDidChange(
() => this.onBuildFilesChanged(),
this,
context.subscriptions,
);
buildWatcher.onDidCreate(
() => this.onBuildFilesChanged(),
this,
context.subscriptions,
);
buildWatcher.onDidDelete(
() => this.onBuildFilesChanged(),
this,
context.subscriptions,
this.disposables.push(
buildFilesWatcher,
buildFilesWatcher.onDidChange(() => this.onBuildFilesChanged()),
buildFilesWatcher.onDidCreate(() => this.onBuildFilesChanged()),
buildFilesWatcher.onDidDelete(() => this.onBuildFilesChanged()),
vscode.workspace.onDidChangeWorkspaceFolders(() => this.refresh()),
);

vscode.workspace.onDidChangeWorkspaceFolders(() => this.refresh(), this);

this.updateWorkspaceFolderTreeItems();
}

Expand Down Expand Up @@ -155,4 +144,10 @@ export class BazelWorkspaceTreeProvider
haveBazelWorkspace,
);
}

public dispose() {
for (const disposable of this.disposables) {
disposable.dispose();
}
}
}

0 comments on commit b01fdbc

Please sign in to comment.