Skip to content

Commit

Permalink
Support show range for inline edits (#237532)
Browse files Browse the repository at this point in the history
Support a show range around the edit
  • Loading branch information
benibenj authored Jan 8, 2025
1 parent e6cba37 commit a016c0b
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 16 deletions.
8 changes: 0 additions & 8 deletions src/vs/editor/common/config/editorOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4200,7 +4200,6 @@ export interface IInlineSuggestOptions {
useWordInsertionView?: 'never' | 'whenPossible';
useWordReplacementView?: 'never' | 'whenPossible';

onlyShowWhenCloseToCursor?: boolean;
useGutterIndicator?: boolean;
};
};
Expand Down Expand Up @@ -4235,7 +4234,6 @@ class InlineEditorSuggest extends BaseEditorOption<EditorOption.inlineSuggest, I
useInterleavedLinesDiff: 'never',
useWordInsertionView: 'whenPossible',
useWordReplacementView: 'whenPossible',
onlyShowWhenCloseToCursor: true,
useGutterIndicator: true,
},
},
Expand Down Expand Up @@ -4304,11 +4302,6 @@ class InlineEditorSuggest extends BaseEditorOption<EditorOption.inlineSuggest, I
description: nls.localize('inlineSuggest.edits.experimental.useWordReplacementView', "Controls whether to enable experimental word replacement view in inline suggestions."),
enum: ['never', 'whenPossible'],
},
'editor.inlineSuggest.edits.experimental.onlyShowWhenCloseToCursor': {
type: 'boolean',
default: defaults.edits.experimental.onlyShowWhenCloseToCursor,
description: nls.localize('inlineSuggest.edits.experimental.onlyShowWhenCloseToCursor', "Controls whether to only show inline suggestions when the cursor is close to the suggestion.")
},
'editor.inlineSuggest.edits.experimental.useGutterIndicator': {
type: 'boolean',
default: defaults.edits.experimental.useGutterIndicator,
Expand Down Expand Up @@ -4338,7 +4331,6 @@ class InlineEditorSuggest extends BaseEditorOption<EditorOption.inlineSuggest, I
useInterleavedLinesDiff: stringSet(input.edits?.experimental?.useInterleavedLinesDiff, this.defaultValue.edits.experimental.useInterleavedLinesDiff, ['never', 'always', 'afterJump']),
useWordInsertionView: stringSet(input.edits?.experimental?.useWordInsertionView, this.defaultValue.edits.experimental.useWordInsertionView, ['never', 'whenPossible']),
useWordReplacementView: stringSet(input.edits?.experimental?.useWordReplacementView, this.defaultValue.edits.experimental.useWordReplacementView, ['never', 'whenPossible']),
onlyShowWhenCloseToCursor: boolean(input.edits?.experimental?.onlyShowWhenCloseToCursor, this.defaultValue.edits.experimental.onlyShowWhenCloseToCursor),
useGutterIndicator: boolean(input.edits?.experimental?.useGutterIndicator, this.defaultValue.edits.experimental.useGutterIndicator),
},
},
Expand Down
3 changes: 3 additions & 0 deletions src/vs/editor/common/languages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,8 @@ export interface InlineCompletion {
readonly completeBracketPairs?: boolean;

readonly isInlineEdit?: boolean;

readonly showRange?: IRange;
}

export interface InlineCompletions<TItem extends InlineCompletion = InlineCompletion> {
Expand Down Expand Up @@ -2396,6 +2398,7 @@ export interface MappedEditsProvider {
export interface IInlineEdit {
text: string;
range: IRange;
showRange?: IRange;
accepted?: Command;
rejected?: Command;
shown?: Command;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ export class InlineCompletionsModel extends Disposable {

private readonly _editorObs = observableCodeEditor(this._editor);

private readonly _onlyShowWhenCloseToCursor = this._editorObs.getOption(EditorOption.inlineSuggest).map(v => !!v.edits.experimental.onlyShowWhenCloseToCursor);
private readonly _suggestPreviewEnabled = this._editorObs.getOption(EditorOption.suggest).map(v => v.preview);
private readonly _suggestPreviewMode = this._editorObs.getOption(EditorOption.suggest).map(v => v.previewMode);
private readonly _inlineSuggestMode = this._editorObs.getOption(EditorOption.inlineSuggest).map(v => v.mode);
Expand Down Expand Up @@ -358,12 +357,13 @@ export class InlineCompletionsModel extends Disposable {

const cursorPos = this.primaryPosition.read(reader);
const cursorAtInlineEdit = LineRange.fromRangeInclusive(edit.range).addMargin(1, 1).contains(cursorPos.lineNumber);
const cursorInsideShowRange = cursorAtInlineEdit || (item.inlineEdit.inlineCompletion.cursorShowRange?.containsPosition(cursorPos) ?? true);

const cursorDist = LineRange.fromRange(edit.range).distanceToLine(this.primaryPosition.read(reader).lineNumber);

if (this._onlyShowWhenCloseToCursor.read(reader) && cursorDist > 3 && !item.inlineEdit.request.isExplicitRequest && !this._inAcceptFlow.read(reader)) {
if (!cursorInsideShowRange && !this._inAcceptFlow.read(reader)) {
return undefined;
}

const cursorDist = LineRange.fromRange(edit.range).distanceToLine(this.primaryPosition.read(reader).lineNumber);
const disableCollapsing = true;
const currentItemIsCollapsed = !disableCollapsing && (cursorDist > 1 && this._collapsedInlineEditId.read(reader) === item.inlineEdit.semanticId);

Expand Down Expand Up @@ -575,6 +575,7 @@ export class InlineCompletionsModel extends Disposable {

editor.pushUndoStop();
if (completion.snippetInfo) {
// ...
editor.executeEdits(
'inlineSuggestion.accept',
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export class InlineEditsAdapter extends Disposable {
items: definedEdits.map(e => {
return {
range: e.result.range,
showRange: e.result.showRange,
insertText: e.result.text,
command: e.result.accepted,
shownCommand: e.result.shown,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ export class InlineCompletionItem {
range,
insertText,
snippetInfo,
Range.lift(inlineCompletion.showRange) ?? undefined,
inlineCompletion.additionalTextEdits || getReadonlyEmptyArray(),
inlineCompletion,
source,
Expand All @@ -345,6 +346,7 @@ export class InlineCompletionItem {
readonly range: Range,
readonly insertText: string,
readonly snippetInfo: SnippetInfo | undefined,
readonly cursorShowRange: Range | undefined,

readonly additionalTextEdits: readonly ISingleEditOperation[],

Expand Down Expand Up @@ -380,6 +382,7 @@ export class InlineCompletionItem {
updatedRange,
this.insertText,
this.snippetInfo,
this.cursorShowRange,
this.additionalTextEdits,
this.sourceInlineCompletion,
this.source,
Expand Down
3 changes: 2 additions & 1 deletion src/vs/monaco.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4607,7 +4607,6 @@ declare namespace monaco.editor {
useInterleavedLinesDiff?: 'never' | 'always' | 'afterJump';
useWordInsertionView?: 'never' | 'whenPossible';
useWordReplacementView?: 'never' | 'whenPossible';
onlyShowWhenCloseToCursor?: boolean;
useGutterIndicator?: boolean;
};
};
Expand Down Expand Up @@ -7277,6 +7276,7 @@ declare namespace monaco.languages {
*/
readonly completeBracketPairs?: boolean;
readonly isInlineEdit?: boolean;
readonly showRange?: IRange;
}

export interface InlineCompletions<TItem extends InlineCompletion = InlineCompletion> {
Expand Down Expand Up @@ -8164,6 +8164,7 @@ declare namespace monaco.languages {
export interface IInlineEdit {
text: string;
range: IRange;
showRange?: IRange;
accepted?: Command;
rejected?: Command;
shown?: Command;
Expand Down
1 change: 1 addition & 0 deletions src/vs/workbench/api/common/extHostLanguageFeatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1577,6 +1577,7 @@ class InlineEditAdapter {
pid,
text: result.text,
range: typeConvert.Range.from(result.range),
showRange: typeConvert.Range.from(result.showRange),
accepted: acceptCommand,
rejected: rejectCommand,
shown: shownCommand,
Expand Down
12 changes: 9 additions & 3 deletions src/vscode-dts/vscode.proposed.inlineEdit.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@ declare module 'vscode' {
readonly text: string;

/**
* An range that will be replaced by the text of the inline edit.
* If change is only additive, this can be empty (same start and end position).
* A range that will be replaced by the text of the inline edit.
* If the change is only additive, this can be empty (same start and end position).
*/
readonly range: Range;

/**
* A range specifying when the edit can be shown based on the cursor position.
* If the cursor is within this range, the inline edit can be displayed.
*/
readonly showRange?: Range;

/**
* An optional command that will be executed after applying the inline edit.
*/
Expand All @@ -36,7 +42,7 @@ declare module 'vscode' {
* Creates a new inline edit.
*
* @param text The new text for this edit.
* @param replaceRange An range that will be replaced by the text of the inline edit.
* @param range A range that will be replaced by the text of the inline edit.
*/
constructor(text: string, range: Range);
}
Expand Down

0 comments on commit a016c0b

Please sign in to comment.