Skip to content

Commit

Permalink
utils: Add GenericParentTreeItem (#1635)
Browse files Browse the repository at this point in the history
  • Loading branch information
MicroFish91 authored Nov 30, 2023
1 parent 0bba4e3 commit f1d026f
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 13 deletions.
30 changes: 30 additions & 0 deletions utils/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,36 @@ export declare abstract class AzExtTreeItem implements IAzExtTreeItem {
export declare function isAzExtTreeItem(maybeTreeItem: unknown): maybeTreeItem is AzExtTreeItem;
export declare function isAzExtParentTreeItem(maybeParentTreeItem: unknown): maybeParentTreeItem is AzExtParentTreeItem;

export interface GenericParentTreeItemOptions {
childTypeLabel?: string;
contextValue: string;
iconPath?: TreeItemIconPath;
initialCollapsibleState?: TreeItemCollapsibleState;
label: string;
suppressMaskLabel?: boolean;

compareChildrenImpl?(item1: AzExtTreeItem, item2: AzExtTreeItem): number;
loadMoreChildrenImpl?(clearCache: boolean, context: IActionContext): Promise<AzExtTreeItem[]>;
}

/**
* A convenience class used for very basic parent tree items
*/
export declare class GenericParentTreeItem extends AzExtParentTreeItem {
public childTypeLabel?: string;
public contextValue: string;
public label: string;
public suppressMaskLabel?: boolean;

public readonly initialCollapsibleState: TreeItemCollapsibleState;

constructor(parent: AzExtParentTreeItem | undefined, options: GenericParentTreeItemOptions);

public compareChildrenImpl(item1: AzExtTreeItem, item2: AzExtTreeItem): number;
public hasMoreChildrenImpl(): boolean;
public loadMoreChildrenImpl(clearCache: boolean, context: IActionContext): Promise<AzExtTreeItem[]>;
}

export interface IGenericTreeItemOptions {
id?: string;
label: string;
Expand Down
25 changes: 13 additions & 12 deletions utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,30 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

export * from './activityLog/activities/ExecuteActivity';
export * from './activityLog/Activity';
export { createAzExtOutputChannel, createAzExtLogOutputChannel } from './AzExtOutputChannel';
export { createAzExtLogOutputChannel, createAzExtOutputChannel } from './AzExtOutputChannel';
export * from './AzExtTreeFileSystem';
export * from './DialogResponses';
export * from './activityLog/Activity';
export * from './activityLog/activities/ExecuteActivity';
export * from './callWithTelemetryAndErrorHandling';
export * from './createApiProvider';
export { createExperimentationService } from './createExperimentationService';
export * from './DialogResponses';
export * from './errors';
export * from './extensionUserAgent';
export { registerUIExtensionVariables } from './extensionVariables';
export { addExtensionValueToMask, callWithMaskHandling, maskValue } from './masking';
export * from './openReadOnlyContent';
export * from './parseError';
export * from './pickTreeItem/GenericQuickPickStep';
export * from './pickTreeItem/contextValue/ContextValueQuickPickStep';
export * from './pickTreeItem/contextValue/RecursiveQuickPickStep';
export * from './pickTreeItem/experiences/azureResourceExperience';
export * from './pickTreeItem/experiences/compatibility/PickTreeItemWithCompatibility';
export * from './pickTreeItem/experiences/contextValueExperience';
export * from './pickTreeItem/experiences/subscriptionExperience';
export * from './pickTreeItem/GenericQuickPickStep';
export * from './pickTreeItem/contextValue/ContextValueQuickPickStep';
export * from './pickTreeItem/contextValue/RecursiveQuickPickStep';
export * from './pickTreeItem/quickPickAzureResource/QuickPickAzureResourceStep';
export * from './pickTreeItem/quickPickAzureResource/QuickPickAzureSubscriptionStep';
export * from './pickTreeItem/quickPickAzureResource/QuickPickGroupStep';
export * from './pickTreeItem/quickPickAzureResource/QuickPickAzureResourceStep';
export * from './pickTreeItem/runQuickPickWizard';
export * from './registerCommand';
export * from './registerCommandWithTreeNodeUnwrapping';
Expand All @@ -35,23 +35,24 @@ export { registerReportIssueCommand } from './registerReportIssueCommand';
export * from './tree/AzExtParentTreeItem';
export * from './tree/AzExtTreeDataProvider';
export * from './tree/AzExtTreeItem';
export * from './tree/GenericParentTreeItem';
export * from './tree/GenericTreeItem';
export * from './tree/isAzExtTreeItem';
export * from './tree/v2/TreeElementStateManager';
export * from './tree/v2/createGenericElement';
export * from './utils/AzExtFsExtra';
export * from './utils/contextUtils';
export * from './utils/credentialUtils';
export * from './utils/findFreePort';
export * from './utils/nonNull';
export * from './utils/openUrl';
export * from './utils/randomUtils'
export * from './utils/randomUtils';
export * from './wizard/AzureNameStep';
export * from './wizard/AzureWizard';
export * from './wizard/AzureWizardExecuteStep';
export * from './wizard/AzureWizardPromptStep';
export * from './wizard/ConfirmPreviousInputStep';
export * from './wizard/DeleteConfirmationStep';
export * from './tree/v2/TreeElementStateManager';
export * from './tree/v2/createGenericElement';
// re-export api types and utils
export { apiUtils, GetApiOptions, AzureExtensionApi } from '@microsoft/vscode-azureresources-api';
export { AzureExtensionApi, GetApiOptions, apiUtils } from '@microsoft/vscode-azureresources-api';
// NOTE: The auto-fix action "source.organizeImports" does weird things with this file, but there doesn't seem to be a way to disable it on a per-file basis so we'll just let it happen
9 changes: 8 additions & 1 deletion utils/src/tree/AzExtTreeItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,14 @@ export abstract class AzExtTreeItem implements types.AzExtTreeItem {
return undefined;
}

if (this.treeDataProvider.collapsibleStateTracker) {
/**
* Reference: https://github.com/microsoft/vscode-azuretools/pull/1635/files#r1408222203
*
* `GenericParentTreeItem` has the option to be defined without a parent or tree data provider (for example, when utilized for the activity log)
* To avoid `nonNull` errors from being thrown, don't call the getter for `treeDataProvider` if none of the associated fields are populated.
*/
const hasTreeDataProvider: boolean = !!this._treeDataProvider || !!this.parent?.treeDataProvider;
if (hasTreeDataProvider && this.treeDataProvider.collapsibleStateTracker) {
return this.treeDataProvider.collapsibleStateTracker.getCollapsibleState(this);
}

Expand Down
54 changes: 54 additions & 0 deletions utils/src/tree/GenericParentTreeItem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { TreeItemCollapsibleState } from "vscode";
import * as types from '../../index';
import { AzExtParentTreeItem } from "./AzExtParentTreeItem";
import { AzExtTreeItem } from "./AzExtTreeItem";
import { IAzExtParentTreeItemInternal } from "./InternalInterfaces";

interface GenericParentTreeItemOptions {
childTypeLabel?: string;
contextValue: string;
iconPath?: types.TreeItemIconPath;
initialCollapsibleState?: TreeItemCollapsibleState;
label: string;
suppressMaskLabel?: boolean;

compareChildrenImpl?(item1: AzExtTreeItem, item2: AzExtTreeItem): number;
loadMoreChildrenImpl?(clearCache: boolean, context: types.IActionContext): Promise<AzExtTreeItem[]>;
}

export class GenericParentTreeItem extends AzExtParentTreeItem implements types.GenericParentTreeItem {
public childTypeLabel?: string;
public contextValue: string;
public label: string;
public suppressMaskLabel?: boolean;

public readonly initialCollapsibleState: TreeItemCollapsibleState;

constructor(parent: IAzExtParentTreeItemInternal | undefined, readonly options: GenericParentTreeItemOptions) {
super(parent);
this.childTypeLabel = options.childTypeLabel;
this.contextValue = options.contextValue;
this.iconPath = options.iconPath;
this.initialCollapsibleState = options.initialCollapsibleState === undefined ?
TreeItemCollapsibleState.Collapsed : options.initialCollapsibleState;
this.label = options.label;
this.suppressMaskLabel = options.suppressMaskLabel;

this.compareChildrenImpl = options.compareChildrenImpl ?? (() => 0);
}

public loadMoreChildrenImpl(clearCache: boolean, context: types.IActionContext): Promise<AzExtTreeItem[]> {
// Save and run off the saved tree item constructor options since we cannot assign the value directly during initialization
// (Abstract class inheritance requires loadMoreChildrenImpl definition be immediately defined)
return this.options.loadMoreChildrenImpl?.(clearCache, context) ?? Promise.resolve([]);
}

public hasMoreChildrenImpl(): boolean {
return false;
}
}

0 comments on commit f1d026f

Please sign in to comment.