suggest.ts 2.8 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';
11
import {IReadOnlyModel, IPosition} 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';
E
Erich Gamma 已提交
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';

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

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

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

	const resource = model.getAssociatedResource();
31
	const result: ISuggestResult2[] = [];
32 33 34 35 36

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

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

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

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

49
					for (let suggestResult of values) {
50

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

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

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

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

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

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

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