rhamt-vscode-extension
Version:
RHAMT VSCode extension
89 lines (74 loc) • 3.25 kB
text/typescript
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { IRhamtNode, IRhamtTreeItem, IRhamtParentNode, RhamtTreeDataProvider } from "./index";
import { ArgumentError, NotImplementedError } from "./errors";
import { Uri } from "vscode";
import { localize } from "./localize";
export class RhamtNode<T extends IRhamtTreeItem = IRhamtTreeItem> implements IRhamtNode<T> {
public readonly treeItem: T;
public readonly parent: IRhamtParentNodeInternal | undefined;
private _temporaryDescription?: string;
public constructor(parent: IRhamtParentNodeInternal | undefined, treeItem: T) {
this.parent = parent;
this.treeItem = treeItem;
}
private get _effectiveDescription(): string | undefined {
return this._temporaryDescription || this.treeItem.description;
}
public get id(): string {
return this.treeItem.id || this.treeItem.label;
}
public get iconPath(): string | Uri | { light: string | Uri; dark: string | Uri } | undefined {
return undefined; // this._temporaryDescription ? loadingIconPath : this.treeItem.iconPath;
}
public get label(): string {
return this._effectiveDescription ? `${this.treeItem.label} (${this._effectiveDescription})` : this.treeItem.label;
}
public get treeDataProvider(): RhamtTreeDataProvider {
if (this.parent) {
return this.parent.treeDataProvider;
} else {
throw new ArgumentError(this);
}
}
public async refresh(): Promise<void> {
if (this.treeItem.refreshLabel) {
await this.treeItem.refreshLabel(this);
}
await this.treeDataProvider.refresh(this);
}
public includeInNodePicker(expectedContextValues: string[]): boolean {
return expectedContextValues.some((val: string) => {
return this.treeItem.contextValue === val ||
!this.treeItem.isAncestorOf ||
this.treeItem.isAncestorOf(val);
});
}
public async deleteNode(): Promise<void> {
await this.run(localize('deleting', 'Deleting...'), async () => {
if (this.treeItem.deleteTreeItem) {
await this.treeItem.deleteTreeItem(this);
if (this.parent) {
await this.parent.removeNodeFromCache(this);
}
} else {
throw new NotImplementedError('deleteTreeItem', this.treeItem);
}
});
}
public async run(description: string, callback: () => Promise<void>): Promise<void> {
this._temporaryDescription = description;
try {
await this.refresh();
await callback();
} finally {
this._temporaryDescription = undefined;
await this.refresh();
}
}
}
export interface IRhamtParentNodeInternal extends IRhamtParentNode {
removeNodeFromCache(node: RhamtNode): Promise<void>;
}