Skip to content

story: Add an application-owned completion menu to the Input story - #3607

Merged
huacnlee merged 4 commits into
longbridge:mainfrom
noosxe:input-completion-example
Oct 22, 2026
Merged

huacnlee merged 4 commits into
longbridge:mainfrom
noosxe:input-completion-example

Conversation

@noosxe

@noosxe noosxe commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Follows up on #3560 and the direction from the #3490 review: instead of adding completion state to InputState, the Input story gains a Completion section, an application-owned completion menu around a single-line Input built only on existing public APIs. No changes to gpui-base or gpui-component.

What it shows

Type $ and a menu of shell-style variables opens. Typing filters it, the keyboard drives it, and accepting replaces the typed $prefix with $NAME . The application owns each part:

  • Candidates, filtering, ranking: a static table; prefix matches before substring matches, each alphabetical.
  • Trigger and dismissal: InputEvent::Change rescans the $-token under the caret ($ must start the text or follow whitespace, so a$b never triggers). Caret movement emits no event, so render repeats the rescan against the live caret.
  • Keyboard ownership: capture_action on the input's wrapper takes MoveUp / MoveDown / Escape / IndentInline (tab) while the menu is open. Each key first checks that the menu still belongs to the token under the caret; a stale menu dismisses and the key goes to the input.
  • Enter: InputEvent::PressEnter accepts with a current menu and submits otherwise.
  • The edit: the token range is revalidated against the current buffer, then set_selected_range + replace swap it in one undo step.

The menu uses popover_style and ListItem rows, the same surface and rows Combobox uses, and anchors to the input region. Applications that need caret anchoring can use cursor_layout() / range_to_bounds().

Files

  • crates/story/src/stories/input_completion.rs: the composition and its tests.
  • crates/story/src/stories/input_story.rs, mod.rs: mount it as the "Completion" section.

Testing

  • cargo clippy -p gpui-component-story --tests -- --deny warnings, cargo fmt --check, typos: clean.
  • cargo test -p gpui-component-story input_completion: 4 tests pass. Two drive the story with keystrokes: select and accept with down / tab / enter, and the review's regression (type echo $HO, press home enter with no edit in between; Enter submits and the text is unchanged). Removing the revalidation makes the regression test fail.
  • Not yet exercised by hand in the gallery; the keystroke tests above drive the mounted story on the test platform.

Refs #3560.

🤖 Generated with Claude Code

Composes the completion interaction that longbridge#3560 asks applications to own,
following the direction from the longbridge#3490 review: InputState carries no
completion state. The example owns the candidate data, filtering and
ranking (static variable table), trigger detection and dismissal (an
InputEvent::Change rescan of the $-token under the caret), keyboard
ownership (capture_action intercepts MoveUp/MoveDown/Escape/IndentInline
while the menu is open), the Enter decision (accept vs. submit), and the
accepted edit (set_selected_range + replace swaps the typed $prefix in
one atomic undo step, revalidated against the current buffer).

The menu anchors to the input region as a deferred overlay; caret
anchoring via cursor_layout()/range_to_bounds() is noted for
applications that need it.

Run with: cargo run -p completion
@noosxe

noosxe commented Oct 19, 2026

Copy link
Copy Markdown
Contributor Author

We looked into the red "Checks" job: it's pre-existing on main — the typos findings are all in release-notes.md from the v0.8.0 release notes (the @UncertaintyDeterminesYou4ndMe / @alex-vas usernames trip the dictionary), reproduced with the pinned typos-cli 1.40.0 and current 1.51.1 on a pristine checkout. Nothing in this PR's files is flagged (typos passes clean on the diff).

Happy to send a small fix for the release-notes baseline if useful — otherwise leaving it to you as a main-side matter.

Copy link
Copy Markdown
Member

The menu is refreshed only on InputEvent::Change, but caret movement only notifies: after typing $HO and moving to the start without editing, the stale menu still captures navigation/Tab and the first Enter dismisses it instead of submitting. Please observe cursor/selection changes as well, revalidate before taking keyboard ownership, and add a regression for leaving the token without changing its text; this finding is from static inspection, not a native reproduction.

For the unrelated Checks failure, a separate narrow typos fix would be useful; please preserve the correct author handles and avoid disabling checks for the whole release-notes file.

Reviewed by ChatGPT on behalf of @huacnlee.

Caret movement notifies without emitting any event, so a menu opened on
a $-token survived Home / arrow-key / click moves that left the token:
it kept capturing navigation and Tab, and the first Enter dismissed it
instead of submitting. Found by review; no native reproduction needed.

The trigger rescan is now ground truth at every moment of authority:

- render re-runs the rescan against the live caret every frame, so a
  menu whose token the caret left is gone that same frame (no notify on
  this path — the frame under construction already reflects it);
- every captured key (MoveUp / MoveDown / Escape / IndentInline) and
  PressEnter revalidate the trigger before acting, so keyboard ownership
  never outlives the token it was granted for — a stale menu dismisses
  and the key falls through to the input (Enter submits).

Regression tests cover leaving the token without changing its text
(Home, whitespace boundaries, mid-token $ that must never trigger) plus
trigger placement and ranking.
@noosxe

noosxe commented Oct 20, 2026

Copy link
Copy Markdown
Contributor Author

Thank you for the careful review — the static finding was correct, and it's fixed in fe2d1ec.

The bug: you're right that caret movement only notifies and never emits an event, so a menu opened on a $-token survived Home / arrow-key / click moves that left the token, kept capturing navigation and Tab, and the first Enter dismissed instead of submitting.

The fix makes the trigger rescan ground truth at every moment of authority, since no selection event exists to observe:

  • render re-runs the same rescan against the live caret every frame, so a menu whose token the caret left is gone that same frame (no cx.notify() on this path — the frame under construction already reflects the decision);
  • every captured key (MoveUp / MoveDown / Escape / IndentInline) and PressEnter revalidates the trigger before acting, so keyboard ownership never outlives the token it was granted for — a stale menu dismisses and the key falls through to the input, with Enter submitting.

Regression tests cover exactly the requested case — leaving the token without changing its text (echo $HO at cursors 0/4/5 vs. 7/8) — plus placement (line start, after whitespace, mid-token $ that must never trigger, whitespace closing the token) and ranking. One note from landing them: use super::* in a test module of an example that globs gpui_kit::* pulls in GPUI's #[test] attribute macro and shadows Rust's built-in one (the compiler recursed into gpui_macros until it died); the kit's own guidance to import test-module types explicitly is now followed and commented.

The typos baseline: separate as requested — #3608 ignores the two exact handle strings via extend-ignore-re, so the handles stay verbatim and the rest of release-notes.md stays checked (verified against the pinned typos-cli 1.40.0, full repo exits 0).

Happy to take another pass on anything.

huacnlee pushed a commit that referenced this pull request Oct 20, 2026
Fixes the red **Checks** job on `main` (first noticed from #3607): the
v0.8.0 release notes credit `@UncertaintyDeterminesYou4ndMe` and
`@alex-vas`, and the typos tokenizer reads those handles as the
misspelled words `nd` and `vas` — 15 findings, all in
`release-notes.md`, reproduced with the pinned `typos-cli 1.40.0` on a
pristine checkout.

The fix is the narrowest one that meets the constraints from the #3607
review: the **exact handle strings** are added to `extend-ignore-re`, so

- the handles stay verbatim (nothing in `release-notes.md` is touched),
- `release-notes.md` itself is still checked — only the two handle
matches are skipped,
- no word-level or file-level disable that could mask a real typo
elsewhere.

Verified locally with the pinned `typos-cli 1.40.0`: `typos` exits 0 on
the full repo with this change, and both recent `Checks` runs on `main`
fail exactly on these findings.
@huacnlee

Copy link
Copy Markdown
Member

Sorry, do not add more example, just add to story.

@huacnlee huacnlee left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change

noosxe and others added 2 commits October 20, 2026 22:32
The maintainer asked for no new example crate, so the composition moves
from examples/completion into a "Completion" section of the Input story.

The popup now uses popover_style and ListItem rows, the surface and rows
Combobox uses, instead of approximating them. The four captured-key
handlers share one revalidation check. The leaving-the-token regression
now drives the mounted story with keystrokes (type `echo $HO`, then
`home enter` with no edit), and fails if the revalidation is removed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@noosxe noosxe changed the title example: Application-owned completion menu around a single-line Input story: Add an application-owned completion menu to the Input story Oct 20, 2026
@noosxe

noosxe commented Oct 20, 2026

Copy link
Copy Markdown
Contributor Author

Done: the standalone example is gone and the completion menu is now a "Completion" section of the Input story (crates/story/src/stories/input_completion.rs). The workspace member and README row are removed too.

While moving it, the popup now uses popover_style and ListItem rows like Combobox, and the regression for leaving the token now drives the story with keystrokes (echo $HO, then home enter) instead of only testing the scan function.

@huacnlee
huacnlee merged commit a64a609 into longbridge:main Oct 22, 2026
18 checks passed
@noosxe
noosxe deleted the input-completion-example branch October 22, 2026 13:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants