helpHandler.ts 5.4 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*---------------------------------------------------------------------------------------------
 *  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 {Builder, $} from 'vs/base/browser/builder';
import types = require('vs/base/common/types');
import {Registry} from 'vs/platform/platform';
import {Mode, IContext, IAutoFocus} from 'vs/base/parts/quickopen/browser/quickOpen';
import {QuickOpenEntry, QuickOpenEntryItem, QuickOpenModel} from 'vs/base/parts/quickopen/browser/quickOpenModel';
import {ITree, IElementCallback} from 'vs/base/parts/tree/common/tree';
import {QuickOpenHandlerDescriptor, IQuickOpenRegistry, Extensions, QuickOpenHandler} from 'vs/workbench/browser/quickopen';
import {IQuickOpenService} from 'vs/workbench/services/quickopen/browser/quickOpenService';

18
export const HELP_PREFIX = '?';
E
Erich Gamma 已提交
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 116 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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184

class HelpEntry extends QuickOpenEntryItem {
	private prefix: string;
	private description: string;
	private groupLabel: string;
	private useBorder: boolean;
	private quickOpenService: IQuickOpenService;
	private openOnPreview: boolean;

	constructor(prefix: string, description: string, quickOpenService: IQuickOpenService, openOnPreview: boolean) {
		super();

		this.prefix = prefix;
		this.description = description;
		this.quickOpenService = quickOpenService;
		this.openOnPreview = openOnPreview;
	}

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

	public getDescription(): string {
		return this.description;
	}

	public getHeight(): number {
		return 24;
	}

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

	public setGroupLabel(groupLabel: string): void {
		this.groupLabel = groupLabel;
	}

	public showBorder(): boolean {
		return this.useBorder;
	}

	public setShowBorder(showBorder: boolean): void {
		this.useBorder = showBorder;
	}

	public render(tree: ITree, container: HTMLElement, previousCleanupFn: IElementCallback): IElementCallback {
		let builder = $(container);
		builder.addClass('quick-open-entry');

		// Support border
		if (this.showBorder()) {
			$(container).addClass('results-group-separator');
		} else {
			$(container).removeClass('results-group-separator');
		}

		// Add a container for the group
		if (this.getGroupLabel()) {
			$(container).div((div: Builder) => {
				div.addClass('results-group');
				div.attr({
					text: this.getGroupLabel()
				});
			});
		}

		// Prefix
		let label = builder.clone().div({
			text: this.prefix,
			'class': 'quick-open-help-entry-label'
		});

		if (!this.prefix) {
			label.text('\u2026');
		}

		// Description
		builder.span({
			text: this.description,
			'class': 'quick-open-entry-description'
		});

		return null;
	}

	public run(mode: Mode, context: IContext): boolean {
		if (mode === Mode.OPEN || this.openOnPreview) {
			this.quickOpenService.show(this.prefix);
		}

		return false;
	}
}

export class HelpHandler extends QuickOpenHandler {

	constructor( @IQuickOpenService private quickOpenService: IQuickOpenService) {
		super();
	}

	public getResults(searchValue: string): TPromise<QuickOpenModel> {
		searchValue = searchValue.trim();

		let registry = (<IQuickOpenRegistry>Registry.as(Extensions.Quickopen));
		let handlerDescriptors = registry.getQuickOpenHandlers();

		let defaultHandlers = registry.getDefaultQuickOpenHandlers();
		if (defaultHandlers.length > 0) {
			handlerDescriptors.push(...defaultHandlers);
		}

		let workbenchScoped: HelpEntry[] = [];
		let editorScoped: HelpEntry[] = [];
		let entry: HelpEntry;
		for (let i = 0; i < handlerDescriptors.length; i++) {
			let handlerDescriptor = handlerDescriptors[i];
			if (handlerDescriptor.prefix !== HELP_PREFIX) {

				// Descriptor has multiple help entries
				if (types.isArray(handlerDescriptor.helpEntries)) {
					for (let j = 0; j < handlerDescriptor.helpEntries.length; j++) {
						let helpEntry = handlerDescriptor.helpEntries[j];

						if (helpEntry.prefix.indexOf(searchValue) === 0) {
							entry = new HelpEntry(helpEntry.prefix, helpEntry.description, this.quickOpenService, searchValue.length > 0);
							if (helpEntry.needsEditor) {
								editorScoped.push(entry);
							} else {
								workbenchScoped.push(entry);
							}
						}
					}
				}

				// Single Help entry for descriptor
				else if (handlerDescriptor.prefix.indexOf(searchValue) === 0) {
					entry = new HelpEntry(handlerDescriptor.prefix, handlerDescriptor.description, this.quickOpenService, searchValue.length > 0);
					workbenchScoped.push(entry);
				}
			}
		}

		// Add separator for workbench scoped handlers
		if (workbenchScoped.length > 0) {
			workbenchScoped[0].setGroupLabel(nls.localize('globalCommands', "global commands"));
		}

		// Add separator for editor scoped handlers
		if (editorScoped.length > 0) {
			editorScoped[0].setGroupLabel(nls.localize('editorCommands', "editor commands"));
			if (workbenchScoped.length > 0) {
				editorScoped[0].setShowBorder(true);
			}
		}

		return TPromise.as(new QuickOpenModel([...workbenchScoped, ...editorScoped]));
	}

	public getAutoFocus(searchValue: string): IAutoFocus {
		searchValue = searchValue.trim();
		return {
			autoFocusFirstEntry: searchValue.length > 0,
			autoFocusPrefixMatch: searchValue
		};
	}
185
}