Skip to content

Reduces flickering of inline completions & fixes context key. #125654

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ export class ActiveGhostTextController extends Disposable {

this._register(this.suggestWidgetAdapterModel.onDidChange(() => {
this.updateModel();
// When the suggest widget becomes inactive and an inline completion
// becomes visible, we need to update the context keys.
this.updateContextKeys();
}));
this.updateModel();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,9 @@ class InlineCompletionsSession extends BaseGhostTextWidgetModel {
}

public scheduleAutomaticUpdate(): void {
// Since updateSoon debounces, starvation can happen.
// To prevent stale cache, we clear the current update operation.
this.updateOperation.clear();
this.updateSoon.schedule();
}

Expand Down Expand Up @@ -366,6 +369,8 @@ class InlineCompletionsSession extends BaseGhostTextWidgetModel {
cache?.dispose();
})
.then(undefined, onUnexpectedExternalError);
} else {
cache?.dispose();
}

this.onDidChangeEmitter.fire();
Expand Down Expand Up @@ -475,15 +480,21 @@ export function inlineCompletionToGhostText(inlineCompletion: NormalizedInlineCo
// "\t\tfoo" -> "\t\t\tfoobar" (+"\t", +"bar")
// "\t\tfoo" -> "\tfoobar" (-"\t", +"\bar")

const firstNonWsCol = textModel.getLineFirstNonWhitespaceColumn(inlineCompletion.range.startLineNumber);

if (inlineCompletion.text.startsWith(valueToBeReplaced)) {
remainingInsertText = inlineCompletion.text.substr(valueToBeReplaced.length);
} else {
} else if (firstNonWsCol === 0 || inlineCompletion.range.startColumn < firstNonWsCol) {
// Only allow ignoring leading whitespace in indentation.
// This prevents flickering when the user types whitespace that extends an empty range.
const valueToBeReplacedTrimmed = leftTrim(valueToBeReplaced);
const insertTextTrimmed = leftTrim(inlineCompletion.text);
if (!insertTextTrimmed.startsWith(valueToBeReplacedTrimmed)) {
return undefined;
}
remainingInsertText = insertTextTrimmed.substr(valueToBeReplacedTrimmed.length);
} else {
return undefined;
}

const position = inlineCompletion.range.getEndPosition();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { RunOnceScheduler } from 'vs/base/common/async';
import { Event } from 'vs/base/common/event';
import { toDisposable } from 'vs/base/common/lifecycle';
import { IActiveCodeEditor } from 'vs/editor/browser/editorBrowser';
Expand All @@ -20,10 +21,21 @@ import { ISelectedSuggestion } from 'vs/editor/contrib/suggest/suggestWidget';
export class SuggestWidgetAdapterModel extends BaseGhostTextWidgetModel {
private isSuggestWidgetVisible: boolean = false;
private currentGhostText: GhostText | undefined = undefined;
private _isActive: boolean = false;

public override minReservedLineCount: number = 0;

public get isActive() { return this.isSuggestWidgetVisible; }
public get isActive() { return this._isActive; }

// This delay fixes an suggest widget issue when typing "." immediately restarts the suggestion session.
private setInactiveDelayed = this._register(new RunOnceScheduler(() => {
if (!this.isSuggestWidgetVisible) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only reset active if the suggest widget is not visible anymore (i.e. if it became invisible and then visibile again in the meantime).

if (this.isActive) {
this._isActive = false;
this.onDidChangeEmitter.fire();
}
}
}, 100));

constructor(
editor: IActiveCodeEditor
Expand All @@ -41,15 +53,18 @@ export class SuggestWidgetAdapterModel extends BaseGhostTextWidgetModel {

this._register(suggestController.widget.value.onDidShow(() => {
this.isSuggestWidgetVisible = true;
this._isActive = true;
this.updateFromSuggestion();
}));
this._register(suggestController.widget.value.onDidHide(() => {
this.isSuggestWidgetVisible = false;
this.setInactiveDelayed.schedule();
this.minReservedLineCount = 0;
this.updateFromSuggestion();
}));
this._register(suggestController.widget.value.onDidFocus(() => {
this.isSuggestWidgetVisible = true;
this._isActive = true;
this.updateFromSuggestion();
}));
};
Expand Down