Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add isAzExtTreeItem util #1280

Merged
merged 3 commits into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions utils/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,9 @@ export declare abstract class AzExtTreeItem implements IAzExtTreeItem {
public resolveTooltip?(): Promise<string | MarkdownString>;
}

export declare function isAzExtTreeItem(maybeTreeItem: unknown): maybeTreeItem is AzExtTreeItem;
export declare function isAzExtParentTreeItem(maybeParentTreeItem: unknown): maybeParentTreeItem is AzExtParentTreeItem;

export interface IGenericTreeItemOptions {
id?: string;
label: string;
Expand Down
1 change: 1 addition & 0 deletions utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ export * from './utils/contextUtils';
export * from './activityLog/activities/ExecuteActivity';
export * from './getAzExtResourceType';
export * from './AzExtResourceType';
export * from './tree/isAzExtParentTreeItem';
// 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
3 changes: 2 additions & 1 deletion utils/src/tree/AzExtParentTreeItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import { localize } from '../localize';
import { randomUtils } from '../utils/randomUtils';
import { AzExtTreeItem } from './AzExtTreeItem';
import { GenericTreeItem } from './GenericTreeItem';
import { IAzExtParentTreeItemInternal, isAzExtParentTreeItem } from './InternalInterfaces';
import { IAzExtParentTreeItemInternal } from './InternalInterfaces';
import { isAzExtParentTreeItem } from './isAzExtParentTreeItem';
import { runWithLoadingNotification } from './runWithLoadingNotification';
import { loadMoreLabel } from './treeConstants';

Expand Down
3 changes: 2 additions & 1 deletion utils/src/tree/AzExtTreeDataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import { AzExtParentTreeItem, InvalidTreeItem } from './AzExtParentTreeItem';
import { AzExtTreeItem } from './AzExtTreeItem';
import { CollapsibleStateTracker } from './CollapsibleStateTracker';
import { GenericTreeItem } from './GenericTreeItem';
import { IAzExtTreeDataProviderInternal, isAzExtParentTreeItem } from './InternalInterfaces';
import { IAzExtTreeDataProviderInternal } from './InternalInterfaces';
import { isAzExtParentTreeItem } from './isAzExtParentTreeItem';
import { runWithLoadingNotification } from './runWithLoadingNotification';
import { loadMoreLabel } from './treeConstants';

Expand Down
7 changes: 5 additions & 2 deletions utils/src/tree/AzExtTreeItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ import * as types from '../../index';
import { NotImplementedError } from '../errors';
import { localize } from '../localize';
import { nonNullProp } from '../utils/nonNull';
import { IAzExtParentTreeItemInternal, IAzExtTreeDataProviderInternal, isAzExtParentTreeItem } from "./InternalInterfaces";
import { IAzExtParentTreeItemInternal, IAzExtTreeDataProviderInternal } from "./InternalInterfaces";
import { settingUtils } from '../utils/settingUtils';
import { showContextValueSetting } from '../constants';
import { isAzExtParentTreeItem } from './isAzExtParentTreeItem';

export abstract class AzExtTreeItem implements types.AzExtTreeItem {
public readonly _isAzExtTreeItem = true;

//#region Properties implemented by base class
public abstract label: string;
public abstract contextValue: string;
Expand Down Expand Up @@ -120,7 +123,7 @@ export abstract class AzExtTreeItem implements types.AzExtTreeItem {
}

public get tooltip(): string | undefined {
if(process.env.DEBUGTELEMETRY === 'v' && !!settingUtils.getWorkspaceSetting<unknown>(showContextValueSetting)) {
if (process.env.DEBUGTELEMETRY === 'v' && !!settingUtils.getWorkspaceSetting<unknown>(showContextValueSetting)) {
return `Context: "${this.contextValue}"`;
} else {
return this._tooltip;
Expand Down
12 changes: 3 additions & 9 deletions utils/src/tree/InternalInterfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@

import { EventEmitter } from 'vscode';
import * as types from '../../index';
import { AzExtTreeItem } from './AzExtTreeItem';
import { AzExtParentTreeItem } from './AzExtParentTreeItem';
import type { AzExtTreeItem } from './AzExtTreeItem';
import { CollapsibleStateTracker } from './CollapsibleStateTracker';

// Interfaces for methods on the tree that aren't exposed outside of this package
// We can't reference the classes directly because it would result in circular dependencies

export interface IAzExtParentTreeItemInternal extends types.AzExtParentTreeItem, AzExtTreeItem {
export interface IAzExtParentTreeItemInternal extends AzExtParentTreeItem {
_isAzExtParentTreeItem: boolean;
parent: IAzExtParentTreeItemInternal | undefined;
treeDataProvider: IAzExtTreeDataProviderInternal;
Expand All @@ -24,10 +25,3 @@ export interface IAzExtTreeDataProviderInternal extends types.AzExtTreeDataProvi
refreshUIOnly(treeItem: AzExtTreeItem | undefined): void;
readonly collapsibleStateTracker: CollapsibleStateTracker | undefined;
}

/**
* Using instanceof AzExtParentTreeItem causes issues whenever packages are linked for dev testing. Instead, check _isAzExtParentTreeItem
*/
export function isAzExtParentTreeItem(item: {}): boolean {
return !!(<IAzExtParentTreeItemInternal>item)._isAzExtParentTreeItem;
}
19 changes: 19 additions & 0 deletions utils/src/tree/isAzExtParentTreeItem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import type { AzExtParentTreeItem } from "./AzExtParentTreeItem";
import type { AzExtTreeItem } from "./AzExtTreeItem";
import type { IAzExtParentTreeItemInternal } from "./InternalInterfaces";

/**
* Using instanceof AzExtParentTreeItem causes issues since each extension has their own version of the utils. Instead, check _isAzExtParentTreeItem
*/
export function isAzExtParentTreeItem(maybeParentTreeItem: unknown): maybeParentTreeItem is AzExtParentTreeItem {
alexweininger marked this conversation as resolved.
Show resolved Hide resolved
return typeof maybeParentTreeItem === 'object' && (maybeParentTreeItem as IAzExtParentTreeItemInternal)._isAzExtParentTreeItem === true;
alexweininger marked this conversation as resolved.
Show resolved Hide resolved
}

export function isAzExtTreeItem(maybeTreeItem: unknown): maybeTreeItem is AzExtTreeItem {
return typeof maybeTreeItem === 'object' && (maybeTreeItem as AzExtTreeItem)._isAzExtTreeItem === true;
}