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

Support 'onTaskType' activation event #12431

Merged
merged 3 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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: 2 additions & 1 deletion packages/core/src/common/quick-pick-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export interface QuickPickSeparator {
}

export type QuickPickItemOrSeparator = QuickPickItem | QuickPickSeparator;
export type QuickPickInput<T = QuickPickItem> = T | QuickPickSeparator;

export namespace QuickPickItem {
export function is(item: QuickPickSeparator | QuickPickItem): item is QuickPickItem {
Expand Down Expand Up @@ -278,7 +279,7 @@ export interface QuickInputService {
open(filter: string): void;
createInputBox(): InputBox;
input(options?: InputOptions, token?: CancellationToken): Promise<string | undefined>;
pick<T extends QuickPickItem, O extends PickOptions<T>>(picks: Promise<T[]> | T[], options?: O, token?: CancellationToken):
pick<T extends QuickPickItem, O extends PickOptions<T>>(picks: Promise<QuickPickInput<T>[]> | QuickPickInput<T>[], options?: O, token?: CancellationToken):
Promise<(O extends { canPickMany: true } ? T[] : T) | undefined>;
showQuickPick<T extends QuickPickItem>(items: Array<T | QuickPickSeparator>, options?: QuickPickOptions<T>): Promise<T | undefined>;
hide(): void;
Expand Down
14 changes: 9 additions & 5 deletions packages/monaco/src/browser/monaco-quick-input-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,15 @@ export class MonacoQuickInputService implements QuickInputService {
): Promise<(O extends { canPickMany: true; } ? T[] : T) | undefined> {
type M = T & { buttons?: NormalizedQuickInputButton[] };
type R = (O extends { canPickMany: true; } ? T[] : T);
const monacoPicks = (await picks).map(pick => {
if (pick.type !== 'separator') {
pick.buttons &&= pick.buttons.map(QuickInputButton.normalize);
}
return pick as M;

const monacoPicks: Promise<QuickPickInput<IQuickPickItem>[]> = new Promise(async resolve => {
const updatedPicks = (await picks).map(pick => {
if (pick.type !== 'separator') {
pick.buttons &&= pick.buttons.map(QuickInputButton.normalize);
}
return pick as M;
});
resolve(updatedPicks);
});
const monacoOptions = options as IPickOptions<M>;
const picked = await this.monacoService.pick(monacoPicks, monacoOptions, token);
Expand Down
31 changes: 26 additions & 5 deletions packages/plugin-ext/src/hosted/browser/hosted-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ import { WaitUntilEvent } from '@theia/core/lib/common/event';
import { FileSearchService } from '@theia/file-search/lib/common/file-search-service';
import { FrontendApplicationStateService } from '@theia/core/lib/browser/frontend-application-state';
import { PluginViewRegistry } from '../../main/browser/view/plugin-view-registry';
import { TaskProviderRegistry, TaskResolverRegistry } from '@theia/task/lib/browser/task-contribution';
import { WillResolveTaskProvider, TaskProviderRegistry, TaskResolverRegistry } from '@theia/task/lib/browser/task-contribution';
import { TaskDefinitionRegistry } from '@theia/task/lib/browser/task-definition-registry';
import { WebviewEnvironment } from '../../main/browser/webview/webview-environment';
import { WebviewWidget } from '../../main/browser/webview/webview';
import { WidgetManager } from '@theia/core/lib/browser/widget-manager';
Expand All @@ -73,6 +74,7 @@ export type PluginHost = 'frontend' | string;
export type DebugActivationEvent = 'onDebugResolve' | 'onDebugInitialConfigurations' | 'onDebugAdapterProtocolTracker' | 'onDebugDynamicConfigurations';

export const PluginProgressLocation = 'plugin';
export const ALL_ACTIVATION_EVENT = '*';

@injectable()
export class HostedPluginSupport {
Expand Down Expand Up @@ -139,6 +141,9 @@ export class HostedPluginSupport {
@inject(TaskResolverRegistry)
protected readonly taskResolverRegistry: TaskResolverRegistry;

@inject(TaskDefinitionRegistry)
protected readonly taskDefinitionRegistry: TaskDefinitionRegistry;

@inject(ProgressService)
protected readonly progressService: ProgressService;

Expand Down Expand Up @@ -205,7 +210,7 @@ export class HostedPluginSupport {
this.debugSessionManager.onWillResolveDebugConfiguration(event => this.ensureDebugActivation(event, 'onDebugResolve', event.debugType));
this.debugConfigurationManager.onWillProvideDebugConfiguration(event => this.ensureDebugActivation(event, 'onDebugInitialConfigurations'));
// Activate all providers of dynamic configurations, i.e. Let the user pick a configuration from all the available ones.
this.debugConfigurationManager.onWillProvideDynamicDebugConfiguration(event => this.ensureDebugActivation(event, 'onDebugDynamicConfigurations', '*'));
this.debugConfigurationManager.onWillProvideDynamicDebugConfiguration(event => this.ensureDebugActivation(event, 'onDebugDynamicConfigurations', ALL_ACTIVATION_EVENT));
this.viewRegistry.onDidExpandView(id => this.activateByView(id));
this.taskProviderRegistry.onWillProvideTaskProvider(event => this.ensureTaskActivation(event));
this.taskResolverRegistry.onWillProvideTaskResolver(event => this.ensureTaskActivation(event));
Expand Down Expand Up @@ -610,6 +615,10 @@ export class HostedPluginSupport {
await this.activateByEvent(`onCommand:${commandId}`);
}

async activateByTaskType(taskType: string): Promise<void> {
await this.activateByEvent(`onTaskType:${taskType}`);
}

async activateByCustomEditor(viewType: string): Promise<void> {
await this.activateByEvent(`onCustomEditor:${viewType}`);
}
Expand Down Expand Up @@ -648,8 +657,20 @@ export class HostedPluginSupport {
event.waitUntil(p);
}

protected ensureTaskActivation(event: WaitUntilEvent): void {
event.waitUntil(this.activateByCommand('workbench.action.tasks.runTask'));
protected ensureTaskActivation(event: WillResolveTaskProvider): void {
const promises = [this.activateByCommand('workbench.action.tasks.runTask')];
const taskType = event.taskType;
if (taskType) {
if (taskType === ALL_ACTIVATION_EVENT) {
for (const taskDefinition of this.taskDefinitionRegistry.getAll()) {
promises.push(this.activateByTaskType(taskDefinition.taskType));
}
} else {
promises.push(this.activateByTaskType(taskType));
}
}

event.waitUntil(Promise.all(promises));
}

protected ensureDebugActivation(event: WaitUntilEvent, activationEvent?: DebugActivationEvent, debugType?: string): void {
Expand Down Expand Up @@ -678,7 +699,7 @@ export class HostedPluginSupport {
for (const activationEvent of activationEvents) {
if (/^workspaceContains:/.test(activationEvent)) {
const fileNameOrGlob = activationEvent.substr('workspaceContains:'.length);
if (fileNameOrGlob.indexOf('*') >= 0 || fileNameOrGlob.indexOf('?') >= 0) {
if (fileNameOrGlob.indexOf(ALL_ACTIVATION_EVENT) >= 0 || fileNameOrGlob.indexOf('?') >= 0) {
includePatterns.push(fileNameOrGlob);
} else {
paths.push(fileNameOrGlob);
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-ext/src/plugin/plugin-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export class PluginManagerExtImpl implements PluginManagerExt, PluginManager {
'onDebugResolve',
'onDebugAdapterProtocolTracker',
'onDebugDynamicConfigurations',
'onTaskType',
'workspaceContains',
'onView',
'onUri',
Expand Down
8 changes: 5 additions & 3 deletions packages/plugin-ext/src/plugin/type-converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -835,10 +835,12 @@ export function fromTask(task: theia.Task): TaskDto | undefined {
if ('detail' in task) {
taskDto.detail = task.detail;
}
if (typeof task.scope === 'object') {
taskDto.scope = task.scope.uri.toString();
} else if (typeof task.scope === 'number') {
if (typeof task.scope === 'number') {
taskDto.scope = task.scope;
} else if (task.scope !== undefined) {
taskDto.scope = task.scope.uri.toString();
} else {
taskDto.scope = types.TaskScope.Workspace;
}

if (task.presentationOptions) {
Expand Down
89 changes: 66 additions & 23 deletions packages/task/src/browser/provided-task-configurations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
// *****************************************************************************

import { inject, injectable } from '@theia/core/shared/inversify';
import { TaskProviderRegistry } from './task-contribution';
import { TaskProviderRegistry, TaskProvider } from './task-contribution';
import { TaskDefinitionRegistry } from './task-definition-registry';
import { TaskConfiguration, TaskCustomization, TaskOutputPresentation, TaskConfigurationScope, TaskScope } from '../common';

const ALL_TASK_TYPES: string = '*';

@injectable()
export class ProvidedTaskConfigurations {
/**
Expand All @@ -35,48 +37,89 @@ export class ProvidedTaskConfigurations {
protected readonly taskDefinitionRegistry: TaskDefinitionRegistry;

private currentToken: number = 0;
private activatedProvidersTypes: string[] = [];
private nextToken = 1;

startUserAction(): number {
return this.nextToken++;
}

/** returns a list of provided tasks */
async getTasks(token: number): Promise<TaskConfiguration[]> {
await this.refreshTasks(token);
protected updateUserAction(token: number): void {
if (this.currentToken !== token) {
this.currentToken = token;
this.activatedProvidersTypes.length = 0;
}
}

protected pushActivatedProvidersType(taskType: string): void {
if (!this.activatedProvidersTypes.includes(taskType)) {
this.activatedProvidersTypes.push(taskType);
}
}

protected isTaskProviderActivationNeeded(taskType?: string): boolean {
if (!taskType || this.activatedProvidersTypes.includes(taskType!) || this.activatedProvidersTypes.includes(ALL_TASK_TYPES)) {
return false;
}
return true;
}

/**
* Activate providers for the given taskType
* @param taskType A specific task type or '*' to indicate all task providers
*/
protected async activateProviders(taskType?: string): Promise<void> {
if (!!taskType) {
await this.taskProviderRegistry.activateProvider(taskType);
this.pushActivatedProvidersType(taskType);
}
}

/** returns a list of provided tasks matching an optional given type, or all if '*' is used */
async getTasks(token: number, type?: string): Promise<TaskConfiguration[]> {
await this.refreshTasks(token, type);
const tasks: TaskConfiguration[] = [];
for (const taskLabelMap of this.tasksMap!.values()) {
for (const taskScopeMap of taskLabelMap.values()) {
for (const task of taskScopeMap.values()) {
tasks.push(task);
if (!type || task.type === type || type === ALL_TASK_TYPES) {
tasks.push(task);
}
}
}
}
return tasks;
}

protected async refreshTasks(token: number): Promise<void> {
if (token !== this.currentToken) {
this.currentToken = token;
protected async refreshTasks(token: number, taskType?: string): Promise<void> {
const newProviderActivationNeeded = this.isTaskProviderActivationNeeded(taskType);
if (token !== this.currentToken || newProviderActivationNeeded) {
this.updateUserAction(token);
await this.activateProviders(taskType);
const providers = await this.taskProviderRegistry.getProviders();
const providedTasks: TaskConfiguration[] = (await Promise.all(providers.map(p => p.provideTasks())))
.reduce((acc, taskArray) => acc.concat(taskArray), [])
// Global/User tasks from providers are not supported.
.filter(task => task.scope !== TaskScope.Global)
.map(providedTask => {
const originalPresentation = providedTask.presentation || {};
return {
...providedTask,
presentation: {
...TaskOutputPresentation.getDefault(),
...originalPresentation
}
};
});

const providedTasks: TaskConfiguration[] = (await Promise.all(providers.map(p => this.resolveTaskConfigurations(p))))
.reduce((acc, taskArray) => acc.concat(taskArray), []);
this.cacheTasks(providedTasks);
}
}

protected async resolveTaskConfigurations(taskProvider: TaskProvider): Promise<TaskConfiguration[]> {
return (await taskProvider.provideTasks())
// Global/User tasks from providers are not supported.
.filter(task => task.scope !== TaskScope.Global)
.map(providedTask => {
const originalPresentation = providedTask.presentation || {};
return {
...providedTask,
presentation: {
...TaskOutputPresentation.getDefault(),
...originalPresentation
}
};
});
}

/** returns the task configuration for a given source and label or undefined if none */
async getTask(token: number, source: string, taskLabel: string, scope: TaskConfigurationScope): Promise<TaskConfiguration | undefined> {
await this.refreshTasks(token);
Expand All @@ -99,7 +142,7 @@ export class ProvidedTaskConfigurations {

const matchedTasks: TaskConfiguration[] = [];
let highest = -1;
const tasks = await this.getTasks(token);
const tasks = await this.getTasks(token, customization.type);
for (const task of tasks) { // find detected tasks that match the `definition`
const required = definition.properties.required || [];
if (!required.every(requiredProp => customization[requiredProp] !== undefined)) {
Expand Down
Loading