suggest.ts 3.0 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'use strict';

7
import {sequence} from 'vs/base/common/async';
J
Johannes Rieken 已提交
8
import {IModel, IPosition} from 'vs/editor/common/editorCommon';
9 10
import {TPromise} from 'vs/base/common/winjs.base';
import {onUnexpectedError, illegalArgument} from 'vs/base/common/errors';
11
import {ISuggestSupport, ISuggestResult} from 'vs/editor/common/modes';
E
Erich Gamma 已提交
12
import LanguageFeatureRegistry from 'vs/editor/common/modes/languageFeatureRegistry';
13
import {CommonEditorRegistry} from 'vs/editor/common/editorCommonExtensions';
E
Erich Gamma 已提交
14 15 16 17 18 19

export var CONTEXT_SUGGEST_WIDGET_VISIBLE = 'suggestWidgetVisible';
export var CONTEXT_SUGGESTION_SUPPORTS_ACCEPT_ON_KEY = 'suggestionSupportsAcceptOnKey';
export var ACCEPT_SELECTED_SUGGESTION_CMD = 'acceptSelectedSuggestion';

export var SuggestRegistry = new LanguageFeatureRegistry<ISuggestSupport>('suggestSupport');
20

21
export interface ISuggestResult2 extends ISuggestResult {
22 23 24
	support?: ISuggestSupport;
}

25
export function suggest(model: IModel, position: IPosition, triggerCharacter: string, groups?: ISuggestSupport[][]): TPromise<ISuggestResult2[][]> {
26 27 28 29 30 31

	if (!groups) {
		groups = SuggestRegistry.orderedGroups(model);
	}

	const resource = model.getAssociatedResource();
32
	const suggestions: ISuggestResult[][] = [];
33 34 35 36 37 38 39 40 41 42 43

	const factory = groups.map((supports, index) => {
		return () => {

			// stop as soon as a group produced a result
			if (suggestions.length > 0) {
				return;
			}

			// for each support in the group ask for suggestions
			let promises = supports.map(support => {
44
				return support.suggest(resource, position, triggerCharacter).then(values => {
45

46
					let result: ISuggestResult2[] = [];
47
					for (let suggestResult of values) {
48

49 50 51
						if (!suggestResult
							|| !Array.isArray(suggestResult.suggestions)
							|| suggestResult.suggestions.length === 0) {
52 53 54
							continue;
						}

55
						const suggestions2: ISuggestResult2 = {
56
							support,
57 58 59 60
							currentWord: suggestResult.currentWord,
							incomplete: suggestResult.incomplete,
							suggestions: suggestResult.suggestions
						}
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
						result.push(suggestions2);
					}

					return result;

				}, onUnexpectedError);
			});

			return TPromise.join(promises).then(values => {
				for (let value of values) {
					if (Array.isArray(value) && value.length > 0) {
						suggestions.push(value);
					}
				}
			});
		};
	});

	return sequence(factory).then(() => suggestions);
}

CommonEditorRegistry.registerDefaultLanguageCommand('_executeCompletionItemProvider', (model, position, args) => {

	let triggerCharacter = args['triggerCharacter'];
	if (typeof triggerCharacter !== 'undefined' && typeof triggerCharacter !== 'string') {
		throw illegalArgument('triggerCharacter');
	}

	return suggest(model, position, triggerCharacter);
J
Johannes Rieken 已提交
90
});