fix(app): keep the @ menu's highlight and Enter on current results (#59434)

This commit is contained in:
Kit Langton
2026-10-29 19:43:27 -07:00
committed by GitHub
Unverified
parent 96c5c3a11a
commit f1e475ef76
4 changed files with 149 additions and 18 deletions
@@ -166,7 +166,7 @@ export function createComposerEditor(input: {
)
}
const [found, setFound] = createStore<{ files: ComposerSuggestion[] }>({ files: [] })
const [found, setFound] = createStore<{ query?: string; files: ComposerSuggestion[] }>({ files: [] })
let fileQuery: string | undefined
let fileAbort = new AbortController()
onCleanup(() => abort(fileAbort))
@@ -181,7 +181,7 @@ export function createComposerEditor(input: {
fileAbort = controller
if (query === undefined || !query.trim() || /\s/.test(query)) {
if (found.files.length) setFound("files", [])
if (found.files.length) setFound({ query: undefined, files: [] })
return
}
@@ -189,7 +189,7 @@ export function createComposerEditor(input: {
const timer = setTimeout(
() =>
void Promise.resolve(input.searchContextFiles(query, controller.signal)).then(
(files) => !controller.signal.aborted && setFound("files", files),
(files) => !controller.signal.aborted && setFound({ query, files }),
() => undefined,
),
150,
@@ -263,6 +263,12 @@ export function createComposerEditor(input: {
})),
)
// Files from the previous query stay listed while the next search runs, but the keyboard can't highlight or pick them.
const stale = (item: ComposerSuggestion) =>
state.popover.type === "context" && item.kind === "file" && !item.recent && found.query !== state.popover.query
const selectable = () => suggestions().flatMap((item) => (stale(item) ? [] : [item.id]))
const suggestions = () => {
if (state.popover.type !== "history") return list().flat()
const query = state.popover.query.trim().toLowerCase()
@@ -324,6 +330,8 @@ export function createComposerEditor(input: {
}
function dispatch(event: ComposerInteractionEvent) {
if (event.type === "popover.active" && !selectable().includes(event.id)) return false
if (event.type === "popover.close") cancelFocus()
if (
@@ -398,7 +406,7 @@ export function createComposerEditor(input: {
key: event.key,
ctrl: event.ctrlKey && !event.metaKey && !event.altKey && !event.shiftKey,
composing: event.isComposing,
ids: suggestions().map((item) => item.id),
ids: selectable(),
empty: draft.state.prompt.every((part) => !("content" in part) || part.content.length === 0),
})
@@ -452,9 +460,9 @@ export function createComposerEditor(input: {
createEffect(() => {
if (state.popover.type === "closed") return
const ids = suggestions().map((item) => item.id)
const ids = selectable()
if (state.popover.activeID ? ids.includes(state.popover.activeID) : ids.length === 0) return
if (state.popover.activeID ? state.popover.chosen && ids.includes(state.popover.activeID) : ids.length === 0) return
dispatch({ type: "popover.results", ids })
})
@@ -146,9 +146,28 @@ test.each<{
name: "loops active popover items with arrow keys",
from: { popover: { type: "context", query: "", activeID: "second" } },
event: key("ArrowDown", { ids: ["first", "second"] }),
popover: { type: "context", query: "", activeID: "first" },
popover: { type: "context", query: "", activeID: "first", chosen: true },
handled: true,
},
{
name: "moves a highlight the user didn't choose to the first row when results reorder",
from: { popover: { type: "context", query: "se", activeID: "server" } },
event: { type: "popover.results", ids: ["session", "server"] },
popover: { type: "context", query: "se", activeID: "session", chosen: false },
},
{
name: "keeps a chosen highlight when results reorder",
from: { popover: { type: "context", query: "se", activeID: "server", chosen: true } },
event: { type: "popover.results", ids: ["session", "server"] },
popover: { type: "context", query: "se", activeID: "server", chosen: true },
},
{
name: "forgets the chosen highlight when the query changes",
from: { popover: { type: "context", query: "se", activeID: "server", chosen: true } },
event: { type: "input.changed", value: "@ses" },
draft: persisted("@ses"),
popover: { type: "context", query: "ses" },
},
])("$name", (row) => {
const result = transitionComposer(
{ ...createComposerInteractionState(), ...row.from },
@@ -5,10 +5,10 @@ export type ComposerInteractionState = {
mode: "normal" | "shell"
popover:
| { type: "closed" }
| { type: "context"; query: string; activeID?: string }
| { type: "command-inline"; query: string; activeID?: string }
| { type: "command-menu"; query: string; activeID?: string }
| { type: "history"; query: string; activeID?: string }
| { type: "context"; query: string; activeID?: string; chosen?: boolean }
| { type: "command-inline"; query: string; activeID?: string; chosen?: boolean }
| { type: "command-menu"; query: string; activeID?: string; chosen?: boolean }
| { type: "history"; query: string; activeID?: string; chosen?: boolean }
drag: "idle" | "active"
focus: "editor" | "command-search" | "history-search" | "external"
activeContextID?: string
@@ -176,11 +176,11 @@ function queryChanged(state: ComposerInteractionState, query: string): ComposerE
if (state.popover.type === "closed") return unchanged(state)
if (state.popover.type === "history")
return changed({ ...state, popover: { ...state.popover, query, activeID: undefined } })
return changed({ ...state, popover: { ...state.popover, query, activeID: undefined, chosen: undefined } })
const popover = state.popover.type === "context" ? "context" : "command"
return changed({ ...state, popover: { ...state.popover, query, activeID: undefined } }, [
return changed({ ...state, popover: { ...state.popover, query, activeID: undefined, chosen: undefined } }, [
{ type: "popover.filter", popover, query },
])
}
@@ -188,21 +188,28 @@ function queryChanged(state: ComposerInteractionState, query: string): ComposerE
function resultsChanged(state: ComposerInteractionState, ids: string[]): ComposerEditorTransition {
if (state.popover.type === "closed") return unchanged(state)
const activeID = state.popover.activeID && ids.includes(state.popover.activeID) ? state.popover.activeID : ids[0]
// Until the user picks a row, the highlight follows the first row as results arrive and reorder.
const kept =
!!state.popover.activeID &&
ids.includes(state.popover.activeID) &&
(state.popover.chosen || state.popover.type === "history")
const activeID = kept ? state.popover.activeID : ids[0]
if (activeID === state.popover.activeID) return unchanged(state)
return changed(
{ ...state, popover: { ...state.popover, activeID } },
{ ...state, popover: { ...state.popover, activeID, chosen: kept && state.popover.chosen } },
state.popover.type === "history" && activeID ? [{ type: "history.preview", id: activeID }] : [],
)
}
function activeChanged(state: ComposerInteractionState, id: string): ComposerEditorTransition {
if (state.popover.type === "closed" || state.popover.activeID === id) return unchanged(state)
if (state.popover.type === "closed" || (state.popover.activeID === id && state.popover.chosen))
return unchanged(state)
return changed(
{ ...state, popover: { ...state.popover, activeID: id } },
{ ...state, popover: { ...state.popover, activeID: id, chosen: true } },
state.popover.type === "history" ? [{ type: "history.preview", id }] : [],
)
}
@@ -296,7 +303,7 @@ function keyDown(
: index
return changed(
{ ...state, popover: { ...state.popover, activeID: event.ids[next] } },
{ ...state, popover: { ...state.popover, activeID: event.ids[next], chosen: true } },
state.popover.type === "history" ? [{ type: "history.preview", id: event.ids[next] }] : [],
true,
)
@@ -0,0 +1,97 @@
import { afterEach, expect, jest, test } from "bun:test"
import { createRoot } from "solid-js"
import { createStore } from "solid-js/store"
import type { ComposerPersistedState, ComposerSuggestion } from "@/composer/types"
import { createComposerEditor } from "@/composer/editor/interaction"
afterEach(() => {
jest.useRealTimers()
})
test("moves the @ highlight to the first file once the next search reorders the results", async () => {
const value = setup({ se: ["src/server.ts", "src/session.ts"], ses: ["src/session.ts", "src/server.ts"] })
try {
await value.type("@se")
expect(activeID(value)).toBe("file:src/server.ts")
await value.type("@ses", { search: false })
expect(value.editor.suggestions().map((item) => item.id)).toEqual(["file:src/server.ts", "file:src/session.ts"])
await value.search()
expect(activeID(value)).toBe("file:src/session.ts")
} finally {
value.dispose()
}
})
test("Enter does not pick a file left over from the previous @ query while the next search runs", async () => {
const value = setup({ r: ["r/plan.md"], re: ["re/plan.md"], rez: [] })
try {
await value.type("@r")
await value.type("@re")
await value.type("@rez", { search: false })
expect(value.editor.suggestions().map((item) => item.id)).toEqual(["file:re/plan.md"])
value.editor.onKeyDown(new KeyboardEvent("keydown", { key: "Enter" }))
expect(value.editor.value()).toBe("@rez")
expect(value.editor.parts().some((part) => part.type === "file")).toBe(false)
await value.search()
expect(value.editor.suggestions()).toEqual([])
} finally {
value.dispose()
}
})
function setup(results: Record<string, string[]>) {
jest.useFakeTimers()
return createRoot((dispose) => {
const store = createStore<ComposerPersistedState>({
prompt: [{ type: "text", content: "", start: 0, end: 0 }],
cursor: 0,
context: { items: [] },
})
const editor = createComposerEditor({
store,
commands: () => [],
context: () => [],
searchContextFiles: (query) => (results[query] ?? []).map(file),
view: { submit: { stopping: () => false, onSubmit() {}, onStop() {} } },
})
const search = async () => {
jest.runOnlyPendingTimers()
await flush()
}
const type = async (content: string, options?: { search: boolean }) => {
editor.onInput(content, [{ type: "text", content, start: 0, end: content.length }], content.length)
await flush()
if (options?.search !== false) await search()
}
return { editor, type, search, dispose }
})
}
// The search result and the filtered list each settle a few microtasks later.
async function flush() {
for (const _ of Array.from({ length: 5 })) await Promise.resolve()
}
function activeID(value: ReturnType<typeof setup>) {
return value.editor.state.popover.type === "closed" ? undefined : value.editor.state.popover.activeID
}
function file(path: string): ComposerSuggestion {
return {
id: `file:${path}`,
kind: "file",
label: path,
path,
mention: { type: "file", path, content: `@${path}`, start: 0, end: 0 },
}
}