Skip to content

Commit

Permalink
Merge pull request #172800 from microsoft/hediet/miserable-manatee
Browse files Browse the repository at this point in the history
Fixes #172384
  • Loading branch information
hediet committed Jan 31, 2023
2 parents d11134f + c3bf0fb commit 93feda3
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/vs/editor/common/languages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,11 @@ export interface InlineCompletionsProvider<T extends InlineCompletions = InlineC
*/
handleItemDidShow?(completions: T, item: T['items'][number]): void;

/**
* Will be called when an item is partially accepted.
*/
handlePartialAccept?(completions: T, item: T['items'][number], acceptedCharacters: number): void;

/**
* Will be called when a completions list is no longer in use and can be garbage-collected.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,20 @@ export class InlineCompletionsSession extends BaseGhostTextWidgetModel {
]
);
this.editor.setPosition(position.delta(0, partialText.length));

if (completion.sourceProvider.handlePartialAccept) {
const acceptedRange = Range.fromPositions(completion.range.getStartPosition(), position.delta(0, acceptUntilIndexExclusive));

// This assumes that the inline completion and the model use the same EOL style.
// This is not a problem at the moment, because partial acceptance only works for the first line of an
// inline completion.
const text = this.editor.getModel()!.getValueInRange(acceptedRange);
completion.sourceProvider.handlePartialAccept(
completion.sourceInlineCompletions,
completion.sourceInlineCompletion,
text.length,
);
}
}

public commitCurrentCompletion(): void {
Expand Down
4 changes: 4 additions & 0 deletions src/vs/monaco.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6733,6 +6733,10 @@ declare namespace monaco.languages {
* Will be called when an item is shown.
*/
handleItemDidShow?(completions: T, item: T['items'][number]): void;
/**
* Will be called when an item is partially accepted.
*/
handlePartialAccept?(completions: T, item: T['items'][number], acceptedCharacters: number): void;
/**
* Will be called when a completions list is no longer in use and can be garbage-collected.
*/
Expand Down
9 changes: 7 additions & 2 deletions src/vs/workbench/api/browser/mainThreadLanguageFeatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,16 +537,21 @@ export class MainThreadLanguageFeatures extends Disposable implements MainThread
this._registrations.set(handle, this._languageFeaturesService.completionProvider.register(selector, provider));
}

$registerInlineCompletionsSupport(handle: number, selector: IDocumentFilterDto[], supportsHandleDidShowCompletionItem: boolean): void {
$registerInlineCompletionsSupport(handle: number, selector: IDocumentFilterDto[], supportsHandleEvents: boolean): void {
const provider: languages.InlineCompletionsProvider<IdentifiableInlineCompletions> = {
provideInlineCompletions: async (model: ITextModel, position: EditorPosition, context: languages.InlineCompletionContext, token: CancellationToken): Promise<IdentifiableInlineCompletions | undefined> => {
return this._proxy.$provideInlineCompletions(handle, model.uri, position, context, token);
},
handleItemDidShow: async (completions: IdentifiableInlineCompletions, item: IdentifiableInlineCompletion): Promise<void> => {
if (supportsHandleDidShowCompletionItem) {
if (supportsHandleEvents) {
await this._proxy.$handleInlineCompletionDidShow(handle, completions.pid, item.idx);
}
},
handlePartialAccept: async (completions, item, acceptedCharacters): Promise<void> => {
if (supportsHandleEvents) {
await this._proxy.$handleInlineCompletionPartialAccept(handle, completions.pid, item.idx, acceptedCharacters);
}
},
freeInlineCompletions: (completions: IdentifiableInlineCompletions): void => {
this._proxy.$freeInlineCompletionsList(handle, completions.pid);
}
Expand Down
3 changes: 3 additions & 0 deletions src/vs/workbench/api/common/extHost.api.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,9 @@ export function createApiFactoryAndRegisterActors(accessor: ServicesAccessor): I
if (provider.handleDidShowCompletionItem) {
checkProposedApiEnabled(extension, 'inlineCompletionsAdditions');
}
if (provider.handleDidPartiallyAcceptCompletionItem) {
checkProposedApiEnabled(extension, 'inlineCompletionsAdditions');
}
return extHostLanguageFeatures.registerInlineCompletionsProvider(extension, checkSelector(selector), provider);
},
registerDocumentLinkProvider(selector: vscode.DocumentSelector, provider: vscode.DocumentLinkProvider): vscode.Disposable {
Expand Down
1 change: 1 addition & 0 deletions src/vs/workbench/api/common/extHost.protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1784,6 +1784,7 @@ export interface ExtHostLanguageFeaturesShape {
$releaseCompletionItems(handle: number, id: number): void;
$provideInlineCompletions(handle: number, resource: UriComponents, position: IPosition, context: languages.InlineCompletionContext, token: CancellationToken): Promise<IdentifiableInlineCompletions | undefined>;
$handleInlineCompletionDidShow(handle: number, pid: number, idx: number): void;
$handleInlineCompletionPartialAccept(handle: number, pid: number, idx: number, acceptedCharacters: number): void;
$freeInlineCompletionsList(handle: number, pid: number): void;
$provideSignatureHelp(handle: number, resource: UriComponents, position: IPosition, context: languages.SignatureHelpContext, token: CancellationToken): Promise<ISignatureHelpDto | undefined>;
$releaseSignatureHelp(handle: number, id: number): void;
Expand Down
25 changes: 22 additions & 3 deletions src/vs/workbench/api/common/extHostLanguageFeatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1074,6 +1074,8 @@ class InlineCompletionAdapterBase {
disposeCompletions(pid: number): void { }

handleDidShowCompletionItem(pid: number, idx: number): void { }

handlePartialAccept(pid: number, idx: number, acceptedCharacters: number): void { }
}

class InlineCompletionAdapter extends InlineCompletionAdapterBase {
Expand All @@ -1093,8 +1095,10 @@ class InlineCompletionAdapter extends InlineCompletionAdapterBase {
super();
}

public get supportsHandleDidShowCompletionItem(): boolean {
return isProposedApiEnabled(this._extension, 'inlineCompletionsAdditions') && typeof this._provider.handleDidShowCompletionItem === 'function';
public get supportsHandleEvents(): boolean {
return isProposedApiEnabled(this._extension, 'inlineCompletionsAdditions')
&& (typeof this._provider.handleDidShowCompletionItem === 'function'
|| typeof this._provider.handleDidPartiallyAcceptCompletionItem === 'function');
}

private readonly languageTriggerKindToVSCodeTriggerKind: Record<languages.InlineCompletionTriggerKind, InlineCompletionTriggerKind> = {
Expand Down Expand Up @@ -1182,6 +1186,15 @@ class InlineCompletionAdapter extends InlineCompletionAdapterBase {
}
}
}

override handlePartialAccept(pid: number, idx: number, acceptedCharacters: number): void {
const completionItem = this._references.get(pid)?.items[idx];
if (completionItem) {
if (this._provider.handleDidPartiallyAcceptCompletionItem && this._isAdditionsProposedApiEnabled) {
this._provider.handleDidPartiallyAcceptCompletionItem(completionItem, acceptedCharacters);
}
}
}
}

class ReferenceMap<T> {
Expand Down Expand Up @@ -2145,7 +2158,7 @@ export class ExtHostLanguageFeatures implements extHostProtocol.ExtHostLanguageF
registerInlineCompletionsProvider(extension: IExtensionDescription, selector: vscode.DocumentSelector, provider: vscode.InlineCompletionItemProvider): vscode.Disposable {
const adapter = new InlineCompletionAdapter(extension, this._documents, provider, this._commands.converter);
const handle = this._addNewAdapter(adapter, extension);
this._proxy.$registerInlineCompletionsSupport(handle, this._transformDocumentSelector(selector), adapter.supportsHandleDidShowCompletionItem);
this._proxy.$registerInlineCompletionsSupport(handle, this._transformDocumentSelector(selector), adapter.supportsHandleEvents);
return this._createDisposable(handle);
}

Expand All @@ -2159,6 +2172,12 @@ export class ExtHostLanguageFeatures implements extHostProtocol.ExtHostLanguageF
}, undefined, undefined);
}

$handleInlineCompletionPartialAccept(handle: number, pid: number, idx: number, acceptedCharacters: number): void {
this._withAdapter(handle, InlineCompletionAdapterBase, async adapter => {
adapter.handlePartialAccept(pid, idx, acceptedCharacters);
}, undefined, undefined);
}

$freeInlineCompletionsList(handle: number, pid: number): void {
this._withAdapter(handle, InlineCompletionAdapterBase, async adapter => { adapter.disposeCompletions(pid); }, undefined, undefined);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ declare module 'vscode' {
export interface InlineCompletionItemProvider {
// eslint-disable-next-line local/vscode-dts-provider-naming
handleDidShowCompletionItem?(completionItem: InlineCompletionItem): void;

/**
* Is called when an inline completion item was accepted partially.
* @param acceptedLength The length of the substring of the inline completion that was accepted already.
*/
// eslint-disable-next-line local/vscode-dts-provider-naming
handleDidPartiallyAcceptCompletionItem?(completionItem: InlineCompletionItem, acceptedLength: number): void;
}

// When finalizing `commands`, make sure to add a corresponding constructor parameter.
Expand Down

0 comments on commit 93feda3

Please sign in to comment.