suggest.ts 5.3 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, ISuggestion, SuggestRegistry} from 'vs/editor/common/modes';
14
import {ISnippetsRegistry, Extensions} from 'vs/editor/common/modes/snippetsRegistry';
A
Alex Dima 已提交
15
import {Position} from 'vs/editor/common/core/position';
16
import {Registry} from 'vs/platform/platform';
E
Erich Gamma 已提交
17

18 19 20 21 22
export const Context = {
	Visible: 'suggestWidgetVisible',
	MultipleSuggestions: 'suggestWidgetMultipleSuggestions',
	AcceptOnKey: 'suggestionSupportsAcceptOnKey'
};
E
Erich Gamma 已提交
23

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

30 31
export type SnippetConfig = 'top' | 'bottom' | 'inline' | 'none' | 'only';

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

37 38
let snippetsRegistry = <ISnippetsRegistry>Registry.as(Extensions.Snippets);

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

41 42 43
	const result: ISuggestionItem[] = [];
	const suggestFilter = createSuggesionFilter(options);
	const suggestCompare = createSuggesionComparator(options);
44

45 46 47 48 49
	// add suggestions from snippet registry.
	// currentWord is irrelevant, all suggestion use overwriteBefore
	let snippetSuggestResult : ISuggestResult = { suggestions: [], currentWord: '' };
	snippetsRegistry.getSnippetCompletions(model, position, snippetSuggestResult.suggestions);
	fillInSuggestResult(result, snippetSuggestResult, undefined, suggestFilter);
50

51 52 53 54 55 56 57 58

	// 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;
	const factory = (options.groups || SuggestRegistry.orderedGroups(model)).map(supports => {
		return () => {
			// stop when we have a result
			if (hasResult) {
59 60 61
				return;
			}
			// for each support in the group ask for suggestions
62 63 64 65
			return TPromise.join(supports.map(support => asWinJsPromise(token => support.provideCompletionItems(model, position, token)).then(values => {
				if (!isFalsyOrEmpty(values)) {
					for (let suggestResult of values) {
						hasResult = fillInSuggestResult(result, suggestResult, support, suggestFilter) || hasResult;
J
Johannes Rieken 已提交
66
					}
67 68 69 70
				}
			}, onUnexpectedError)));
		};
	});
J
Johannes Rieken 已提交
71

72 73
	return sequence(factory).then(() => result.sort(suggestCompare));
}
74

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
function fillInSuggestResult(bucket: ISuggestionItem[], result: ISuggestResult, support: ISuggestSupport, acceptFn: (c: ISuggestion) => boolean): boolean {
	if (!result) {
		return false;
	}
	if (!result.suggestions) {
		return false;
	}
	const len = bucket.length;
	for (const suggestion of result.suggestions) {
		if (acceptFn(suggestion)) {
			bucket.push({
				support,
				suggestion,
				container: result,
			});
		}
	}
	return len !== bucket.length;
}
94

95 96 97 98 99 100 101 102 103
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;
	}
}
104

105
function createSuggesionComparator(options: ISuggestOptions): (a: ISuggestionItem, b: ISuggestionItem) => number {
106

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
	function defaultComparator(a: ISuggestionItem, b: ISuggestionItem): number {

		if (typeof a.suggestion.sortText === 'string' && typeof b.suggestion.sortText === 'string') {
			const one = a.suggestion.sortText.toLowerCase();
			const other = b.suggestion.sortText.toLowerCase();

			if (one < other) {
				return -1;
			} else if (one > other) {
				return 1;
			}
		}

		return a.suggestion.label.toLowerCase() < b.suggestion.label.toLowerCase() ? -1 : 1;
	}

	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;
			}
		}
131
		return defaultComparator(a, b);
132 133 134 135 136 137 138 139 140 141
	}

	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;
			}
		}
142
		return defaultComparator(a, b);
143 144 145 146 147 148 149 150 151
	}

	if (options.snippetConfig === 'top') {
		return snippetUpComparator;
	} else if (options.snippetConfig === 'bottom') {
		return snippetDownComparator;
	} else {
		return defaultComparator;
	}
152 153 154
}

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