suggest.ts 6.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 {compare} from 'vs/base/common/strings';
10
import {assign} from 'vs/base/common/objects';
11
import {onUnexpectedError} from 'vs/base/common/errors';
12
import {TPromise} from 'vs/base/common/winjs.base';
13
import {IReadOnlyModel, IPosition} from 'vs/editor/common/editorCommon';
14
import {CommonEditorRegistry} from 'vs/editor/common/editorCommonExtensions';
15
import {ISuggestResult, ISuggestSupport, ISuggestion, SuggestRegistry} from 'vs/editor/common/modes';
16
import {ISnippetsRegistry, Extensions} from 'vs/editor/common/modes/snippetsRegistry';
A
Alex Dima 已提交
17
import {Position} from 'vs/editor/common/core/position';
18
import {Registry} from 'vs/platform/platform';
A
Alex Dima 已提交
19
import {RawContextKey} from 'vs/platform/contextkey/common/contextkey';
E
Erich Gamma 已提交
20

21
export const Context = {
A
Alex Dima 已提交
22 23 24
	Visible: new RawContextKey<boolean>('suggestWidgetVisible', false),
	MultipleSuggestions: new RawContextKey<boolean>('suggestWidgetMultipleSuggestions', false),
	AcceptOnKey: new RawContextKey<boolean>('suggestionSupportsAcceptOnKey', true)
25
};
E
Erich Gamma 已提交
26

27
export interface ISuggestionItem {
28
	position: IPosition;
29 30 31
	suggestion: ISuggestion;
	container: ISuggestResult;
	support: ISuggestSupport;
32
	resolve(): TPromise<void>;
33 34
}

35
export type SnippetConfig = 'top' | 'bottom' | 'inline' | 'none';
36

37 38 39 40 41 42

// add suggestions from snippet registry.
const snippetSuggestSupport: ISuggestSupport = {

	triggerCharacters: [],

43
	provideCompletionItems(model: IReadOnlyModel, position: Position): ISuggestResult {
44 45 46
		// currentWord is irrelevant, all suggestion use overwriteBefore
		const result: ISuggestResult = { suggestions: [], currentWord: '' };
		Registry.as<ISnippetsRegistry>(Extensions.Snippets).getSnippetCompletions(model, position, result.suggestions);
47
		return result;
48 49
	}
};
50

51
export function provideSuggestionItems(model: IReadOnlyModel, position: Position, snippetConfig: SnippetConfig = 'bottom', onlyFrom?: ISuggestSupport[]): TPromise<ISuggestionItem[]> {
52

53
	const result: ISuggestionItem[] = [];
54
	const acceptSuggestion = createSuggesionFilter(snippetConfig);
55

56 57
	position = position.clone();

58
	// get provider groups, always add snippet suggestion provider
59
	const supports = SuggestRegistry.orderedGroups(model);
60
	supports.unshift([snippetSuggestSupport]);
61 62 63 64

	// add suggestions from contributed providers - providers are ordered in groups of
	// equal score and once a group produces a result the process stops
	let hasResult = false;
65
	const factory = supports.map(supports => {
66 67 68
		return () => {
			// stop when we have a result
			if (hasResult) {
69 70 71
				return;
			}
			// for each support in the group ask for suggestions
72 73 74 75 76
			return TPromise.join(supports.map(support => {

				if (!isFalsyOrEmpty(onlyFrom) && onlyFrom.indexOf(support) < 0) {
					return;
				}
77

78
				return asWinJsPromise(token => support.provideCompletionItems(model, position, token)).then(container => {
79

80
					const len = result.length;
81

82 83 84
					if (container && !isFalsyOrEmpty(container.suggestions)) {
						for (let suggestion of container.suggestions) {
							if (acceptSuggestion(suggestion)) {
85

86 87 88
								fixOverwriteBeforeAfter(suggestion, container);

								result.push({
89
									position,
90 91 92 93 94 95
									container,
									suggestion,
									support,
									resolve: createSuggestionResolver(support, suggestion, model, position)
								});
							}
96
						}
J
Johannes Rieken 已提交
97
					}
98

99 100 101
					if (len !== result.length && support !== snippetSuggestSupport) {
						hasResult = true;
					}
102

103 104
				}, onUnexpectedError);
			}));
105 106
		};
	});
J
Johannes Rieken 已提交
107

J
Johannes Rieken 已提交
108
	return sequence(factory).then(() => result.sort(getSuggestionComparator(snippetConfig)));
109
}
110

111 112 113 114 115 116 117 118 119
function fixOverwriteBeforeAfter(suggestion: ISuggestion, container: ISuggestResult): void {
	if (typeof suggestion.overwriteBefore !== 'number') {
		suggestion.overwriteBefore = container.currentWord.length;
	}
	if (typeof suggestion.overwriteAfter !== 'number' || suggestion.overwriteAfter < 0) {
		suggestion.overwriteAfter = 0;
	}
}

120 121 122 123 124 125 126 127 128 129
function createSuggestionResolver(provider: ISuggestSupport, suggestion: ISuggestion, model: IReadOnlyModel, position: Position): () => TPromise<void> {
	return () => {
		if (typeof provider.resolveCompletionItem === 'function') {
			return asWinJsPromise(token => provider.resolveCompletionItem(model, position, suggestion, token))
				.then(value => { assign(suggestion, value); });
		}
		return TPromise.as(void 0);
	};
}

130 131
function createSuggesionFilter(snippetConfig: SnippetConfig): (candidate: ISuggestion) => boolean {
	if (snippetConfig === 'none') {
132 133
		return suggestion => suggestion.type !== 'snippet';
	} else {
134
		return () => true;
135 136
	}
}
137

J
Johannes Rieken 已提交
138
export function getSuggestionComparator(snippetConfig: SnippetConfig): (a: ISuggestionItem, b: ISuggestionItem) => number {
139

140 141
	function defaultComparator(a: ISuggestionItem, b: ISuggestionItem): number {

142 143 144
		let ret = 0;

		// check with 'sortText'
145
		if (typeof a.suggestion.sortText === 'string' && typeof b.suggestion.sortText === 'string') {
146 147
			ret = compare(a.suggestion.sortText.toLowerCase(), b.suggestion.sortText.toLowerCase());
		}
148

149 150 151 152 153 154 155 156 157 158 159
		// check with 'label'
		if (!ret) {
			ret = compare(a.suggestion.label.toLowerCase(), b.suggestion.label.toLowerCase());
		}

		// check with 'type' and lower snippets
		if (!ret && a.suggestion.type !== b.suggestion.type) {
			if (a.suggestion.type === 'snippet') {
				ret = 1;
			} else if (b.suggestion.type === 'snippet') {
				ret = -1;
160 161 162
			}
		}

163
		return ret;
164 165 166 167 168 169 170 171 172 173
	}

	function snippetUpComparator(a: ISuggestionItem, b: ISuggestionItem): number {
		if (a.suggestion.type !== b.suggestion.type) {
			if (a.suggestion.type === 'snippet') {
				return -1;
			} else if (b.suggestion.type === 'snippet') {
				return 1;
			}
		}
174
		return defaultComparator(a, b);
175 176 177 178 179 180 181 182 183 184
	}

	function snippetDownComparator(a: ISuggestionItem, b: ISuggestionItem): number {
		if (a.suggestion.type !== b.suggestion.type) {
			if (a.suggestion.type === 'snippet') {
				return 1;
			} else if (b.suggestion.type === 'snippet') {
				return -1;
			}
		}
185
		return defaultComparator(a, b);
186 187
	}

188
	if (snippetConfig === 'top') {
189
		return snippetUpComparator;
190
	} else if (snippetConfig === 'bottom') {
191 192 193 194
		return snippetDownComparator;
	} else {
		return defaultComparator;
	}
195 196 197
}

CommonEditorRegistry.registerDefaultLanguageCommand('_executeCompletionItemProvider', (model, position, args) => {
198
	return provideSuggestionItems(model, position);
J
Johannes Rieken 已提交
199
});