quickOpen.ts 5.5 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'use strict';

import nls = require('vs/nls');
import Filters = require('vs/base/common/filters');
import { TPromise } from 'vs/base/common/winjs.base';
D
Dirk Baeumer 已提交
10
import { Action, IAction } from 'vs/base/common/actions';
11 12 13 14 15
import Quickopen = require('vs/workbench/browser/quickopen');
import QuickOpen = require('vs/base/parts/quickopen/common/quickOpen');
import Model = require('vs/base/parts/quickopen/browser/quickOpenModel');
import { IQuickOpenService } from 'vs/platform/quickOpen/common/quickOpen';

16
import { Task, TaskSourceKind } from 'vs/workbench/parts/tasks/common/tasks';
17
import { ITaskService } from 'vs/workbench/parts/tasks/common/taskService';
18
import { ActionBarContributor, ContributableActionProvider } from 'vs/workbench/browser/actions';
19 20 21

export class TaskEntry extends Model.QuickOpenEntry {

D
Dirk Baeumer 已提交
22
	constructor(protected taskService: ITaskService, protected _task: Task, highlights: Model.IHighlight[] = []) {
23 24 25 26
		super(highlights);
	}

	public getLabel(): string {
27
		return this.task._label;
28 29 30 31 32
	}

	public getAriaLabel(): string {
		return nls.localize('entryAriaLabel', "{0}, tasks", this.getLabel());
	}
D
Dirk Baeumer 已提交
33 34 35 36

	public get task(): Task {
		return this._task;
	}
37 38
}

39 40 41 42 43 44
export class TaskGroupEntry extends Model.QuickOpenEntryGroup {
	constructor(entry: TaskEntry, groupLabel: string, withBorder: boolean) {
		super(entry, groupLabel, withBorder);
	}
}

45 46
export abstract class QuickOpenHandler extends Quickopen.QuickOpenHandler {

47 48 49
	private tasks: TPromise<Task[]>;


50
	constructor(
51 52
		protected quickOpenService: IQuickOpenService,
		protected taskService: ITaskService,
53 54 55 56 57 58 59
	) {
		super();

		this.quickOpenService = quickOpenService;
		this.taskService = taskService;
	}

60 61 62 63 64 65 66 67
	public onOpen(): void {
		this.tasks = this.getTasks();
	}

	public onClose(canceled: boolean): void {
		this.tasks = undefined;
	}

68
	public getResults(input: string): TPromise<Model.QuickOpenModel> {
69 70 71 72 73 74 75 76 77
		return this.tasks.then((tasks) => {
			let entries: Model.QuickOpenEntry[] = [];
			if (tasks.length === 0) {
				return new Model.QuickOpenModel(entries);
			}
			tasks = tasks.sort((a, b) => {
				let aKind = a._source.kind;
				let bKind = b._source.kind;
				if (aKind === bKind) {
78 79 80 81 82 83
					if (aKind === TaskSourceKind.Extension) {
						let compare = a._source.label.localeCompare(b._source.label);
						if (compare !== 0) {
							return compare;
						}
					}
84
					return a._label.localeCompare(b._label);
85 86 87 88 89 90 91
				}
				if (aKind === TaskSourceKind.Workspace) {
					return -1;
				} else {
					return +1;
				}
			});
D
Dirk Baeumer 已提交
92 93 94
			let firstWorkspace: boolean = true;
			let firstExtension: boolean = true;
			let hadWorkspace: boolean = false;
95
			for (let task of tasks) {
96
				let highlights = Filters.matchesContiguousSubString(input, task._label);
97 98 99
				if (!highlights) {
					continue;
				}
D
Dirk Baeumer 已提交
100 101
				if (task._source.kind === TaskSourceKind.Workspace && firstWorkspace) {
					firstWorkspace = false;
102
					hadWorkspace = true;
D
Dirk Baeumer 已提交
103
					entries.push(new TaskGroupEntry(this.createEntry(this.taskService, task, highlights), nls.localize('configured', 'Configured Tasks'), false));
D
Dirk Baeumer 已提交
104 105
				} else if (task._source.kind === TaskSourceKind.Extension && firstExtension) {
					firstExtension = false;
D
Dirk Baeumer 已提交
106
					entries.push(new TaskGroupEntry(this.createEntry(this.taskService, task, highlights), nls.localize('detected', 'Detected Tasks'), hadWorkspace));
107 108 109 110
				} else {
					entries.push(this.createEntry(this.taskService, task, highlights));
				}
			}
111
			return new Model.QuickOpenModel(entries, new ContributableActionProvider());
112
		});
113 114 115 116 117 118 119 120 121 122 123
	}

	protected abstract getTasks(): TPromise<Task[]>;

	protected abstract createEntry(taskService: ITaskService, task: Task, highlights: Model.IHighlight[]): TaskEntry;

	public getAutoFocus(input: string): QuickOpen.IAutoFocus {
		return {
			autoFocusFirstEntry: !!input
		};
	}
D
Dirk Baeumer 已提交
124 125 126 127 128 129 130
}

class CustomizeTaskAction extends Action {

	private static ID = 'workbench.action.tasks.customizeTask';
	private static LABEL = nls.localize('customizeTask', "Customize Task");

D
Dirk Baeumer 已提交
131
	constructor(private taskService: ITaskService, private quickOpenService: IQuickOpenService, private task: Task) {
D
Dirk Baeumer 已提交
132 133 134 135 136
		super(CustomizeTaskAction.ID, CustomizeTaskAction.LABEL);
		this.updateClass();
	}

	public updateClass(): void {
137
		this.class = 'quick-open-task-configure';
D
Dirk Baeumer 已提交
138 139
	}

D
Dirk Baeumer 已提交
140 141 142 143
	public run(context: any): TPromise<any> {
		return this.taskService.customize(this.task, true).then(() => {
			this.quickOpenService.close();
		});
D
Dirk Baeumer 已提交
144 145 146 147 148
	}
}

export class QuickOpenActionContributor extends ActionBarContributor {

D
Dirk Baeumer 已提交
149
	constructor( @ITaskService private taskService: ITaskService, @IQuickOpenService private quickOpenService: IQuickOpenService) {
D
Dirk Baeumer 已提交
150 151 152 153
		super();
	}

	public hasActions(context: any): boolean {
D
Dirk Baeumer 已提交
154
		let task = this.getTask(context);
D
Dirk Baeumer 已提交
155

D
Dirk Baeumer 已提交
156
		return !!task;
D
Dirk Baeumer 已提交
157 158 159
	}

	public getActions(context: any): IAction[] {
D
Dirk Baeumer 已提交
160 161 162 163
		let actions: Action[] = [];
		let task = this.getTask(context);
		if (task && task._source.kind === TaskSourceKind.Extension) {
			actions.push(new CustomizeTaskAction(this.taskService, this.quickOpenService, task));
D
Dirk Baeumer 已提交
164 165 166 167
		}
		return actions;
	}

D
Dirk Baeumer 已提交
168 169
	private getTask(context: any): Task {
		if (!context) {
D
Dirk Baeumer 已提交
170 171
			return undefined;
		}
D
Dirk Baeumer 已提交
172 173 174 175 176 177 178
		let element = context.element;
		if (element instanceof TaskEntry) {
			return element.task;
		} else if (element instanceof TaskGroupEntry) {
			return (element.getEntry() as TaskEntry).task;
		}
		return undefined;
D
Dirk Baeumer 已提交
179
	}
180
}