Skip to content

Fix composer @ completion started in whitespace#1

Closed
mwolson wants to merge 2 commits intot3-mainfrom
feature/fix-composer-mention-completion-cursors
Closed

Fix composer @ completion started in whitespace#1
mwolson wants to merge 2 commits intot3-mainfrom
feature/fix-composer-mention-completion-cursors

Conversation

@mwolson
Copy link
Copy Markdown
Owner

@mwolson mwolson commented Mar 7, 2026

Summary

This fixes the remaining composer @-completion bug described in pingdotgg/t3code#291.

When @ completion is started inside existing whitespace, especially in prompts that already contain mention pills, the composer could mix collapsed mention-aware offsets with expanded text offsets. That caused three visible failures:

  • the completion menu could dismiss while typing even though the query was still valid
  • selecting a completion could leave an extra trailing space
  • the caret could land farther right than expected after insertion

Repro

  1. In the composer, insert a file mention so there is already at least one mention pill in the prompt, for example @AGENTS.md.
  2. Continue typing normal text and leave two spaces somewhere later in the prompt.
  3. Move the cursor into that double-space gap so there is one space before the cursor and one space at the cursor position.
  4. Type @.
  5. Type one or two more characters so the menu filters to matching results.
  6. Select a completion.

Root Cause

The composer still had a few paths that were not consistent about cursor coordinate systems:

  • mention-aware composer state uses collapsed offsets, where a rendered mention pill counts as length 1
  • Lexical selection state and raw prompt text use expanded offsets, where @path/to/file counts by its full text length

That mismatch showed up in trigger detection, cursor restoration, prompt replacement, and a few state resets. In the whitespace-started case, it made the active trigger drift out of sync with what the user was actually editing.

Fix

  • add explicit collapsed-cursor helpers in composer-logic.ts, including clampCollapsedComposerCursor and collapseExpandedComposerCursor
  • keep ComposerPromptEditor snapshots in both coordinate systems so callers can use the right cursor form for the right job
  • normalize ChatView state updates, trigger detection, and cursor restoration around collapsed cursor state plus expanded trigger positions
  • when inserting replacements that already end with a space, consume an existing space at the cursor instead of leaving a doubled trailing space
  • keep focus and caret placement aligned after insertions, retries, pending-question handoff, and other composer state resets

Result

After this change:

  • the completion menu stays open while the typed query still matches results
  • selecting a completion replaces the query cleanly
  • the inserted mention ends with exactly one trailing space
  • the caret lands immediately after that trailing space

Testing

  • bun lint
  • bun typecheck
  • added focused unit coverage for collapsed/expanded cursor conversion and mention-aware clamping in apps/web/src/composer-logic.test.ts

@mwolson mwolson changed the title Fix composer mention completion cursors Fix composer @ completion started in whitespace Mar 7, 2026
@github-actions github-actions bot added the vouch:trusted PR author is trusted by repo permissions or the VOUCHED list. label Mar 9, 2026
@mwolson mwolson force-pushed the feature/fix-composer-mention-completion-cursors branch from 36f72eb to ba598aa Compare March 12, 2026 00:13
@mwolson mwolson force-pushed the feature/fix-composer-mention-completion-cursors branch from ba598aa to 8f7ca4f Compare March 12, 2026 21:31
@mwolson mwolson closed this Mar 13, 2026
@mwolson mwolson deleted the feature/fix-composer-mention-completion-cursors branch March 13, 2026 00:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

vouch:trusted PR author is trusted by repo permissions or the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant