quickCommand.ts 4.7 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
/*---------------------------------------------------------------------------------------------
 *  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 QuickOpenWidget = require('vs/base/parts/quickopen/browser/quickOpenWidget');
import QuickOpenModel = require('vs/base/parts/quickopen/browser/quickOpenModel');
import QuickOpen = require('vs/base/parts/quickopen/browser/quickOpen');
import Strings = require('vs/base/common/strings');
import Actions = require('vs/base/common/actions');
import Filters = require('vs/base/common/filters');
import {CommonEditorRegistry} from 'vs/editor/common/editorCommonExtensions';
import {EditorAction, Behaviour} from 'vs/editor/common/editorAction';
import EditorQuickOpen = require('./editorQuickOpen');
import {KeybindingsUtils} from 'vs/platform/keybinding/common/keybindingsUtils';
import {IKeybindingService} from 'vs/platform/keybinding/common/keybindingService';

export class EditorActionCommandEntry extends QuickOpenModel.QuickOpenEntryGroup {
	private key:string;
	private action:Actions.IAction;
	private editor:EditorCommon.IEditor;

	constructor(key:string, highlights:QuickOpenModel.IHighlight[], action:Actions.IAction, editor:EditorCommon.IEditor) {
		super();

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

	public getLabel():string {
		return this.action.label;
	}

	public getGroupLabel():string {
		return this.key;
	}

	public run(mode:QuickOpen.Mode, context:QuickOpenModel.IContext):boolean {
		if (mode === QuickOpen.Mode.OPEN) {

			// Use a timeout to give the quick open widget a chance to close itself first
			TPromise.timeout(50).done(()=>{

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

				if (this.action.enabled) {
					try {
						var promise = this.action.run() || TPromise.as(null);
						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;

	constructor(descriptor:EditorCommon.IEditorActionDescriptorData, editor:EditorCommon.ICommonCodeEditor, @IKeybindingService keybindingService: IKeybindingService) {
		super(descriptor, editor, nls.localize('QuickCommandAction.label', "Command Palette"), Behaviour.WidgetFocus | Behaviour.ShowInContextMenu);
		this._keybindingService = keybindingService;
	}

	_getModel(value:string):QuickOpenModel.QuickOpenModel {
		var model = new QuickOpenModel.QuickOpenModel();

		var editorActions = this.editor.getActions();
		var entries = this._editorActionsToEntries(editorActions, value);

		model.addEntries(entries);

		return model;
	}

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

	_sort(elementA:QuickOpenModel.QuickOpenEntryGroup, elementB:QuickOpenModel.QuickOpenEntryGroup):number {
		var elementAName = elementA.getLabel().toLowerCase();
		var elementBName = elementB.getLabel().toLowerCase();

		return Strings.localeCompare(elementAName, elementBName);
	}

	_editorActionsToEntries(actions:Actions.IAction[], searchValue:string):EditorActionCommandEntry[] {
		var entries:EditorActionCommandEntry[] = [];

		for (var i = 0; i < actions.length; i++) {
			var action = actions[i];

			var editorAction = <EditorAction>action;

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

116
			var keys = this._keybindingService.lookupKeybindings(editorAction.id).map(k => this._keybindingService.getLabelFor(k));
E
Erich Gamma 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142

			if (action.label) {
				var highlights = Filters.matchesFuzzy(searchValue, action.label);
				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;
	}

	_getAutoFocus(searchValue:string):QuickOpen.IAutoFocus {
		return {
			autoFocusFirstEntry: true,
			autoFocusPrefixMatch: searchValue
		};
	}

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