Skip to content

Commit 05302ca

Browse files
matt2eclaude
andcommitted
fix(desktop): keep the draft space when typing right after a mention pick
Desktop Smoke E2E (2) failed on mentions.spec.ts ("typing a mention before existing text does not interleave spaces"): picking a suggestion mid-draft and typing immediately could swallow the draft's space ("hello @bob abcworld"). Chromium sometimes rewrites the whole whitespace run around the caret as replace(" " -> "\u00A0a"), and the settling-time text-input redirect only recognized a caret or a selected trailing space, so the rewrite fell through to the destructive default replacement. Teach the redirect to recognize a space-run rewrite (space or NBSP) and insert only the typed remainder after the mention's trailing space. The race pre-dates this branch (reproduces on main at ~5-25% per local run); 300+ consecutive passes after the fix. Co-Authored-By: Claude Fable 5 <[email protected]> Signed-off-by: Matt Toohey <[email protected]>
1 parent f5506a0 commit 05302ca

2 files changed

Lines changed: 81 additions & 31 deletions

File tree

‎desktop/src/features/messages/lib/mentionHighlightExtension.test.mjs‎

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import {
1010
buildHighlightPatterns,
1111
createMentionCaretSettlement,
1212
findHighlightMatches,
13-
insertPosForMentionTextInput,
13+
insertionForMentionTextInput,
1414
MentionHighlightExtension,
15-
mentionTextInputInsertPos,
15+
mentionTextInputInsertion,
1616
positionAfterArrowLeftThroughMentionSpace,
1717
selectionAfterMentionTrailingSpace,
1818
shouldAdvanceMentionCaret,
@@ -252,35 +252,60 @@ test("createMentionCaretSettlement keeps two editors independent", () => {
252252
assert.equal(composerB.peek(), 12);
253253
});
254254

255-
test("insertPosForMentionTextInput redirects a caret at the chip edge", () => {
255+
test("insertionForMentionTextInput redirects a caret at the chip edge", () => {
256256
const doc = document(paragraph(text("@quinn ")));
257257
const spacePos = 1 + "@quinn".length;
258+
assert.deepEqual(insertionForMentionTextInput(doc, spacePos, spacePos, "x"), {
259+
insertAt: spacePos + 1,
260+
text: "x",
261+
});
258262
assert.equal(
259-
insertPosForMentionTextInput(doc, spacePos, spacePos),
260-
spacePos + 1,
261-
);
262-
assert.equal(
263-
insertPosForMentionTextInput(doc, spacePos + 1, spacePos + 1),
263+
insertionForMentionTextInput(doc, spacePos + 1, spacePos + 1, "x"),
264264
null,
265265
);
266266
});
267267

268-
test("insertPosForMentionTextInput keeps a selected trailing space", () => {
268+
test("insertionForMentionTextInput keeps a selected trailing space", () => {
269269
const doc = document(paragraph(text("@quinn ")));
270270
const spacePos = 1 + "@quinn".length;
271+
assert.deepEqual(
272+
insertionForMentionTextInput(doc, spacePos, spacePos + 1, "x"),
273+
{ insertAt: spacePos + 1, text: "x" },
274+
);
275+
});
276+
277+
test("insertionForMentionTextInput keeps the draft space in a whitespace-run rewrite", () => {
278+
// Chromium can rewrite the whole space run when typing between the
279+
// mention's trailing space and a pre-existing draft space, emitting
280+
// replace(" " → " a") — usually with a non-breaking space. The draft's
281+
// space must survive either way.
282+
const doc = document(paragraph(text("hello @bob world")));
283+
const spacePos = 1 + "hello @bob".length;
284+
assert.deepEqual(
285+
insertionForMentionTextInput(doc, spacePos, spacePos + 2, " a"),
286+
{ insertAt: spacePos + 1, text: "a" },
287+
);
288+
assert.deepEqual(
289+
insertionForMentionTextInput(doc, spacePos, spacePos + 2, "\u00A0a"),
290+
{ insertAt: spacePos + 1, text: "a" },
291+
);
292+
// A rewrite that is not space-led is a real replacement — leave it alone.
271293
assert.equal(
272-
insertPosForMentionTextInput(doc, spacePos, spacePos + 1),
273-
spacePos + 1,
294+
insertionForMentionTextInput(doc, spacePos, spacePos + 2, "x"),
295+
null,
274296
);
275297
});
276298

277-
test("mentionTextInputInsertPos honors a deliberate caret after settlement", () => {
299+
test("mentionTextInputInsertion honors a deliberate caret after settlement", () => {
278300
const doc = document(paragraph(text("@bob ")));
279301
const spacePos = 1 + "@bob".length;
280-
assert.equal(mentionTextInputInsertPos(doc, spacePos, spacePos, false), null);
281302
assert.equal(
282-
mentionTextInputInsertPos(doc, spacePos, spacePos, true),
283-
spacePos + 1,
303+
mentionTextInputInsertion(doc, spacePos, spacePos, "x", false),
304+
null,
305+
);
306+
assert.deepEqual(
307+
mentionTextInputInsertion(doc, spacePos, spacePos, "x", true),
308+
{ insertAt: spacePos + 1, text: "x" },
284309
);
285310
});
286311

‎desktop/src/features/messages/lib/mentionHighlightExtension.ts‎

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,23 +58,43 @@ export function shouldAdvanceMentionCaret({
5858
return next !== from && settling;
5959
}
6060

61+
export type MentionTextInsertion = {
62+
insertAt: number;
63+
text: string;
64+
};
65+
6166
/**
62-
* Where to insert typed text when the caret (or a one-character selection)
63-
* sits on the trailing space after an `@name` / `#channel` token.
64-
* A selected trailing space would otherwise be replaced, producing
65-
* `@bobhello`.
67+
* Where (and what) to insert when typed text arrives at the trailing space
68+
* after an `@name` / `#channel` token.
69+
*
70+
* - Caret on the space: insert after it, so the next keystroke lands after
71+
* the token (`@bobhello` fix).
72+
* - One-character selection of the space: insert after it — replacing the
73+
* selected space would produce `@bobhello`.
74+
* - Whitespace-run rewrite: when typing between the mention's trailing
75+
* space and a pre-existing draft space, Chromium may re-emit the whole
76+
* run as `replace(" " → " a")` — usually with a non-breaking space.
77+
* Applying that verbatim deletes the draft's space
78+
* (`hello @bob abcworld`), so keep the document's spaces and insert only
79+
* the typed remainder after the trailing space.
6680
*/
67-
export function insertPosForMentionTextInput(
81+
export function insertionForMentionTextInput(
6882
doc: ProseMirrorNode,
6983
from: number,
7084
to: number,
71-
): number | null {
85+
text: string,
86+
): MentionTextInsertion | null {
7287
const next = selectionAfterMentionTrailingSpace(doc, from);
7388
if (from === to) {
74-
return next === from ? null : next;
89+
return next === from ? null : { insertAt: next, text };
7590
}
76-
if (to === next && next === from + 1) {
77-
return next;
91+
if (next !== from + 1) return null;
92+
if (to === next) {
93+
return { insertAt: next, text };
94+
}
95+
const replaced = doc.textBetween(from, to, "\n", "\0");
96+
if (/^[ \u00A0]+$/.test(replaced) && /^[ \u00A0]/.test(text)) {
97+
return { insertAt: next, text: text.replace(/^[ \u00A0]+/, "") };
7898
}
7999
return null;
80100
}
@@ -84,14 +104,15 @@ export function insertPosForMentionTextInput(
84104
* deliberate ArrowLeft or chip click, honor the caret so `x` lands in the
85105
* token (`@bobx`) instead of after the space (`@bob x`).
86106
*/
87-
export function mentionTextInputInsertPos(
107+
export function mentionTextInputInsertion(
88108
doc: ProseMirrorNode,
89109
from: number,
90110
to: number,
111+
text: string,
91112
settling: boolean,
92-
): number | null {
113+
): MentionTextInsertion | null {
93114
if (!settling) return null;
94-
return insertPosForMentionTextInput(doc, from, to);
115+
return insertionForMentionTextInput(doc, from, to, text);
95116
}
96117

97118
/** Caret just after a mention trailing space: ArrowLeft lands on the token end. */
@@ -396,18 +417,22 @@ export const MentionHighlightExtension = Extension.create({
396417
return this.getState(state) ?? DecorationSet.empty;
397418
},
398419
handleTextInput(view, from, to, text) {
399-
const insertAt = mentionTextInputInsertPos(
420+
const insertion = mentionTextInputInsertion(
400421
view.state.doc,
401422
from,
402423
to,
424+
text,
403425
settlement.peek() !== null,
404426
);
405-
if (insertAt == null) {
427+
if (insertion == null) {
406428
settlement.cancel();
407429
return false;
408430
}
409-
const tr = view.state.tr.insertText(text, insertAt);
410-
const caret = tr.mapping.map(insertAt, 1);
431+
const tr = view.state.tr.insertText(
432+
insertion.text,
433+
insertion.insertAt,
434+
);
435+
const caret = tr.mapping.map(insertion.insertAt, 1);
411436
tr.setSelection(TextSelection.create(tr.doc, caret));
412437
view.dispatch(tr);
413438
settlement.cancel();

0 commit comments

Comments
 (0)