Skip to content
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 @@ -53,6 +53,7 @@ export class SuggestionEntrySession {
private readonly insertSpaceAfterAutocomplete: boolean;
private readonly logRenderedSuggestionPopup: SuggestionEntrySessionOptions["logRenderedSuggestionPopup"];
private readonly logNoVisibleSuggestions: (context: PredictionResponse) => void;
private lastAcceptedSuggestion: string | null = null;

constructor(options: SuggestionEntrySessionOptions) {
this.entry = options.entry;
Expand Down Expand Up @@ -1188,12 +1189,21 @@ export class SuggestionEntrySession {
}

private acceptSuggestionInternal(suggestion: string): boolean {
if (
this.lastAcceptedSuggestion === suggestion &&
this.entry.suppressNextSuggestionInputPrediction &&
this.entry.pendingExtensionEdit?.source === "suggestion"
) {
return false;
}

this.entry.suppressNextSuggestionInputPrediction = true;
const accepted = this.textEditService.acceptSuggestion(this.entry, suggestion);
if (!accepted) {
this.entry.suppressNextSuggestionInputPrediction = false;
return false;
}
this.lastAcceptedSuggestion = suggestion;
this.finishAcceptedSuggestion(
accepted.triggerText,
accepted.insertedText,
Expand Down Expand Up @@ -1292,6 +1302,7 @@ export class SuggestionEntrySession {
}

private clearAcceptedSuggestionTransientState(): void {
this.lastAcceptedSuggestion = null;
this.clearPendingExtensionEdit();
this.entry.missingTrailingSpace = false;
this.entry.expectedCursorPos = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1071,29 +1071,7 @@ export class SuggestionTextEditService {
cursorAfter,
{ scopeRoot: activeBlock },
);
const deferredHostNoMutation =
"appliedBy" in initialApplyResult &&
initialApplyResult.appliedBy === "host-beforeinput" &&
!initialApplyResult.didMutateDom;

if (!deferredHostNoMutation) {
return initialApplyResult;
}

const domFallbackResult = this.replaceTextByOffsets(
elem,
blockSourceText,
replaceStart,
replaceEnd,
replacementText,
cursorAfter,
{
preferDomMutation: true,
scopeRoot: activeBlock,
},
);

return domFallbackResult.didMutateDom ? domFallbackResult : initialApplyResult;
return initialApplyResult;
}

private acceptContentEditableSuggestion(
Expand Down
43 changes: 43 additions & 0 deletions tests/SuggestionEntrySession.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,49 @@ test("session ignores stale prediction responses after suggestion acceptance", (
expect(renderMenu).not.toHaveBeenCalled();
});

test("session ignores an immediate duplicate suggestion accept while the first accepted edit is still pending", () => {
const editable = document.createElement("div");
editable.setAttribute("contenteditable", "true");
Object.defineProperty(editable, "isContentEditable", { value: true, configurable: true });

const entry = createSuggestionEntry({
elem: editable as SuggestionEntry["elem"],
requestId: 2,
suggestions: ["beta"],
latestMentionText: "bet",
});
const textEditService = {
acceptSuggestion: jest.fn(() => {
entry.pendingExtensionEdit = {
replaceStart: 0,
originalText: "bet",
replacementText: "beta",
cursorBefore: 3,
cursorAfter: 4,
postEditFingerprint: {
fullText: "beta",
cursorOffset: 4,
selectionCollapsed: true,
},
source: "suggestion",
};
return {
triggerText: "bet",
insertedText: "beta",
cursorAfter: 4,
cursorAfterIsBlockLocal: false,
};
}),
applyGrammarEdit: jest.fn(() => ({ applied: false, didDispatchInput: false })),
syncManualAutoFixSuppression: jest.fn(),
};
const session = makeSession({ entry, textEditService });

expect(session.acceptSuggestionAtIndex(0)).toBe(true);
expect(session.acceptSuggestionAtIndex(0)).toBe(false);
expect(textEditService.acceptSuggestion).toHaveBeenCalledTimes(1);
});

test("session suppresses the synthetic input emitted by accepted suggestions", () => {
const clearPendingFallback = jest.fn();
const predictionCoordinator = {
Expand Down
4 changes: 2 additions & 2 deletions tests/SuggestionTextEditService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1264,7 +1264,7 @@ describe("SuggestionTextEditService", () => {
expect(editable.textContent).toBe("Wh");
});

test("retries generic contenteditable acceptance with direct DOM mutation after a canceled no-op beforeinput", () => {
test("keeps generic contenteditable acceptance deferred when beforeinput is canceled without immediate DOM mutation", () => {
const service = new SuggestionTextEditService({
findMentionToken,
isSeparator: (value) => /\s/.test(value),
Expand Down Expand Up @@ -1297,7 +1297,7 @@ describe("SuggestionTextEditService", () => {
cursorAfter: 5,
cursorAfterIsBlockLocal: true,
});
expect(editable.textContent).toBe("What\u00A0");
expect(editable.textContent).toBe("Wh");
expect(entry.pendingExtensionEdit?.awaitingHostInputEcho).toBe(true);
});

Expand Down
Loading