suggest.ts 2.9 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';
8
import {isFalsyOrEmpty} from 'vs/base/common/arrays';
A
Alex Dima 已提交
9
import {illegalArgument, onUnexpectedError} from 'vs/base/common/errors';
10
import {TPromise} from 'vs/base/common/winjs.base';
A
Alex Dima 已提交
11
import {IModel, IPosition} from 'vs/editor/common/editorCommon';
12
import {CommonEditorRegistry} from 'vs/editor/common/editorCommonExtensions';
A
Alex Dima 已提交
13 14
import {ISuggestResult, ISuggestSupport} from 'vs/editor/common/modes';
import LanguageFeatureRegistry from 'vs/editor/common/modes/languageFeatureRegistry';
15
import {SnippetsRegistry} from 'vs/editor/common/modes/supports';
E
Erich Gamma 已提交
16 17 18 19 20 21

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');
22

23
export interface ISuggestResult2 extends ISuggestResult {
24 25 26
	support?: ISuggestSupport;
}

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

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

	const resource = model.getAssociatedResource();
34
	const result: ISuggestResult2[] = [];
35 36 37 38 39

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

			// stop as soon as a group produced a result
40
			if (result.length > 0) {
41 42 43 44
				return;
			}

			// for each support in the group ask for suggestions
45
			return TPromise.join(supports.map(support => {
46
				return support.suggest(resource, position, triggerCharacter).then(values => {
47

J
Johannes Rieken 已提交
48 49 50 51
					if (!values) {
						return;
					}

52
					for (let suggestResult of values) {
53

54
						if (!suggestResult || isFalsyOrEmpty(suggestResult.suggestions)) {
55 56 57
							continue;
						}

58
						result.push({
59
							support,
60 61 62
							currentWord: suggestResult.currentWord,
							incomplete: suggestResult.incomplete,
							suggestions: suggestResult.suggestions
63
						});
64 65 66
					}

				}, onUnexpectedError);
67
			}));
68 69 70
		};
	});

71 72
	return sequence(factory).then(() => {
		// add snippets to the first group
73
		const snippets = SnippetsRegistry.getSnippets(model, position);
74 75
		result.push(snippets);
		return result;
76
	});
77 78 79 80 81 82 83 84 85 86
}

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 已提交
87
});