Skip to content

Commit 129908a

Browse files
luoxuanzaoQoder-AI
andcommitted
refactor(composer): keep selection and chip in plain live sync
Drop the consumed-signature bookkeeping: a visible selection always shows its chip, clicking a chip restores the highlight and therefore the chip. Sending a turn collapses the editor selection so the chip leaves with the message instead of snapping back into the cleared composer. Co-authored-by: QoderAI (Qwen 3.8 Max) <qoder_ai@qoder.com>
1 parent 46edd82 commit 129908a

3 files changed

Lines changed: 31 additions & 49 deletions

File tree

‎src/features/chat/controllers/input-controller.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,12 @@ export class InputController {
290290
});
291291
const { displayContent, turnRequest } = turnSubmission;
292292

293+
// The selection travelled with this turn; collapse it so the live
294+
// selection<->chip sync does not put it straight back into the composer.
295+
if (shouldUseInput && options?.editorContextOverride === undefined) {
296+
selectionController?.releaseSelectionAfterSend();
297+
}
298+
293299
const userMsg: ChatMessage = {
294300
id: this.deps.generateId(),
295301
role: 'user',

‎src/features/chat/controllers/selection-controller.ts‎

Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ export class SelectionController {
3333
private focusScopeEls: HTMLElement[];
3434
private storedSelection: StoredSelection | null = null;
3535
private storedToken: string | null = null;
36-
/** Signature of a selection whose chip was sent or removed; never auto-appended again. */
37-
private consumedSignature: string | null = null;
3836
private syncingToken = false;
3937
private inputHandoffGraceUntil: number | null = null;
4038
private pollInterval: number | null = null;
@@ -133,13 +131,6 @@ export class SelectionController {
133131
const notePath = view.file?.path || 'unknown';
134132
const lineCount = selectedText.split(/\r?\n/).length;
135133

136-
if (
137-
!this.storedSelection
138-
&& this.consumedSignature === this.selectionSignature({ notePath, from, to, selectedText })
139-
) {
140-
return;
141-
}
142-
143134
const s = this.storedSelection;
144135
const sameRange = s
145136
&& s.editorView === editorView
@@ -189,13 +180,6 @@ export class SelectionController {
189180
const lineCount = selectedText.split(/\r?\n/).length;
190181
const domRanges = this.cloneDOMRanges(selection);
191182

192-
if (
193-
!this.storedSelection
194-
&& this.consumedSignature === this.selectionSignature({ notePath, selectedText })
195-
) {
196-
return;
197-
}
198-
199183
const unchanged = this.storedSelection
200184
&& this.storedSelection.editorView === undefined
201185
&& this.storedSelection.notePath === notePath
@@ -403,17 +387,6 @@ export class SelectionController {
403387
// Token Sync
404388
// ============================================
405389

406-
/** Identifies a selection so a sent/removed one is not re-appended while it persists. */
407-
private selectionSignature(sel: {
408-
notePath: string;
409-
from?: number;
410-
to?: number;
411-
selectedText: string;
412-
}): string {
413-
const range = sel.from === undefined ? 'dom' : `${sel.from}-${sel.to}`;
414-
return `${sel.notePath}:${range}:${sel.selectedText}`;
415-
}
416-
417390
private currentToken(): { token: string; reference: SelectionTokenReference } | null {
418391
const sel = this.storedSelection;
419392
if (!sel) return null;
@@ -441,10 +414,6 @@ export class SelectionController {
441414
const nextToken = next?.token ?? null;
442415
if (nextToken === this.storedToken) return;
443416

444-
if (nextToken && this.storedSelection) {
445-
// A changed selection is a fresh intent; forget any consumed marker.
446-
this.consumedSignature = null;
447-
}
448417
this.syncingToken = true;
449418
try {
450419
const previous = this.storedToken;
@@ -469,11 +438,6 @@ export class SelectionController {
469438
if (this.storedToken) {
470439
if (!value.includes(this.storedToken)) {
471440
const stale = this.storedToken;
472-
// The chip left the text (sent or deleted): remember the selection so
473-
// the still-active editor highlight does not immediately re-append it.
474-
if (this.storedSelection) {
475-
this.consumedSignature = this.selectionSignature(this.storedSelection);
476-
}
477441
this.storedToken = null;
478442
this.storedSelection = null;
479443
this.tokenSink.unregister(stale);
@@ -515,11 +479,25 @@ export class SelectionController {
515479
// Clear
516480
// ============================================
517481

482+
/**
483+
* Collapses the editor selection after a turn is sent. The chip travels with
484+
* the message, and without this the still-active highlight would immediately
485+
* re-append it to the now-empty composer.
486+
*/
487+
releaseSelectionAfterSend(): void {
488+
const sel = this.storedSelection;
489+
if (sel?.editorView) {
490+
sel.editorView.dispatch({ selection: { anchor: sel.from ?? 0 } });
491+
} else if (sel) {
492+
this.getFocusScopeOwnerDocument()?.getSelection()?.removeAllRanges();
493+
}
494+
this.clear();
495+
}
496+
518497
clear(): void {
519498
this.inputHandoffGraceUntil = null;
520499
this.clearHighlight();
521500
this.storedSelection = null;
522-
this.consumedSignature = null;
523501
this.syncTokenFromStored();
524502
}
525503
}

‎tests/unit/features/chat/controllers/selection-controller.test.ts‎

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -169,35 +169,33 @@ describe('SelectionController', () => {
169169
expect(tokenSink.unregister).toHaveBeenCalledWith(SOURCE_TOKEN);
170170
});
171171

172-
it('does not re-append a selection whose chip left the input while the highlight persists', () => {
172+
it('collapses the editor selection on send so the chip does not come back', () => {
173173
controller.start();
174174
jest.advanceTimersByTime(250);
175175
expect(inputEl.value).toBe(`${SOURCE_TOKEN} `);
176176

177-
// Simulates sending: the composer clears while the editor selection stays.
178-
inputEl.value = '';
179-
inputEl.dispatchEvent(new Event('input', { bubbles: true }));
177+
controller.releaseSelectionAfterSend();
178+
expect(editorView.dispatch).toHaveBeenCalledWith({ selection: { anchor: 0 } });
179+
180+
editor.getSelection.mockReturnValue('');
180181
jest.advanceTimersByTime(750);
181182

182183
expect(inputEl.value).toBe('');
183184
expect(controller.hasSelection()).toBe(false);
184185
});
185186

186-
it('keeps suppressing a consumed range across deselect and identical re-select', () => {
187+
it('re-appends when the same range is selected again after a send', () => {
187188
controller.start();
188189
jest.advanceTimersByTime(250);
189-
inputEl.value = '';
190-
inputEl.dispatchEvent(new Event('input', { bubbles: true }));
190+
controller.releaseSelectionAfterSend();
191+
editor.getSelection.mockReturnValue('');
191192
jest.advanceTimersByTime(250);
192193
expect(inputEl.value).toBe('');
193194

194-
editor.getSelection.mockReturnValue('');
195-
jest.advanceTimersByTime(500);
196195
editor.getSelection.mockReturnValue('selected text');
197-
jest.advanceTimersByTime(500);
196+
jest.advanceTimersByTime(250);
198197

199-
expect(inputEl.value).toBe('');
200-
expect(controller.hasSelection()).toBe(false);
198+
expect(inputEl.value).toBe(`${SOURCE_TOKEN} `);
201199
});
202200

203201
it('appends again when the selection range changes after being consumed', () => {

0 commit comments

Comments
 (0)