From ceb7a31391619623bd1fb9183ab3665fa2c9d1ca Mon Sep 17 00:00:00 2001 From: Liang Huang Date: Sun, 22 Mar 2020 11:38:34 -0400 Subject: [PATCH] align how tasks are displayed as quick open items - In a multi root workspace, detected tasks are displayed in different ways across `Run task`, `Restart running task`, `Terminate task`, and `Show running tasks`. This change aligns the display of the task quick open items. - fixed #6821 Signed-off-by: Liang Huang --- CHANGELOG.md | 4 +- packages/task/src/browser/quick-open-task.ts | 174 ++++++++++++------- 2 files changed, 113 insertions(+), 65 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a121be14d657..a24fd577b1988 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,9 +7,11 @@ Breaking changes: -- [scm][git] the History view (GitHistoryWidget) has moved from the git package to a new package, scm-extra, and +- [scm][git] the History view (GitHistoryWidget) has moved from the git package to a new package, scm-extra, and renamed to ScmHistoryWidget. GitNavigableListWidget has also moved. CSS classes have been moved renamed accordingly. [6381](https://github.com/eclipse-theia/theia/pull/6381) +- [task] `TaskRestartRunningQuickOpenItem` class is renamed into `RunningTaskQuickOpenItem`. [7392](https://github.com/eclipse-theia/theia/pull/7392) +- [task] `TaskAttachQuickOpenItem` class is removed. ## v0.16.0 diff --git a/packages/task/src/browser/quick-open-task.ts b/packages/task/src/browser/quick-open-task.ts index 2e472fb5899a1..dd38c4e28ed51 100644 --- a/packages/task/src/browser/quick-open-task.ts +++ b/packages/task/src/browser/quick-open-task.ts @@ -117,6 +117,9 @@ export class QuickOpenTask implements QuickOpenModel, QuickOpenHandler { @inject(PreferenceService) protected readonly preferences: PreferenceService; + @inject(LabelProvider) + protected readonly labelProvider: LabelProvider; + /** Initialize this quick open model with the tasks. */ async init(): Promise { const recentTasks = this.taskService.recentTasks; @@ -196,25 +199,37 @@ export class QuickOpenTask implements QuickOpenModel, QuickOpenHandler { attach(): void { this.items = []; this.actionProvider = undefined; - + const isMulti: boolean = this.workspaceService.isMultiRootWorkspaceOpened; this.taskService.getRunningTasks().then(tasks => { if (!tasks.length) { this.items.push(new QuickOpenItem({ label: 'No tasks found', run: (_mode: QuickOpenMode): boolean => false })); - } - for (const task of tasks) { - // can only attach to terminal processes, so only list those - if (task.terminalId) { - this.items.push( - new TaskAttachQuickOpenItem( + } else { + tasks.forEach((task: TaskInfo) => { + // can only attach to terminal processes, so only list those + if (task.terminalId) { + this.items.push(new RunningTaskQuickOpenItem( task, - this.getRunningTaskLabel(task), - this.taskService - ) - ); - } + this.taskService, + this.taskNameResolver, + this.taskSourceResolver, + this.taskDefinitionRegistry, + this.labelProvider, + isMulti, + { + run: (mode: QuickOpenMode): boolean => { + if (mode !== QuickOpenMode.OPEN) { + return false; + } + this.taskService.attach(task.terminalId!, task.taskId); + return true; + } + }, + )); + } + }); } this.quickOpenService.open(this, { placeholder: 'Choose task to open', @@ -543,30 +558,6 @@ export class ConfigureBuildOrTestTaskQuickOpenItem extends TaskRunQuickOpenItem } } -export class TaskAttachQuickOpenItem extends QuickOpenItem { - - constructor( - protected readonly task: TaskInfo, - protected readonly taskLabel: string, - protected taskService: TaskService - ) { - super(); - } - - getLabel(): string { - return this.taskLabel!; - } - - run(mode: QuickOpenMode): boolean { - if (mode !== QuickOpenMode.OPEN) { - return false; - } - if (this.task.terminalId) { - this.taskService.attach(this.task.terminalId, this.task.taskId); - } - return true; - } -} export class TaskConfigureQuickOpenItem extends QuickOpenGroupItem { protected taskDefinitionRegistry: TaskDefinitionRegistry; @@ -619,15 +610,31 @@ export class TaskConfigureQuickOpenItem extends QuickOpenGroupItem { @injectable() export class TaskTerminateQuickOpen implements QuickOpenModel { + @inject(LabelProvider) + protected readonly labelProvider: LabelProvider; + @inject(QuickOpenService) protected readonly quickOpenService: QuickOpenService; + @inject(TaskDefinitionRegistry) + protected readonly taskDefinitionRegistry: TaskDefinitionRegistry; + + @inject(TaskNameResolver) + protected readonly taskNameResolver: TaskNameResolver; + + @inject(TaskSourceResolver) + protected readonly taskSourceResolver: TaskSourceResolver; + @inject(TaskService) protected readonly taskService: TaskService; + @inject(WorkspaceService) + protected readonly workspaceService: WorkspaceService; + async onType(_lookFor: string, acceptor: (items: QuickOpenItem[]) => void): Promise { const items: QuickOpenItem[] = []; const runningTasks: TaskInfo[] = await this.taskService.getRunningTasks(); + const isMulti: boolean = this.workspaceService.isMultiRootWorkspaceOpened; if (runningTasks.length <= 0) { items.push(new QuickOpenItem({ label: 'No task is currently running', @@ -635,16 +642,24 @@ export class TaskTerminateQuickOpen implements QuickOpenModel { })); } else { runningTasks.forEach((task: TaskInfo) => { - items.push(new QuickOpenItem({ - label: task.config.label, - run: (mode: QuickOpenMode): boolean => { - if (mode !== QuickOpenMode.OPEN) { - return false; + items.push(new RunningTaskQuickOpenItem( + task, + this.taskService, + this.taskNameResolver, + this.taskSourceResolver, + this.taskDefinitionRegistry, + this.labelProvider, + isMulti, + { + run: (mode: QuickOpenMode): boolean => { + if (mode !== QuickOpenMode.OPEN) { + return false; + } + this.taskService.kill(task.taskId); + return true; } - this.taskService.kill(task.taskId); - return true; - } - })); + }, + )); }); if (runningTasks.length > 1) { items.push(new QuickOpenItem({ @@ -677,18 +692,34 @@ export class TaskTerminateQuickOpen implements QuickOpenModel { @injectable() export class TaskRunningQuickOpen implements QuickOpenModel { + @inject(LabelProvider) + protected readonly labelProvider: LabelProvider; + @inject(QuickOpenService) protected readonly quickOpenService: QuickOpenService; + @inject(TaskDefinitionRegistry) + protected readonly taskDefinitionRegistry: TaskDefinitionRegistry; + + @inject(TaskNameResolver) + protected readonly taskNameResolver: TaskNameResolver; + + @inject(TaskSourceResolver) + protected readonly taskSourceResolver: TaskSourceResolver; + @inject(TaskService) protected readonly taskService: TaskService; + @inject(WorkspaceService) + protected readonly workspaceService: WorkspaceService; + @inject(TerminalService) protected readonly terminalService: TerminalService; async onType(_lookFor: string, acceptor: (items: QuickOpenItem[]) => void): Promise { const items: QuickOpenItem[] = []; const runningTasks: TaskInfo[] = await this.taskService.getRunningTasks(); + const isMulti: boolean = this.workspaceService.isMultiRootWorkspaceOpened; if (runningTasks.length <= 0) { items.push(new QuickOpenItem({ label: 'No task is currently running', @@ -696,21 +727,29 @@ export class TaskRunningQuickOpen implements QuickOpenModel { })); } else { runningTasks.forEach((task: TaskInfo) => { - items.push(new QuickOpenItem({ - label: task.config.label, - run: (mode: QuickOpenMode): boolean => { - if (mode !== QuickOpenMode.OPEN) { - return false; - } - if (task.terminalId) { - const terminal = this.terminalService.getById('terminal-' + task.terminalId); - if (terminal) { - this.terminalService.open(terminal); + items.push(new RunningTaskQuickOpenItem( + task, + this.taskService, + this.taskNameResolver, + this.taskSourceResolver, + this.taskDefinitionRegistry, + this.labelProvider, + isMulti, + { + run: (mode: QuickOpenMode): boolean => { + if (mode !== QuickOpenMode.OPEN) { + return false; + } + if (task.terminalId) { + const terminal = this.terminalService.getById('terminal-' + task.terminalId); + if (terminal) { + this.terminalService.open(terminal); + } } + return true; } - return true; - } - })); + }, + )); }); } acceptor(items); @@ -725,7 +764,7 @@ export class TaskRunningQuickOpen implements QuickOpenModel { } } -export class TaskRestartRunningQuickOpenItem extends QuickOpenItem { +export class RunningTaskQuickOpenItem extends QuickOpenItem { constructor( protected readonly taskInfo: TaskInfo, @@ -753,11 +792,10 @@ export class TaskRestartRunningQuickOpenItem extends QuickOpenItem { } run(mode: QuickOpenMode): boolean { - if (mode !== QuickOpenMode.OPEN) { + if (mode !== QuickOpenMode.OPEN || !this.options.run) { return false; } - this.taskService.restartTask(this.taskInfo); - return true; + return this.options.run(mode); } } @@ -796,7 +834,7 @@ export class TaskRestartRunningQuickOpen implements QuickOpenModel { })); } else { runningTasks.forEach((task: TaskInfo) => { - items.push(new TaskRestartRunningQuickOpenItem( + items.push(new RunningTaskQuickOpenItem( task, this.taskService, this.taskNameResolver, @@ -804,7 +842,15 @@ export class TaskRestartRunningQuickOpen implements QuickOpenModel { this.taskDefinitionRegistry, this.labelProvider, isMulti, - {}, + { + run: (mode: QuickOpenMode): boolean => { + if (mode !== QuickOpenMode.OPEN) { + return false; + } + this.taskService.restartTask(task); + return true; + } + }, )); }); }