Skip to content

Commit

Permalink
Adds shown command to proposed inline edit API (#233916)
Browse files Browse the repository at this point in the history
  • Loading branch information
hediet authored Nov 15, 2024
1 parent 2fc0143 commit 78742c4
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 5 deletions.
6 changes: 6 additions & 0 deletions src/vs/editor/common/languages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,11 @@ export interface InlineCompletion {

readonly command?: Command;

/**
* Is called the first time an inline completion is shown.
*/
readonly shownCommand?: Command;

/**
* If set to `true`, unopened closing brackets are removed and unclosed opening brackets are closed.
* Defaults to `false`.
Expand Down Expand Up @@ -2345,6 +2350,7 @@ export interface IInlineEdit {
range: IRange;
accepted?: Command;
rejected?: Command;
shown?: Command;
commands?: Command[];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ export class InlineCompletionsModel extends Disposable {

const commands = item.inlineEdit.inlineCompletion.source.inlineCompletions.commands;
const renderExplicitly = this._jumpedTo.read(reader);
const inlineEdit = new InlineEdit(edit, currentItemIsCollapsed, renderExplicitly, commands ?? []);
const inlineEdit = new InlineEdit(edit, currentItemIsCollapsed, renderExplicitly, commands ?? [], item.inlineEdit.inlineCompletion);

return { kind: 'inlineEdit', inlineEdit, inlineCompletion: item.inlineEdit, edits: [edit], cursorAtInlineEdit };
}
Expand Down Expand Up @@ -681,6 +681,17 @@ export class InlineCompletionsModel extends Disposable {
this._editor.focus();
});
}

public async handleInlineCompletionShown(inlineCompletion: InlineCompletionItem): Promise<void> {
if (!inlineCompletion.shownCommand) {
return;
}
if (inlineCompletion.didShow) {
return;
}
inlineCompletion.markAsShown();
await this._commandService.executeCommand(inlineCompletion.shownCommand.id, ...(inlineCompletion.shownCommand.arguments || []));
}
}

interface Repro {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@

import { SingleTextEdit } from '../../../../common/core/textEdit.js';
import { Command } from '../../../../common/languages.js';
import { InlineCompletionItem } from './provideInlineCompletions.js';

export class InlineEdit {
constructor(
public readonly edit: SingleTextEdit,
public readonly isCollapsed: boolean,
public readonly renderExplicitly: boolean,
public readonly commands: readonly Command[],
public readonly inlineCompletion: InlineCompletionItem,
) { }

public get range() {
Expand All @@ -23,6 +25,9 @@ export class InlineEdit {
}

public equals(other: InlineEdit): boolean {
return this.edit.equals(other.edit) && this.isCollapsed === other.isCollapsed && this.renderExplicitly === other.renderExplicitly;
return this.edit.equals(other.edit)
&& this.isCollapsed === other.isCollapsed
&& this.renderExplicitly === other.renderExplicitly
&& this.inlineCompletion === other.inlineCompletion;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export class InlineEditsAdapter extends Disposable {
range: e.result.range,
insertText: e.result.text,
command: e.result.accepted,
shownCommand: e.result.shown,
isInlineEdit: true,
edit: e.result,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ export class InlineCompletionItem {
return new InlineCompletionItem(
insertText,
inlineCompletion.command,
inlineCompletion.shownCommand,
range,
insertText,
snippetInfo,
Expand All @@ -332,9 +333,12 @@ export class InlineCompletionItem {
);
}

private _didCallShow = false;

constructor(
readonly filterText: string,
readonly command: Command | undefined,
readonly shownCommand: Command | undefined,
readonly range: Range,
readonly insertText: string,
readonly snippetInfo: SnippetInfo | undefined,
Expand All @@ -358,10 +362,18 @@ export class InlineCompletionItem {
insertText = filterText.replace(/\r\n|\r/g, '\n');
}

public get didShow(): boolean {
return this._didCallShow;
}
public markAsShown(): void {
this._didCallShow = true;
}

public withRange(updatedRange: Range): InlineCompletionItem {
return new InlineCompletionItem(
this.filterText,
this.command,
this.shownCommand,
updatedRange,
this.insertText,
this.snippetInfo,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ export class InlineEditsView extends Disposable {
const edit = this._edit.read(reader);
if (!edit) { return undefined; }

this._model.get()?.handleInlineCompletionShown(edit.inlineCompletion);

let mappings = RangeMapping.fromEdit(edit.edit);
let newText = edit.edit.apply(edit.originalText);
let diff = lineRangeMappingFromRangeMappings(mappings, edit.originalText, new StringText(newText));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { TextModelText } from '../../../../../common/model/textModelText.js';
import { IModelService } from '../../../../../common/services/model.js';
import { InlineCompletionsModel } from '../../model/inlineCompletionsModel.js';
import { InlineEdit } from '../../model/inlineEdit.js';
import { InlineCompletionItem } from '../../model/provideInlineCompletions.js';
import { InlineEditsView } from './inlineEditsView.js';
import { UniqueUriGenerator } from './utils.js';

Expand Down Expand Up @@ -81,7 +82,7 @@ export class InlineEditsViewAndDiffProducer extends Disposable {
));
const diffEdits = new TextEdit(edits);

return new InlineEditWithChanges(text, diffEdits, inlineEdit.isCollapsed, inlineEdit.renderExplicitly, inlineEdit.commands); //inlineEdit.showInlineIfPossible);
return new InlineEditWithChanges(text, diffEdits, inlineEdit.isCollapsed, inlineEdit.renderExplicitly, inlineEdit.commands, inlineEdit.inlineCompletion); //inlineEdit.showInlineIfPossible);
});
});

Expand Down Expand Up @@ -112,7 +113,8 @@ export class InlineEditWithChanges {
public readonly edit: TextEdit,
public readonly isCollapsed: boolean,
public readonly userJumpedToIt: boolean,
public readonly commands: readonly Command[]
public readonly commands: readonly Command[],
public readonly inlineCompletion: InlineCompletionItem,
) {
}

Expand All @@ -121,6 +123,7 @@ export class InlineEditWithChanges {
this.edit.equals(other.edit) &&
this.isCollapsed === other.isCollapsed &&
this.userJumpedToIt === other.userJumpedToIt &&
this.commands === other.commands;
this.commands === other.commands &&
this.inlineCompletion === other.inlineCompletion;
}
}
5 changes: 5 additions & 0 deletions src/vs/monaco.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7246,6 +7246,10 @@ declare namespace monaco.languages {
*/
readonly range?: IRange;
readonly command?: Command;
/**
* Is called the first time an inline completion is shown.
*/
readonly shownCommand?: Command;
/**
* If set to `true`, unopened closing brackets are removed and unclosed opening brackets are closed.
* Defaults to `false`.
Expand Down Expand Up @@ -8134,6 +8138,7 @@ declare namespace monaco.languages {
range: IRange;
accepted?: Command;
rejected?: Command;
shown?: Command;
commands?: Command[];
}

Expand Down
9 changes: 9 additions & 0 deletions src/vs/workbench/api/common/extHostLanguageFeatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1554,6 +1554,14 @@ class InlineEditAdapter {
rejectCommand = this._commands.toInternal(result.rejected, disposableStore);
}

let shownCommand: languages.Command | undefined = undefined;
if (result.shown) {
if (!disposableStore) {
disposableStore = new DisposableStore();
}
shownCommand = this._commands.toInternal(result.shown, disposableStore);
}

if (!disposableStore) {
disposableStore = new DisposableStore();
}
Expand All @@ -1563,6 +1571,7 @@ class InlineEditAdapter {
range: typeConvert.Range.from(result.range),
accepted: acceptCommand,
rejected: rejectCommand,
shown: shownCommand,
commands: result.commands?.map(c => this._commands.toInternal(c, disposableStore)),
};

Expand Down
2 changes: 2 additions & 0 deletions src/vscode-dts/vscode.proposed.inlineEdit.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ declare module 'vscode' {
*/
rejected?: Command;

shown?: Command;

commands?: Command[];

/**
Expand Down

0 comments on commit 78742c4

Please sign in to comment.