suggest.ts 7.4 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';

J
Johannes Rieken 已提交
7 8 9 10 11 12
import { sequence, asWinJsPromise } from 'vs/base/common/async';
import { isFalsyOrEmpty } from 'vs/base/common/arrays';
import { compare } from 'vs/base/common/strings';
import { assign } from 'vs/base/common/objects';
import { onUnexpectedError } from 'vs/base/common/errors';
import { TPromise } from 'vs/base/common/winjs.base';
13
import { IModel, IPosition } from 'vs/editor/common/editorCommon';
J
Johannes Rieken 已提交
14 15 16 17 18 19 20
import { CommonEditorRegistry } from 'vs/editor/common/editorCommonExtensions';
import { ISuggestResult, ISuggestSupport, ISuggestion, SuggestRegistry } from 'vs/editor/common/modes';
import { ISnippetsRegistry, Extensions } from 'vs/editor/common/modes/snippetsRegistry';
import { Position } from 'vs/editor/common/core/position';
import { Registry } from 'vs/platform/platform';
import { RawContextKey } from 'vs/platform/contextkey/common/contextkey';
import { DefaultConfig } from 'vs/editor/common/config/defaultConfig';
E
Erich Gamma 已提交
21

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

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

37
export type SnippetConfig = 'top' | 'bottom' | 'inline' | 'none';
38

39 40

// add suggestions from snippet registry.
41
export const snippetSuggestSupport: ISuggestSupport = {
42 43 44

	triggerCharacters: [],

45
	provideCompletionItems(model: IModel, position: Position): ISuggestResult {
46 47
		const suggestions = Registry.as<ISnippetsRegistry>(Extensions.Snippets).getSnippetCompletions(model, position);
		if (suggestions) {
48
			return { suggestions };
49
		}
50 51
	}
};
52

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

55
	const allSuggestions: ISuggestionItem[] = [];
56
	const acceptSuggestion = createSuggesionFilter(snippetConfig);
57

58 59
	position = position.clone();

60
	// get provider groups, always add snippet suggestion provider
61
	const supports = SuggestRegistry.orderedGroups(model);
62 63 64 65 66

	// add snippets provider unless turned off
	if (snippetConfig !== 'none') {
		supports.unshift([snippetSuggestSupport]);
	}
67 68 69 70

	// 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;
71
	const factory = supports.map(supports => {
72 73 74
		return () => {
			// stop when we have a result
			if (hasResult) {
75 76 77
				return;
			}
			// for each support in the group ask for suggestions
78 79 80 81 82
			return TPromise.join(supports.map(support => {

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

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

86
					const len = allSuggestions.length;
87

88 89 90
					if (container && !isFalsyOrEmpty(container.suggestions)) {
						for (let suggestion of container.suggestions) {
							if (acceptSuggestion(suggestion)) {
91

92 93
								fixOverwriteBeforeAfter(suggestion, container);

94
								allSuggestions.push({
95
									position,
96 97 98 99 100 101
									container,
									suggestion,
									support,
									resolve: createSuggestionResolver(support, suggestion, model, position)
								});
							}
102
						}
J
Johannes Rieken 已提交
103
					}
104

105
					if (len !== allSuggestions.length && support !== snippetSuggestSupport) {
106 107
						hasResult = true;
					}
108

109 110
				}, onUnexpectedError);
			}));
111 112
		};
	});
J
Johannes Rieken 已提交
113

114 115 116
	const result = sequence(factory).then(() => allSuggestions.sort(getSuggestionComparator(snippetConfig)));

	// result.then(items => {
J
Johannes Rieken 已提交
117
	// 	console.log(model.getWordUntilPosition(position), items.map(item => `${item.suggestion.label}, type=${item.suggestion.type}, incomplete?${item.container.incomplete}, overwriteBefore=${item.suggestion.overwriteBefore}`));
118 119 120 121 122 123
	// 	return items;
	// }, err => {
	// 	console.warn(model.getWordUntilPosition(position), err);
	// });

	return result;
124
}
125

126 127
function fixOverwriteBeforeAfter(suggestion: ISuggestion, container: ISuggestResult): void {
	if (typeof suggestion.overwriteBefore !== 'number') {
128
		suggestion.overwriteBefore = 0;
129 130 131 132 133 134
	}
	if (typeof suggestion.overwriteAfter !== 'number' || suggestion.overwriteAfter < 0) {
		suggestion.overwriteAfter = 0;
	}
}

135
function createSuggestionResolver(provider: ISuggestSupport, suggestion: ISuggestion, model: IModel, position: Position): () => TPromise<void> {
136 137 138 139 140 141 142 143 144
	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);
	};
}

145 146
function createSuggesionFilter(snippetConfig: SnippetConfig): (candidate: ISuggestion) => boolean {
	if (snippetConfig === 'none') {
147 148
		return suggestion => suggestion.type !== 'snippet';
	} else {
149
		return () => true;
150 151
	}
}
152

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

155 156
	function defaultComparator(a: ISuggestionItem, b: ISuggestionItem): number {

157 158 159
		let ret = 0;

		// check with 'sortText'
160
		if (typeof a.suggestion.sortText === 'string' && typeof b.suggestion.sortText === 'string') {
161 162
			ret = compare(a.suggestion.sortText.toLowerCase(), b.suggestion.sortText.toLowerCase());
		}
163

164 165 166 167 168 169 170 171 172 173 174
		// 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;
175 176 177
			}
		}

178
		return ret;
179 180 181 182 183 184 185 186 187 188
	}

	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;
			}
		}
189
		return defaultComparator(a, b);
190 191 192 193 194 195 196 197 198 199
	}

	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;
			}
		}
200
		return defaultComparator(a, b);
201 202
	}

203
	if (snippetConfig === 'top') {
204
		return snippetUpComparator;
205
	} else if (snippetConfig === 'bottom') {
206 207 208 209
		return snippetDownComparator;
	} else {
		return defaultComparator;
	}
210 211 212
}

CommonEditorRegistry.registerDefaultLanguageCommand('_executeCompletionItemProvider', (model, position, args) => {
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227

	const result: ISuggestResult = {
		incomplete: false,
		suggestions: []
	};

	return provideSuggestionItems(model, position).then(items => {

		for (const {container, suggestion} of items) {
			result.incomplete = result.incomplete || container.incomplete;
			result.suggestions.push(suggestion);
		}

		return result;
	});
J
Johannes Rieken 已提交
228
});