Implements #137004. When an inline completion provider provides a suited augmented inline completion, a preview will be shown even if editor.suggest.preview is set to false.

This commit is contained in:
Henning Dieterichs 2021-11-23 12:05:37 +01:00
parent a1c3ece798
commit 52dbedd034
No known key found for this signature in database
GPG key ID: 771381EFFDB9EC06
2 changed files with 32 additions and 33 deletions

View file

@ -51,7 +51,7 @@ export class SuggestWidgetPreviewModel extends BaseGhostTextWidgetModel {
this.minReservedLineCount = Math.max(this.minReservedLineCount, sum(newGhostText.parts.map(p => p.lines.length - 1)));
}
if (this.minReservedLineCount >= 1 && this.isSuggestionPreviewEnabled()) {
if (this.minReservedLineCount >= 1) {
this.suggestionInlineCompletionSource.forceRenderingAbove();
} else {
this.suggestionInlineCompletionSource.stopForceRenderingAbove();
@ -64,11 +64,9 @@ export class SuggestWidgetPreviewModel extends BaseGhostTextWidgetModel {
}));
this._register(this.editor.onDidChangeCursorPosition((e) => {
if (this.isSuggestionPreviewEnabled()) {
this.minReservedLineCount = 0;
this.updateCacheSoon.schedule();
this.onDidChangeEmitter.fire();
}
this.minReservedLineCount = 0;
this.updateCacheSoon.schedule();
this.onDidChangeEmitter.fire();
}));
this._register(toDisposable(() => this.suggestionInlineCompletionSource.stopForceRenderingAbove()));
@ -123,39 +121,40 @@ export class SuggestWidgetPreviewModel extends BaseGhostTextWidgetModel {
}
public override get ghostText(): GhostText | undefined {
if (!this.isSuggestionPreviewEnabled()) {
const isSuggestionPreviewEnabled = this.isSuggestionPreviewEnabled();
const augmentedCompletion = minimizeInlineCompletion(this.editor.getModel()!, this.cache.value?.completions[0]?.toLiveInlineCompletion());
const suggestWidgetState = this.suggestionInlineCompletionSource.state;
const suggestInlineCompletion = minimizeInlineCompletion(this.editor.getModel()!, suggestWidgetState?.selectedItemAsInlineCompletion);
const isAugmentedCompletionValid = augmentedCompletion
&& suggestInlineCompletion
&& augmentedCompletion.text.startsWith(suggestInlineCompletion.text)
&& augmentedCompletion.range.equalsRange(suggestInlineCompletion.range);
if (!isSuggestionPreviewEnabled && !isAugmentedCompletionValid) {
return undefined;
}
const suggestWidgetState = this.suggestionInlineCompletionSource.state;
// If the augmented completion is not valid and there is no suggest inline completion, we still show the augmented completion.
const finalCompletion = isAugmentedCompletionValid ? augmentedCompletion : (suggestInlineCompletion || augmentedCompletion);
const originalInlineCompletion = minimizeInlineCompletion(this.editor.getModel()!, suggestWidgetState?.selectedItemAsInlineCompletion);
const augmentedCompletion = minimizeInlineCompletion(this.editor.getModel()!, this.cache.value?.completions[0]?.toLiveInlineCompletion());
const finalCompletion =
augmentedCompletion
&& originalInlineCompletion
&& augmentedCompletion.text.startsWith(originalInlineCompletion.text)
&& augmentedCompletion.range.equalsRange(originalInlineCompletion.range)
? augmentedCompletion : (originalInlineCompletion || augmentedCompletion);
const inlineCompletionPreviewLength = originalInlineCompletion ? (finalCompletion?.text.length || 0) - (originalInlineCompletion.text.length) : 0;
const toGhostText = (completion: NormalizedInlineCompletion | undefined): GhostText | undefined => {
const mode = this.editor.getOptions().get(EditorOption.suggest).previewMode;
return completion
? (
inlineCompletionToGhostText(completion, this.editor.getModel(), mode, this.editor.getPosition(), inlineCompletionPreviewLength) ||
// Show an invisible ghost text to reserve space
new GhostText(completion.range.endLineNumber, [], this.minReservedLineCount)
)
: undefined;
};
const newGhostText = toGhostText(finalCompletion);
const inlineCompletionPreviewLength = isAugmentedCompletionValid ? finalCompletion!.text.length - suggestInlineCompletion.text.length : 0;
const newGhostText = this.toGhostText(finalCompletion, inlineCompletionPreviewLength);
return newGhostText;
}
private toGhostText(completion: NormalizedInlineCompletion | undefined, inlineCompletionPreviewLength: number): GhostText | undefined {
const mode = this.editor.getOptions().get(EditorOption.suggest).previewMode;
return completion
? (
inlineCompletionToGhostText(completion, this.editor.getModel(), mode, this.editor.getPosition(), inlineCompletionPreviewLength) ||
// Show an invisible ghost text to reserve space
new GhostText(completion.range.endLineNumber, [], this.minReservedLineCount)
)
: undefined;
}
}
function sum(arr: number[]): number {

View file

@ -63,7 +63,7 @@ suite('Suggest Widget Model', () => {
const suggestController = (editor.getContribution(SuggestController.ID) as SuggestController);
suggestController.triggerSuggest();
await timeout(1000);
assert.deepStrictEqual(history.splice(0), [true]);
assert.deepStrictEqual(history.splice(0), [false, true]);
context.keyboardType('.');
await timeout(1000);