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

Refactor quick pick filters #333

Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
36d411d
App Resource filter
alexweininger Jul 14, 2022
3fba54d
Use encapsulation for filtering
alexweininger Jul 14, 2022
e9c7db3
Simplify options interface
alexweininger Jul 14, 2022
3f8e498
Fix Workspace view loadMore (#357)
alexweininger Aug 10, 2022
3aae964
Merge remote-tracking branch 'origin/bmw/philliphoff-resource-api-qui…
alexweininger Aug 11, 2022
3126a0e
Fixes
alexweininger Aug 11, 2022
cc2030c
Merge remote-tracking branch 'origin/philliphoff-resource-api' into a…
alexweininger Aug 13, 2022
17033f2
Use `AzExtResourceType` to simplify `azureUtils.ts` (#337)
alexweininger Aug 15, 2022
8d185b9
Merge remote-tracking branch 'origin/philliphoff-resource-api' into a…
alexweininger Aug 16, 2022
1211e02
working
alexweininger Aug 17, 2022
c16be00
Merge remote-tracking branch 'origin/main' into alex/resource-api-qp-…
alexweininger Aug 17, 2022
5336aa6
wip
alexweininger Aug 17, 2022
f11cba9
Fixup
alexweininger Aug 17, 2022
44f09c0
Fixup
alexweininger Aug 18, 2022
586cbe7
Sketch getApi() with options.
philliphoff Aug 19, 2022
0514154
Add telemetry for API calls.
philliphoff Aug 19, 2022
df89831
Merge main (#364)
alexweininger Aug 22, 2022
6db86c3
Update dev package
alexweininger Aug 22, 2022
6e9efec
Merge branch 'philliphoff-resource-api' into alex/resource-api-qp-fil…
alexweininger Aug 22, 2022
63ab62b
throw UserCancelledError
alexweininger Aug 22, 2022
ae88a7d
Merge remote-tracking branch 'origin/bmw/philliphoff-resource-api-qui…
alexweininger Aug 24, 2022
00fd216
Fixup
alexweininger Aug 24, 2022
74fbf59
Merge remote-tracking branch 'origin/bmw/philliphoff-resource-api-qui…
alexweininger Aug 24, 2022
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
32 changes: 32 additions & 0 deletions src/api/v2/quickPickWizard/AppResourceFilter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { AppResource, AppResourceFilter as AppResourceFilterOptions } from "@microsoft/vscode-azext-utils/hostapi";
import { Filter } from "../v2AzureResourcesApi";

export class AppResourceFilter implements Filter<AppResource> {

constructor(private readonly options: AppResourceFilterOptions) { }

public matches(resource: AppResource): boolean {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once microsoft/vscode-azuretools#1191 is merged, we can change the body of this method to

return resource.azureResourceType === this.azureResourceType

without having to make changes elsewhere.

if (this.options.type !== resource.type) {
return false;
}

if (this.options.kind && this.options.kind !== resource.kind) {
return false;
}

if (this.options.tags) {
for (const tag of Object.keys(this.options.tags)) {
if (this.options.tags[tag] !== resource.tags?.[tag]) {
return false;
}
}
}

return true;
}
}
38 changes: 21 additions & 17 deletions src/api/v2/quickPickWizard/ContextValueFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,29 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { ResourceModelBase } from '../v2AzureResourcesApi';
import { Filter, ResourceModelBase } from '../v2AzureResourcesApi';

export type ContextValueFilter = string | RegExp | (string | RegExp)[];
export class ContextValueFilter implements Filter<ResourceModelBase> {

export function matchesContextValueFilter(resource: ResourceModelBase, filter: ContextValueFilter): boolean {
if (!resource?.quickPickOptions) {
return false;
}
constructor(private readonly expectedContextValue: string | RegExp | (string | RegExp)[]) { }

matches(resource: ResourceModelBase): boolean {

const filterArray = Array.isArray(filter) ? filter : [filter];
const filterArray = Array.isArray(this.expectedContextValue) ? this.expectedContextValue : [this.expectedContextValue];

return filterArray.some(filter => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return resource.quickPickOptions!.contexts.some(contextValue => {
if (typeof filter === 'string') {
return filter === contextValue;
} else {
return filter.test(contextValue);
}
})
});
if (!resource.quickPickOptions) {
return false;
}

return filterArray.some(filter => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return resource.quickPickOptions!.contexts.some(contextValue => {
if (typeof filter === 'string') {
return filter === contextValue;
} else {
return filter.test(contextValue);
}
})
});
}
}
7 changes: 3 additions & 4 deletions src/api/v2/quickPickWizard/GenericQuickPickStep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@

import { AzureWizardPromptStep, IAzureQuickPickItem, NoResourceFoundError } from '@microsoft/vscode-azext-utils';
import * as vscode from 'vscode';
import { ResourceModelBase } from '../v2AzureResourcesApi';
import { ContextValueFilter, matchesContextValueFilter } from './ContextValueFilter';
import { Filter, ResourceModelBase } from '../v2AzureResourcesApi';
import { QuickPickWizardContext } from './QuickPickWizardContext';

export class GenericQuickPickStep<TModel extends ResourceModelBase> extends AzureWizardPromptStep<QuickPickWizardContext<TModel>> {
public supportsDuplicateSteps = true;

public constructor(
protected readonly treeDataProvider: vscode.TreeDataProvider<TModel>,
protected readonly contextValueFilter: ContextValueFilter,
protected readonly contextValueFilter: Filter<TModel>,
) {
super();
}
Expand All @@ -31,7 +30,7 @@ export class GenericQuickPickStep<TModel extends ResourceModelBase> extends Azur
protected async getPicks(wizardContext: QuickPickWizardContext<TModel>): Promise<IAzureQuickPickItem<TModel>[]> {
const children = (await this.treeDataProvider.getChildren(wizardContext.currentNode)) || [];

const matchingChildren = children.filter(c => matchesContextValueFilter(c, this.contextValueFilter));
const matchingChildren = children.filter(this.contextValueFilter.matches);
const nonLeafChildren = children.filter(c => c.quickPickOptions?.isLeaf === false);

let promptChoices: TModel[];
Expand Down
55 changes: 25 additions & 30 deletions src/api/v2/quickPickWizard/QuickPickAppResourceStep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,34 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { AzureWizardPromptStep, IAzureQuickPickItem, IWizardOptions } from '@microsoft/vscode-azext-utils';
import { PickAppResourceOptions } from '@microsoft/vscode-azext-utils/hostapi';
import { AzureWizardPromptStep, IAzureQuickPickItem, IAzureQuickPickOptions, IWizardOptions } from '@microsoft/vscode-azext-utils';
import { AppResource } from '@microsoft/vscode-azext-utils/hostapi';
import { BranchDataProviderManager } from '../../../tree/v2/providers/BranchDataProviderManager';
import { ApplicationResourceProviderManager } from '../providers/ApplicationResourceProviderManager';
import { ApplicationResource, ResourceModelBase } from '../v2AzureResourcesApi';
import { matchesContextValueFilter } from './ContextValueFilter';
import { ApplicationResource, Filter, ResourceModelBase } from '../v2AzureResourcesApi';
import { QuickPickAppResourceWizardContext } from './QuickPickAppResourceWizardContext';
import { RecursiveQuickPickStep } from './RecursiveQuickPickStep';

export interface PickAppResourceOptions2 extends IAzureQuickPickOptions {
/**
* Set this to pick a child of the selected app resource
*/
childFilter?: Filter<ResourceModelBase>;

/**
* Whether `AppResourceTreeItem`s should be resolved before displaying them as quick picks, or only once one has been selected
* If a client extension needs to change label/description/something visible on the quick pick via `resolve`, set to true,
* otherwise set to false. Default will be false.
*/
resolveQuickPicksBeforeDisplay?: boolean;
}

export class QuickPickAppResourceStep<TModel extends ResourceModelBase> extends AzureWizardPromptStep<QuickPickAppResourceWizardContext<TModel>> {
public constructor(
private readonly resourceProviderManager: ApplicationResourceProviderManager,
private readonly branchDataProviderManager: BranchDataProviderManager,
private readonly options: PickAppResourceOptions
private readonly filter?: Filter<AppResource> | Filter<AppResource>[],
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I promoted filter from inside options to a top level parameter because in most cases we will want to filter the app resources.

private readonly options?: PickAppResourceOptions2
) {
super();
}
Expand All @@ -39,8 +53,8 @@ export class QuickPickAppResourceStep<TModel extends ResourceModelBase> extends
}

public async getSubWizard(wizardContext: QuickPickAppResourceWizardContext<TModel>): Promise<IWizardOptions<QuickPickAppResourceWizardContext<TModel>> | undefined> {
if (this.options.expectedChildContextValue) {
if (matchesContextValueFilter(wizardContext.currentNode as TModel, this.options.expectedChildContextValue)) {
if (this.options?.childFilter) {
if (this.options.childFilter?.matches(wizardContext.currentNode as TModel)) {
return undefined;
}

Expand All @@ -49,7 +63,7 @@ export class QuickPickAppResourceStep<TModel extends ResourceModelBase> extends
return {
hideStepCount: true,
promptSteps: [
new RecursiveQuickPickStep(bdp, this.options.expectedChildContextValue),
new RecursiveQuickPickStep(bdp, this.options.childFilter),
],
};
}
Expand All @@ -65,30 +79,11 @@ export class QuickPickAppResourceStep<TModel extends ResourceModelBase> extends
}

private matchesAppResource(resource: ApplicationResource): boolean {
if (!this.options.filter) {
if (!this.filter) {
return true;
}

const filterArray = Array.isArray(this.options.filter) ? this.options.filter : [this.options.filter];

return filterArray.some(filter => {
if (filter.type !== resource.type) {
return false;
}

if (filter.kind && filter.kind !== resource.kind) {
return false;
}

if (filter.tags) {
for (const tag of Object.keys(filter.tags)) {
if (filter.tags[tag] !== resource.tags?.[tag]) {
return false;
}
}
}

return true;
})
const filterArray = Array.isArray(this.filter) ? this.filter : [this.filter];
return filterArray.some((filter) => filter.matches(resource));
}
}
2 changes: 1 addition & 1 deletion src/api/v2/quickPickWizard/RecursiveQuickPickStep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { QuickPickWizardContext } from './QuickPickWizardContext';

export class RecursiveQuickPickStep<TModel extends ResourceModelBase> extends GenericQuickPickStep<TModel> {
public async getSubWizard(wizardContext: QuickPickWizardContext<TModel>): Promise<IWizardOptions<QuickPickWizardContext<TModel>> | undefined> {
if (this.matchesContextValue(wizardContext.currentNode)) {
if (this.contextValueFilter.matches(wizardContext.currentNode as TModel)) {
return undefined;
} else {
return {
Expand Down
4 changes: 4 additions & 0 deletions src/api/v2/v2AzureResourcesApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,7 @@ export interface AzureResourcesApiManager {
*/
getApi<T extends AzureResourcesApiBase>(versionRange: string): T | undefined
}

export interface Filter<T> {
matches(value: T): boolean;
}