From 67a854871255a707c6072c88b986de9dc7a3b2ce Mon Sep 17 00:00:00 2001 From: Henning Dieterichs Date: Mon, 31 May 2021 16:28:44 +0200 Subject: [PATCH] Expose the inline completions provider in the monaco editor. --- src/vs/editor/common/modes.ts | 18 ++++----- .../common/standalone/standaloneEnums.ts | 2 +- .../standalone/browser/standaloneLanguages.ts | 8 ++++ src/vs/monaco.d.ts | 38 ++++++++++++++++++- 4 files changed, 54 insertions(+), 12 deletions(-) diff --git a/src/vs/editor/common/modes.ts b/src/vs/editor/common/modes.ts index 602f8ba5f50..f42c4ee0307 100644 --- a/src/vs/editor/common/modes.ts +++ b/src/vs/editor/common/modes.ts @@ -686,7 +686,7 @@ export interface CompletionItemProvider { } /** - * How an {@link InlineCompletionItemProvider inline completion provider} was triggered. + * How an {@link InlineCompletionsProvider inline completion provider} was triggered. */ export enum InlineCompletionTriggerKind { /** @@ -709,9 +709,6 @@ export interface InlineCompletionContext { readonly triggerKind: InlineCompletionTriggerKind; } -/** - * @internal - */ export interface InlineCompletion { /** * The text to insert. @@ -729,20 +726,21 @@ export interface InlineCompletion { readonly command?: Command; } -/** - * @internal - */ export interface InlineCompletions { readonly items: readonly TItem[]; } -/** - * @internal - */ export interface InlineCompletionsProvider { provideInlineCompletions(model: model.ITextModel, position: Position, context: InlineCompletionContext, token: CancellationToken): ProviderResult; + /** + * Will be called when an item is shown. + */ handleItemDidShow?(completions: T, item: T['items'][number]): void; + + /** + * Will be called when a completions list is no longer in use and can be garbage-collected. + */ freeInlineCompletions(completions: T): void; } diff --git a/src/vs/editor/common/standalone/standaloneEnums.ts b/src/vs/editor/common/standalone/standaloneEnums.ts index 7dc1e7418e4..b386af2bb3b 100644 --- a/src/vs/editor/common/standalone/standaloneEnums.ts +++ b/src/vs/editor/common/standalone/standaloneEnums.ts @@ -361,7 +361,7 @@ export enum InlayHintKind { } /** - * How an {@link InlineCompletionItemProvider inline completion provider} was triggered. + * How an {@link InlineCompletionsProvider inline completion provider} was triggered. */ export enum InlineCompletionTriggerKind { /** diff --git a/src/vs/editor/standalone/browser/standaloneLanguages.ts b/src/vs/editor/standalone/browser/standaloneLanguages.ts index b4da8233937..e4b78662521 100644 --- a/src/vs/editor/standalone/browser/standaloneLanguages.ts +++ b/src/vs/editor/standalone/browser/standaloneLanguages.ts @@ -547,6 +547,13 @@ export function registerDocumentRangeSemanticTokensProvider(languageId: string, return modes.DocumentRangeSemanticTokensProviderRegistry.register(languageId, provider); } +/** + * Register an inline completions provider. + */ +export function registerInlineCompletionsProvider(languageId: string, provider: modes.InlineCompletionsProvider): IDisposable { + return modes.InlineCompletionsProviderRegistry.register(languageId, provider); +} + /** * Contains additional diagnostic information about the context in which * a [code action](#CodeActionProvider.provideCodeActions) is run. @@ -613,6 +620,7 @@ export function createMonacoLanguagesAPI(): typeof monaco.languages { registerSelectionRangeProvider: registerSelectionRangeProvider, registerDocumentSemanticTokensProvider: registerDocumentSemanticTokensProvider, registerDocumentRangeSemanticTokensProvider: registerDocumentRangeSemanticTokensProvider, + registerInlineCompletionsProvider: registerInlineCompletionsProvider, // enums DocumentHighlightKind: standaloneEnums.DocumentHighlightKind, diff --git a/src/vs/monaco.d.ts b/src/vs/monaco.d.ts index d8dbc2f8e90..338e9eed7f4 100644 --- a/src/vs/monaco.d.ts +++ b/src/vs/monaco.d.ts @@ -5330,6 +5330,11 @@ declare namespace monaco.languages { */ export function registerDocumentRangeSemanticTokensProvider(languageId: string, provider: DocumentRangeSemanticTokensProvider): IDisposable; + /** + * Register an inline completions provider. + */ + export function registerInlineCompletionsProvider(languageId: string, provider: InlineCompletionsProvider): IDisposable; + /** * Contains additional diagnostic information about the context in which * a [code action](#CodeActionProvider.provideCodeActions) is run. @@ -5837,7 +5842,7 @@ declare namespace monaco.languages { } /** - * How an {@link InlineCompletionItemProvider inline completion provider} was triggered. + * How an {@link InlineCompletionsProvider inline completion provider} was triggered. */ export enum InlineCompletionTriggerKind { /** @@ -5859,6 +5864,37 @@ declare namespace monaco.languages { readonly triggerKind: InlineCompletionTriggerKind; } + export interface InlineCompletion { + /** + * The text to insert. + * If the text contains a line break, the range must end at the end of a line. + * If existing text should be replaced, the existing text must be a prefix of the text to insert. + */ + readonly text: string; + /** + * The range to replace. + * Must begin and end on the same line. + */ + readonly range?: IRange; + readonly command?: Command; + } + + export interface InlineCompletions { + readonly items: readonly TItem[]; + } + + export interface InlineCompletionsProvider { + provideInlineCompletions(model: editor.ITextModel, position: Position, context: InlineCompletionContext, token: CancellationToken): ProviderResult; + /** + * Will be called when an item is shown. + */ + handleItemDidShow?(completions: T, item: T['items'][number]): void; + /** + * Will be called when a completions list is no longer in use and can be garbage-collected. + */ + freeInlineCompletions(completions: T): void; + } + export interface CodeAction { title: string; command?: Command;