Code cleanup

This commit is contained in:
Henning Dieterichs 2024-02-09 12:19:42 +01:00 committed by Henning Dieterichs
parent 808582dad0
commit c4455a4d5c

View file

@ -194,11 +194,11 @@ export class InlineCompletionsModel extends Disposable {
});
public readonly state = derivedOpts<{
suggestItem: SuggestItemInfo | undefined;
inlineCompletion: InlineCompletionWithUpdatedRange | undefined;
edits: readonly SingleTextEdit[];
primaryGhostText: GhostTextOrReplacement;
ghostTexts: readonly GhostTextOrReplacement[];
edits: SingleTextEdit[];
suggestItem: SuggestItemInfo | undefined;
inlineCompletion: InlineCompletionWithUpdatedRange | undefined;
} | undefined>({
owner: this,
equalityComparer: (a, b) => {
@ -212,29 +212,29 @@ export class InlineCompletionsModel extends Disposable {
const suggestItem = this.selectedSuggestItem.read(reader);
if (suggestItem) {
const suggestCompletion = suggestItem.toSingleTextEdit().removeCommonPrefix(model);
const augmentedCompletion = this._computeAugmentedCompletion(suggestCompletion, reader);
const suggestCompletionEdit = suggestItem.toSingleTextEdit().removeCommonPrefix(model);
const augmentation = this._computeAugmentation(suggestCompletionEdit, reader);
const isSuggestionPreviewEnabled = this._suggestPreviewEnabled.read(reader);
if (!isSuggestionPreviewEnabled && !augmentedCompletion) { return undefined; }
if (!isSuggestionPreviewEnabled && !augmentation) { return undefined; }
const edit = augmentedCompletion?.edit ?? suggestCompletion;
const editPreviewLength = augmentedCompletion ? augmentedCompletion.edit.text.length - suggestCompletion.text.length : 0;
const fullEdit = augmentation?.edit ?? suggestCompletionEdit;
const fullEditPreviewLength = augmentation ? augmentation.edit.text.length - suggestCompletionEdit.text.length : 0;
const mode = this._suggestPreviewMode.read(reader);
const positions = this._positions.read(reader);
const edits = [edit, ...this._getSecondaryEdits(this.textModel, positions, edit)];
const edits = [fullEdit, ...this._getSecondaryEdits(this.textModel, positions, fullEdit)];
const ghostTexts = edits
.map((edit, idx) => edit.computeGhostText(model, mode, positions[idx], editPreviewLength))
.map((edit, idx) => edit.computeGhostText(model, mode, positions[idx], fullEditPreviewLength))
.filter(isDefined);
const primaryGhostText = ghostTexts[0] ?? new GhostText(edit.range.endLineNumber, []);
return { ghostTexts, primaryGhostText, inlineCompletion: augmentedCompletion?.completion, suggestItem, edits };
const primaryGhostText = ghostTexts[0] ?? new GhostText(fullEdit.range.endLineNumber, []);
return { edits, primaryGhostText, ghostTexts, inlineCompletion: augmentation?.completion, suggestItem };
} else {
if (!this._isActive.read(reader)) { return undefined; }
const item = this.selectedInlineCompletion.read(reader);
if (!item) { return undefined; }
const inlineCompletion = this.selectedInlineCompletion.read(reader);
if (!inlineCompletion) { return undefined; }
const replacement = item.toSingleTextEdit(reader);
const replacement = inlineCompletion.toSingleTextEdit(reader);
const mode = this._inlineSuggestMode.read(reader);
const positions = this._positions.read(reader);
const edits = [replacement, ...this._getSecondaryEdits(this.textModel, positions, replacement)];
@ -242,11 +242,11 @@ export class InlineCompletionsModel extends Disposable {
.map((edit, idx) => edit.computeGhostText(model, mode, positions[idx], 0))
.filter(isDefined);
if (!ghostTexts[0]) { return undefined; }
return { ghostTexts, primaryGhostText: ghostTexts[0], inlineCompletion: item, suggestItem: undefined, edits };
return { edits, primaryGhostText: ghostTexts[0], ghostTexts, inlineCompletion, suggestItem: undefined };
}
});
private _computeAugmentedCompletion(suggestCompletion: SingleTextEdit, reader: IReader | undefined) {
private _computeAugmentation(suggestCompletion: SingleTextEdit, reader: IReader | undefined) {
const model = this.textModel;
const suggestWidgetInlineCompletions = this._source.suggestWidgetInlineCompletions.read(reader);
const candidateInlineCompletions = suggestWidgetInlineCompletions
@ -256,7 +256,7 @@ export class InlineCompletionsModel extends Disposable {
const augmentedCompletion = mapFindFirst(candidateInlineCompletions, completion => {
let r = completion.toSingleTextEdit(reader);
r = r.removeCommonPrefix(model, Range.fromPositions(r.range.getStartPosition(), suggestCompletion.range.getEndPosition()));
return r.augments(suggestCompletion) ? { edit: r, completion } : undefined;
return r.augments(suggestCompletion) ? { completion, edit: r } : undefined;
});
return augmentedCompletion;
@ -411,16 +411,17 @@ export class InlineCompletionsModel extends Disposable {
}
const firstPart = ghostText.parts[0];
const position = new Position(ghostText.lineNumber, firstPart.column);
const text = firstPart.text;
const acceptUntilIndexExclusive = getAcceptUntilIndex(position, text);
if (acceptUntilIndexExclusive === text.length && ghostText.parts.length === 1) {
const ghostTextPos = new Position(ghostText.lineNumber, firstPart.column);
const ghostTextVal = firstPart.text;
const acceptUntilIndexExclusive = getAcceptUntilIndex(ghostTextPos, ghostTextVal);
if (acceptUntilIndexExclusive === ghostTextVal.length && ghostText.parts.length === 1) {
this.accept(editor);
return;
}
const partialGhostTextVal = ghostTextVal.substring(0, acceptUntilIndexExclusive);
const partialText = text.substring(0, acceptUntilIndexExclusive);
const positions = this._positions.get();
const cursorPosition = positions[0];
// Executing the edit might free the completion, so we have to hold a reference on it.
completion.source.addRef();
@ -428,13 +429,10 @@ export class InlineCompletionsModel extends Disposable {
this._isAcceptingPartially = true;
try {
editor.pushUndoStop();
const replaceRange = Range.fromPositions(completion.range.getStartPosition(), position);
const newText = completion.insertText.substring(
0,
firstPart.column - completion.range.startColumn + acceptUntilIndexExclusive);
const singleTextEdit = new SingleTextEdit(replaceRange, newText);
const positions = this._positions.get();
const edits = [singleTextEdit, ...this._getSecondaryEdits(this.textModel, positions, singleTextEdit)];
const replaceRange = Range.fromPositions(cursorPosition, ghostTextPos);
const newText = editor.getModel()!.getValueInRange(replaceRange) + partialGhostTextVal;
const primaryEdit = new SingleTextEdit(replaceRange, newText);
const edits = [primaryEdit, ...this._getSecondaryEdits(this.textModel, positions, primaryEdit)];
const selections = getEndPositionsAfterApplying(edits).map(p => Selection.fromPositions(p));
editor.executeEdits('inlineSuggestion.accept', edits.map(edit => EditOperation.replaceMove(edit.range, edit.text)));
editor.setSelections(selections, 'inlineCompletionPartialAccept');
@ -443,7 +441,7 @@ export class InlineCompletionsModel extends Disposable {
}
if (completion.source.provider.handlePartialAccept) {
const acceptedRange = Range.fromPositions(completion.range.getStartPosition(), addPositions(position, lengthOfText(partialText)));
const acceptedRange = Range.fromPositions(completion.range.getStartPosition(), addPositions(ghostTextPos, lengthOfText(partialGhostTextVal)));
// This assumes that the inline completion and the model use the same EOL style.
const text = editor.getModel()!.getValueInRange(acceptedRange, EndOfLinePreference.LF);
completion.source.provider.handlePartialAccept(
@ -458,7 +456,6 @@ export class InlineCompletionsModel extends Disposable {
}
private _getSecondaryEdits(textModel: ITextModel, positions: readonly Position[], primaryEdit: SingleTextEdit): SingleTextEdit[] {
const primaryPosition = positions[0];
const secondaryPositions = positions.slice(1);
const replacedTextAfterPrimaryCursor = textModel
@ -477,7 +474,7 @@ export class InlineCompletionsModel extends Disposable {
public handleSuggestAccepted(item: SuggestItemInfo) {
const itemEdit = item.toSingleTextEdit().removeCommonPrefix(this.textModel);
const augmentedCompletion = this._computeAugmentedCompletion(itemEdit, undefined);
const augmentedCompletion = this._computeAugmentation(itemEdit, undefined);
if (!augmentedCompletion) { return; }
const inlineCompletion = augmentedCompletion.completion.inlineCompletion;