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

20
export const Context = {
A
Alex Dima 已提交
21 22 23
	Visible: new KbCtxKey('suggestWidgetVisible'),
	MultipleSuggestions: new KbCtxKey('suggestWidgetMultipleSuggestions'),
	AcceptOnKey: new KbCtxKey('suggestionSupportsAcceptOnKey')
24
};
E
Erich Gamma 已提交
25

26 27 28 29
export interface ISuggestionItem {
	suggestion: ISuggestion;
	container: ISuggestResult;
	support: ISuggestSupport;
30 31
}

32 33
export type SnippetConfig = 'top' | 'bottom' | 'inline' | 'none' | 'only';

34 35
export interface ISuggestOptions {
	groups?: ISuggestSupport[][];
36
	snippetConfig?: SnippetConfig;
37
}
38

39 40 41 42 43 44 45 46 47 48 49 50 51

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

	triggerCharacters: [],

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

53
export function provideSuggestionItems(model: IReadOnlyModel, position: Position, options: ISuggestOptions = {}): TPromise<ISuggestionItem[]> {
54

55
	const result: ISuggestionItem[] = [];
56
	const acceptSuggestion = createSuggesionFilter(options);
57

58 59 60
	// get provider groups, always add snippet suggestion provider
	const supports = (options.groups || SuggestRegistry.orderedGroups(model));
	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
			return TPromise.join(supports.map(support => asWinJsPromise(token => support.provideCompletionItems(model, position, token)).then(values => {
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

				if (isFalsyOrEmpty(values)) {
					return;
				}

				let oldLen = result.length;

				for (let container of values) {
					if (container && !isFalsyOrEmpty(container.suggestions)) {
						for (let suggestion of container.suggestions) {
							if (acceptSuggestion(suggestion)) {

								fixOverwriteBeforeAfter(suggestion, container);

								result.push({
									container,
									suggestion,
									support
								});
							}
						}
J
Johannes Rieken 已提交
94
					}
95
				}
96 97 98 99 100

				if (oldLen !== result.length && support !== snippetSuggestSupport) {
					hasResult = true;
				}

101 102 103
			}, onUnexpectedError)));
		};
	});
J
Johannes Rieken 已提交
104

105
	return sequence(factory).then(() => result.sort(createSuggesionComparator(options)));
106
}
107

108 109 110 111 112 113 114 115 116
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;
	}
}

117 118 119 120 121 122 123 124 125
function createSuggesionFilter(options: ISuggestOptions): (candidate: ISuggestion) => boolean {
	if (options.snippetConfig === 'only') {
		return suggestion => suggestion.type === 'snippet';
	} else if (options.snippetConfig === 'none') {
		return suggestion => suggestion.type !== 'snippet';
	} else {
		return _ => true;
	}
}
126

127
function createSuggesionComparator(options: ISuggestOptions): (a: ISuggestionItem, b: ISuggestionItem) => number {
128

129 130
	function defaultComparator(a: ISuggestionItem, b: ISuggestionItem): number {

131 132 133
		let ret = 0;

		// check with 'sortText'
134
		if (typeof a.suggestion.sortText === 'string' && typeof b.suggestion.sortText === 'string') {
135 136
			ret = compare(a.suggestion.sortText.toLowerCase(), b.suggestion.sortText.toLowerCase());
		}
137

138 139 140 141 142 143 144 145 146 147 148
		// 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;
149 150 151
			}
		}

152
		return ret;
153 154 155 156 157 158 159 160 161 162
	}

	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;
			}
		}
163
		return defaultComparator(a, b);
164 165 166 167 168 169 170 171 172 173
	}

	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;
			}
		}
174
		return defaultComparator(a, b);
175 176 177 178 179 180 181 182 183
	}

	if (options.snippetConfig === 'top') {
		return snippetUpComparator;
	} else if (options.snippetConfig === 'bottom') {
		return snippetDownComparator;
	} else {
		return defaultComparator;
	}
184 185 186
}

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