<commit_before>/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'use strict';

import {sequence, asWinJsPromise} from 'vs/base/common/async';
import {isFalsyOrEmpty} from 'vs/base/common/arrays';
import {compare} from 'vs/base/common/strings';
import {onUnexpectedError} from 'vs/base/common/errors';
import {TPromise} from 'vs/base/common/winjs.base';
import {IReadOnlyModel} from 'vs/editor/common/editorCommon';
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 {KbCtxKey} from 'vs/platform/keybinding/common/keybinding';

export const Context = {
	Visible: new KbCtxKey('suggestWidgetVisible'),
	MultipleSuggestions: new KbCtxKey('suggestWidgetMultipleSuggestions'),
	AcceptOnKey: new KbCtxKey('suggestionSupportsAcceptOnKey')
};

export interface ISuggestionItem {
	suggestion: ISuggestion;
	container: ISuggestResult;
	support: ISuggestSupport;
}

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

export interface ISuggestOptions {
	groups?: ISuggestSupport[][];
	snippetConfig?: SnippetConfig;
}

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

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

	const result: ISuggestionItem[] = [];
	const suggestFilter = createSuggesionFilter(options);
	const suggestCompare = createSuggesionComparator(options);

	// 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);


	// 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) {
				return;
			}
			// for each support in the group ask for suggestions
			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;
					}
				}
			}, onUnexpectedError)));
		};
	});

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

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)) {

			fixOverwriteBeforeAfter(suggestion, result);

			bucket.push({
				support,
				suggestion,
				container: result,
			});
		}
	}
	return len !== bucket.length;
}

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;
	}
}

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 {<commit_msg>debt - make snippets come in as completion provider<commit_after>/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'use strict';

import {sequence, asWinJsPromise} from 'vs/base/common/async';
import {isFalsyOrEmpty} from 'vs/base/common/arrays';
import {compare} from 'vs/base/common/strings';
import {onUnexpectedError} from 'vs/base/common/errors';
import {TPromise} from 'vs/base/common/winjs.base';
import {IReadOnlyModel} from 'vs/editor/common/editorCommon';
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 {KbCtxKey} from 'vs/platform/keybinding/common/keybinding';

export const Context = {
	Visible: new KbCtxKey('suggestWidgetVisible'),
	MultipleSuggestions: new KbCtxKey('suggestWidgetMultipleSuggestions'),
	AcceptOnKey: new KbCtxKey('suggestionSupportsAcceptOnKey')
};

export interface ISuggestionItem {
	suggestion: ISuggestion;
	container: ISuggestResult;
	support: ISuggestSupport;
}

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

export interface ISuggestOptions {
	groups?: ISuggestSupport[][];
	snippetConfig?: SnippetConfig;
}


// 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];
	}
};

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

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

	// get provider groups, always add snippet suggestion provider
	const supports = (options.groups || SuggestRegistry.orderedGroups(model));
	supports.unshift([snippetSuggestSupport]);

	// 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 = supports.map(supports => {
		return () => {
			// stop when we have a result
			if (hasResult) {
				return;
			}
			// for each support in the group ask for suggestions
			return TPromise.join(supports.map(support => asWinJsPromise(token => support.provideCompletionItems(model, position, token)).then(values => {

				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
								});
							}
						}
					}
				}

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

			}, onUnexpectedError)));
		};
	});

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

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;
	}
}

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 {
{'fraction_non_alphanumeric': 0.15534308211473566, 'fraction_numerical': 0.0004499437570303712, 'mean_word_length': 4.077669902912621, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5388121', 'n_tokens_mistral': 2693, 'n_tokens_neox': 2428, 'n_words': 625}