quickCommand.ts 4.5 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8 9 10 11
/*---------------------------------------------------------------------------------------------
 *  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 {TPromise} from 'vs/base/common/winjs.base';
import nls = require('vs/nls');
import Errors = require('vs/base/common/errors');
import EditorCommon = require('vs/editor/common/editorCommon');
import QuickOpenModel = require('vs/base/parts/quickopen/browser/quickOpenModel');
12
import QuickOpen = require('vs/base/parts/quickopen/common/quickOpen');
E
Erich Gamma 已提交
13 14 15 16 17 18 19 20
import Strings = require('vs/base/common/strings');
import Actions = require('vs/base/common/actions');
import Filters = require('vs/base/common/filters');
import {EditorAction, Behaviour} from 'vs/editor/common/editorAction';
import EditorQuickOpen = require('./editorQuickOpen');
import {IKeybindingService} from 'vs/platform/keybinding/common/keybindingService';

export class EditorActionCommandEntry extends QuickOpenModel.QuickOpenEntryGroup {
21 22 23
	private key: string;
	private action: Actions.IAction;
	private editor: EditorCommon.IEditor;
E
Erich Gamma 已提交
24

25
	constructor(key: string, highlights: QuickOpenModel.IHighlight[], action: Actions.IAction, editor: EditorCommon.IEditor) {
E
Erich Gamma 已提交
26 27 28 29 30 31 32 33
		super();

		this.key = key;
		this.setHighlights(highlights);
		this.action = action;
		this.editor = editor;
	}

34
	public getLabel(): string {
E
Erich Gamma 已提交
35 36 37
		return this.action.label;
	}

38 39 40 41 42
	public getAriaLabel(): string {
		return nls.localize('ariaLabelEntry', "{0}, commands", this.getLabel());
	}

	public getGroupLabel(): string {
E
Erich Gamma 已提交
43 44 45
		return this.key;
	}

46
	public run(mode: QuickOpen.Mode, context: QuickOpenModel.IContext): boolean {
E
Erich Gamma 已提交
47 48 49
		if (mode === QuickOpen.Mode.OPEN) {

			// Use a timeout to give the quick open widget a chance to close itself first
50
			TPromise.timeout(50).done(() => {
E
Erich Gamma 已提交
51 52 53 54 55 56

				// Some actions are enabled only when editor has focus
				this.editor.focus();

				if (this.action.enabled) {
					try {
57
						let promise = this.action.run() || TPromise.as(null);
E
Erich Gamma 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
						promise.done(null, Errors.onUnexpectedError);
					} catch (error) {
						Errors.onUnexpectedError(error);
					}
				}
			}, Errors.onUnexpectedError);

			return true;
		}

		return false;
	}
}

export class QuickCommandAction extends EditorQuickOpen.BaseEditorQuickOpenAction {

	public static ID = 'editor.action.quickCommand';

	private _keybindingService: IKeybindingService;

78
	constructor(descriptor: EditorCommon.IEditorActionDescriptorData, editor: EditorCommon.ICommonCodeEditor, @IKeybindingService keybindingService: IKeybindingService) {
E
Erich Gamma 已提交
79 80 81 82
		super(descriptor, editor, nls.localize('QuickCommandAction.label', "Command Palette"), Behaviour.WidgetFocus | Behaviour.ShowInContextMenu);
		this._keybindingService = keybindingService;
	}

83 84
	_getModel(value: string): QuickOpenModel.QuickOpenModel {
		return new QuickOpenModel.QuickOpenModel(this._editorActionsToEntries(this.editor.getActions(), value));
E
Erich Gamma 已提交
85 86 87 88 89 90
	}

	public getGroupId(): string {
		return '4_tools/1_commands';
	}

91 92 93
	_sort(elementA: QuickOpenModel.QuickOpenEntryGroup, elementB: QuickOpenModel.QuickOpenEntryGroup): number {
		let elementAName = elementA.getLabel().toLowerCase();
		let elementBName = elementB.getLabel().toLowerCase();
E
Erich Gamma 已提交
94 95 96 97

		return Strings.localeCompare(elementAName, elementBName);
	}

98 99
	_editorActionsToEntries(actions: Actions.IAction[], searchValue: string): EditorActionCommandEntry[] {
		let entries: EditorActionCommandEntry[] = [];
E
Erich Gamma 已提交
100

101 102
		for (let i = 0; i < actions.length; i++) {
			let action = actions[i];
E
Erich Gamma 已提交
103

104
			let editorAction = <EditorAction>action;
E
Erich Gamma 已提交
105 106 107 108 109

			if (!editorAction.isSupported()) {
				continue; // do not show actions that are not supported in this context
			}

110
			let keys = this._keybindingService.lookupKeybindings(editorAction.id).map(k => this._keybindingService.getLabelFor(k));
E
Erich Gamma 已提交
111 112

			if (action.label) {
113
				let highlights = Filters.matchesFuzzy(searchValue, action.label);
E
Erich Gamma 已提交
114 115 116 117 118 119 120 121 122 123 124 125
				if (highlights) {
					entries.push(new EditorActionCommandEntry(keys.length > 0 ? keys.join(', ') : '', highlights, action, this.editor));
				}
			}
		}

		// Sort by name
		entries = entries.sort(this._sort);

		return entries;
	}

126
	_getAutoFocus(searchValue: string): QuickOpen.IAutoFocus {
E
Erich Gamma 已提交
127 128 129 130 131 132 133 134 135 136
		return {
			autoFocusFirstEntry: true,
			autoFocusPrefixMatch: searchValue
		};
	}

	_getInputAriaLabel(): string {
		return nls.localize('quickCommandActionInput', "Type the name of an action you want to execute");
	}
}