suggest.ts 2.6 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, asWinJsPromise} from 'vs/base/common/async';
8
import {isFalsyOrEmpty} from 'vs/base/common/arrays';
9
import {onUnexpectedError} from 'vs/base/common/errors';
10
import {TPromise} from 'vs/base/common/winjs.base';
A
Alex Dima 已提交
11
import {IReadOnlyModel} from 'vs/editor/common/editorCommon';
12
import {CommonEditorRegistry} from 'vs/editor/common/editorCommonExtensions';
13
import {ISuggestResult, ISuggestSupport, SuggestRegistry} from 'vs/editor/common/modes';
14
import {SnippetsRegistry} from 'vs/editor/common/modes/supports';
A
Alex Dima 已提交
15
import {Position} from 'vs/editor/common/core/position';
E
Erich Gamma 已提交
16 17 18 19

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

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

A
Alex Dima 已提交
24
export function provideCompletionItems(model: IReadOnlyModel, position: Position, groups?: ISuggestSupport[][]): TPromise<ISuggestResult2[]> {
25 26 27 28 29

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

30
	const result: ISuggestResult2[] = [];
31 32 33 34 35

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

			// stop as soon as a group produced a result
36
			if (result.length > 0) {
37 38 39 40
				return;
			}

			// for each support in the group ask for suggestions
41
			return TPromise.join(supports.map(support => {
42 43 44
				return asWinJsPromise((token) => {
					return support.provideCompletionItems(model, position, token);
				}).then(values => {
45

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

50
					for (let suggestResult of values) {
51

52
						if (!suggestResult || isFalsyOrEmpty(suggestResult.suggestions)) {
53 54 55
							continue;
						}

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

				}, onUnexpectedError);
65
			}));
66 67 68
		};
	});

69 70
	return sequence(factory).then(() => {
		// add snippets to the first group
71
		const snippets = SnippetsRegistry.getSnippets(model, position);
72 73
		result.push(snippets);
		return result;
74
	});
75 76 77
}

CommonEditorRegistry.registerDefaultLanguageCommand('_executeCompletionItemProvider', (model, position, args) => {
78
	return provideCompletionItems(model, position);
J
Johannes Rieken 已提交
79
});