src/vs/editor/contrib/inlineCompletions/browser/inlineCompletionsModel.ts/provideInlineCompletions
export async function provideInlineCompletions(
	registry: LanguageFeatureRegistry<InlineCompletionsProvider>,
	position: Position,
	model: ITextModel,
	context: InlineCompletionContext,
	token: CancellationToken = CancellationToken.None,
	languageConfigurationService?: ILanguageConfigurationService
): Promise<TrackedInlineCompletions> {
	const defaultReplaceRange = getDefaultRange(position, model);

	const providers = registry.all(model);
	const results = await Promise.all(
		providers.map(
			async provider => {
				const completions = await Promise.resolve(provider.provideInlineCompletions(model, position, context, token)).catch(onUnexpectedExternalError);
				return ({
					completions,
					provider,
					dispose: () => {
						if (completions) {
							provider.freeInlineCompletions(completions);
						}
					}
				});
			}
		)
	);

	const itemsByHash = new Map<string, TrackedInlineCompletion>();
	for (const result of results) {
		const completions = result.completions;
		if (!completions) {
			continue;
		}

		for (const item of completions.items) {
			let range = item.range ? Range.lift(item.range) : defaultReplaceRange;

			if (range.startLineNumber !== range.endLineNumber) {
				// Ignore invalid ranges.
				continue;
			}

			let insertText: string;
			let snippetInfo: {
				snippet: string;
				/* Could be different than the main range */
				range: Range;
			}
				| undefined;

			if (typeof item.insertText === 'string') {
				insertText = item.insertText;

				if (languageConfigurationService && item.completeBracketPairs) {
					insertText = closeBrackets(
						insertText,
						range.getStartPosition(),
						model,
						languageConfigurationService
					);

					// Modify range depending on if brackets are added or removed
					const diff = insertText.length - item.insertText.length;
					if (diff !== 0) {
						range = new Range(range.startLineNumber, range.startColumn, range.endLineNumber, range.endColumn + diff);
					}
				}

				snippetInfo = undefined;
			} else if ('snippet' in item.insertText) {
				const preBracketCompletionLength = item.insertText.snippet.length;

				if (languageConfigurationService && item.completeBracketPairs) {
					item.insertText.snippet = closeBrackets(
						item.insertText.snippet,
						range.getStartPosition(),
						model,
						languageConfigurationService
					);

					// Modify range depending on if brackets are added or removed
					const diff = item.insertText.snippet.length - preBracketCompletionLength;
					if (diff !== 0) {
						range = new Range(range.startLineNumber, range.startColumn, range.endLineNumber, range.endColumn + diff);
					}
				}

				const snippet = new SnippetParser().parse(item.insertText.snippet);

				if (snippet.children.length === 1 && snippet.children[0] instanceof Text) {
					insertText = snippet.children[0].value;
					snippetInfo = undefined;
				} else {
					insertText = snippet.toString();
					snippetInfo = {
						snippet: item.insertText.snippet,
						range: range
					};
				}
			} else {
				assertNever(item.insertText);
			}

			const trackedItem: TrackedInlineCompletion = ({
				insertText,
				snippetInfo,
				range,
				command: item.command,
				sourceProvider: result.provider,
				sourceInlineCompletions: completions,
				sourceInlineCompletion: item,
				filterText: item.filterText || insertText,
				additionalTextEdits: item.additionalTextEdits || getReadonlyEmptyArray()
			});

			itemsByHash.set(JSON.stringify({ insertText, range: item.range }), trackedItem);
		}
	}

	return {
		items: [...itemsByHash.values()],
		dispose: () => {
			for (const result of results) {
				result.dispose();
			}
		},
	};
}