From 86f9bb2a163deb72f1744e6d6af35f72cbca170b Mon Sep 17 00:00:00 2001 From: Wooseong Kim <2222333+innocarpe@users.noreply.github.com> Date: Tue, 10 Nov 2026 11:59:14 +0900 Subject: [PATCH] fix(native-chat): highlight the slash item whose name best matches the typed query (#40487) Commands render before skills in the slash menu, so typing an exact skill name could leave a fuzzy command highlighted and Enter would pick the wrong item. After each edit or caret move the picker now highlights the row whose name ranks best against the query. Arrow-key navigation still owns the highlight between edits, and a catalog refresh keeps the row the user arrowed to (by id) instead of jumping. Co-authored-by: Neil --- .../composer/NativeChatComposerPane.tsx | 2 +- .../use-native-chat-picker-state.test.tsx | 108 +++++++++++++++++- .../pickers/use-native-chat-picker-state.ts | 12 +- .../use-native-chat-preferred-suggestion.ts | 49 ++++++++ .../input/native-chat-picker-items.ts | 18 +++ 5 files changed, 184 insertions(+), 5 deletions(-) create mode 100644 src/renderer/src/components/native-chat/composer/pickers/use-native-chat-preferred-suggestion.ts diff --git a/src/renderer/src/components/native-chat/composer/NativeChatComposerPane.tsx b/src/renderer/src/components/native-chat/composer/NativeChatComposerPane.tsx index 442257a4319..23f46b5c735 100644 --- a/src/renderer/src/components/native-chat/composer/NativeChatComposerPane.tsx +++ b/src/renderer/src/components/native-chat/composer/NativeChatComposerPane.tsx @@ -337,7 +337,7 @@ export const NativeChatComposerPane = forwardRef 0, ...(!editSession && steerHead ? { steerHead } : {}), chordSteersHead: sendButton.chordSteersHead, - setActiveSuggestion, + setActiveSuggestion: picker.navigateSuggestion, setDraft, setCaret }) diff --git a/src/renderer/src/components/native-chat/composer/pickers/use-native-chat-picker-state.test.tsx b/src/renderer/src/components/native-chat/composer/pickers/use-native-chat-picker-state.test.tsx index 960f83ef07a..dee5595bcbd 100644 --- a/src/renderer/src/components/native-chat/composer/pickers/use-native-chat-picker-state.test.tsx +++ b/src/renderer/src/components/native-chat/composer/pickers/use-native-chat-picker-state.test.tsx @@ -1,12 +1,37 @@ // @vitest-environment happy-dom -import { renderHook } from '@testing-library/react' -import { describe, expect, it, vi } from 'vitest' +import { act, renderHook } from '@testing-library/react' +import { useState } from 'react' +import { afterEach, describe, expect, it, vi } from 'vitest' +import type { DiscoveredSkill } from '../../../../../../shared/skills' import { useNativeChatPickerState } from './use-native-chat-picker-state' +const skillMock = vi.hoisted((): { skills: DiscoveredSkill[] } => ({ skills: [] })) + vi.mock('./use-native-chat-skills', () => ({ - useNativeChatSkills: () => ({ status: 'ready', skills: [], error: null, retry: () => {} }) + useNativeChatSkills: () => ({ + status: 'ready', + skills: skillMock.skills, + error: null, + retry: () => {} + }) })) +function skill(name: string): DiscoveredSkill { + return { + id: name, + name, + description: null, + providers: ['claude'], + sourceKind: 'repo', + sourceLabel: 'Repository', + rootPath: '/repo/.agents/skills', + directoryPath: `/repo/.agents/skills/${name}`, + skillFilePath: `/repo/.agents/skills/${name}/SKILL.md`, + installed: true, + updatedAt: null + } +} + function pickerMode(draft: string, recalledFromHistory: boolean): string { const setDraft = vi.fn() const { result, rerender } = renderHook( @@ -45,7 +70,42 @@ function pickerMode(draft: string, recalledFromHistory: boolean): string { return result.current.autocomplete.mode } +function renderSlashPicker(draft: string, caret: number) { + return renderHook( + ({ draft, caret }) => { + const [active, setActive] = useState(0) + const picker = useNativeChatPickerState({ + agent: 'claude', + terminalTabId: 'tab-1', + draftScopeKey: 'pane-1', + draft, + caret, + agentCommands: [{ name: 'design-consent' }, { name: 'deploy' }], + recalledFromHistory: false, + disabled: false, + isComposing: () => false, + textareaRef: { current: null }, + setDraft: vi.fn(), + setCaret: vi.fn(), + setActiveSuggestion: setActive + }) + const items = picker.autocomplete.mode === 'slash' ? picker.autocomplete.items : [] + return { + names: items.map((item) => item.name), + active, + setActive, + navigate: picker.navigateSuggestion + } + }, + { initialProps: { draft, caret } } + ) +} + describe('useNativeChatPickerState', () => { + afterEach(() => { + skillMock.skills = [] + }) + it.each([ ['open @app', 'mention'], ['/cl', 'slash'] @@ -53,4 +113,46 @@ describe('useNativeChatPickerState', () => { expect(pickerMode(draft, false)).toBe(typedMode) expect(pickerMode(draft, true)).toBe('none') }) + + it('highlights an exact skill below a fuzzy command and keeps arrow navigation', () => { + skillMock.skills = [skill('done')] + const { result, rerender } = renderSlashPicker('/doneX', 5) + expect(result.current.names).toEqual(['design-consent', 'done']) + expect(result.current.active).toBe(1) + act(() => result.current.setActive(0)) + rerender({ draft: '/doneX', caret: 5 }) + expect(result.current.active).toBe(0) + // Forward-deleting past the caret keeps the query but the edit handler still resets to 0. + act(() => result.current.setActive(0)) + rerender({ draft: '/done', caret: 5 }) + expect(result.current.active).toBe(1) + }) + + it('follows late skills to the best match but keeps a row the user arrowed to', () => { + const { result, rerender } = renderSlashPicker('/done', 5) + expect(result.current.names).toEqual(['design-consent']) + skillMock.skills = [skill('done')] + rerender({ draft: '/done', caret: 5 }) + expect(result.current.active).toBe(1) + + const browsing = renderSlashPicker('/', 1) + act(() => browsing.result.current.navigate(2)) + expect(browsing.result.current.names[2]).toBe('done') + skillMock.skills = [skill('aaa'), skill('done')] + browsing.rerender({ draft: '/', caret: 1 }) + expect(browsing.result.current.names).toEqual(['design-consent', 'deploy', 'aaa', 'done']) + expect(browsing.result.current.active).toBe(3) + }) + + it('keeps a row the user arrowed back to when an exact skill loads late', () => { + skillMock.skills = [skill('foo-bar'), skill('foo-baz')] + const { result, rerender } = renderSlashPicker('/foo', 4) + expect(result.current.names).toEqual(['foo-bar', 'foo-baz']) + expect(result.current.active).toBe(0) + act(() => result.current.navigate((index) => index + 1)) + act(() => result.current.navigate((index) => index - 1)) + skillMock.skills = [skill('foo-bar'), skill('foo-baz'), skill('foo')] + rerender({ draft: '/foo', caret: 4 }) + expect(result.current.names[result.current.active]).toBe('foo-bar') + }) }) diff --git a/src/renderer/src/components/native-chat/composer/pickers/use-native-chat-picker-state.ts b/src/renderer/src/components/native-chat/composer/pickers/use-native-chat-picker-state.ts index b5c57300341..095d2534398 100644 --- a/src/renderer/src/components/native-chat/composer/pickers/use-native-chat-picker-state.ts +++ b/src/renderer/src/components/native-chat/composer/pickers/use-native-chat-picker-state.ts @@ -34,6 +34,7 @@ import { import type { NativeChatPickerItem } from '../../../../../../shared/native-chat/input/native-chat-picker-items' import { nativeChatPickerCommands } from '../../../../../../shared/native-chat/input/native-chat-skill-visibility' import { useNativeChatSkills } from './use-native-chat-skills' +import { useNativeChatPreferredSuggestion } from './use-native-chat-preferred-suggestion' import { emitNativeChatPickerItemAccepted, emitNativeChatPickerOpened, @@ -52,6 +53,8 @@ export type NativeChatPickerState = { completeMention: (path: string) => void dismiss: (triggerKey: string) => void handleDraftOrCaretChange: (value: string, caret: number) => void + /** Arrow-key highlight moves; a catalog refresh keeps the row they land on until the next edit. */ + navigateSuggestion: Dispatch> } export function useNativeChatPickerState(args: { @@ -157,6 +160,12 @@ export function useNativeChatPickerState(args: { : derivedAutocomplete, [derivedAutocomplete, skillsChecking] ) + const navigateSuggestion = useNativeChatPreferredSuggestion( + autocomplete, + draft, + caret, + setActiveSuggestion + ) useEffect(() => { // Reset dismissal/origin on context changes so a reused composer cannot retain a prior pane’s suppression. @@ -309,6 +318,7 @@ export function useNativeChatPickerState(args: { completeItem, completeMention, dismiss, - handleDraftOrCaretChange + handleDraftOrCaretChange, + navigateSuggestion } } diff --git a/src/renderer/src/components/native-chat/composer/pickers/use-native-chat-preferred-suggestion.ts b/src/renderer/src/components/native-chat/composer/pickers/use-native-chat-preferred-suggestion.ts new file mode 100644 index 00000000000..505608869c3 --- /dev/null +++ b/src/renderer/src/components/native-chat/composer/pickers/use-native-chat-preferred-suggestion.ts @@ -0,0 +1,49 @@ +import { useCallback, useLayoutEffect, useRef, type Dispatch, type SetStateAction } from 'react' +import type { ComposerAutocomplete } from '../../../../../../shared/native-chat/input/native-chat-composer-autocomplete' +import { preferredNativeChatPickerIndex } from '../../../../../../shared/native-chat/input/native-chat-picker-items' + +/** Highlight the best name match after every edit or caret move; arrow keys own it in between. + * Returns the setter for user navigation, which pins the highlight until the next edit. */ +export function useNativeChatPreferredSuggestion( + autocomplete: ComposerAutocomplete, + draft: string, + caret: number, + setActiveSuggestion: Dispatch> +): Dispatch> { + const slash = autocomplete.mode === 'slash' ? autocomplete : null + // Why draft+caret, not just query: edits that keep the query (e.g. deleting text after it) still reset the highlight to 0. + const editKey = slash && `${caret}\0${draft}` + const rowKey = slash ? slash.items.map((item) => item.id).join('\0') : '' + const preferredIndex = slash ? preferredNativeChatPickerIndex(slash.items, slash.query) : 0 + const shownRef = useRef<{ editKey: string; rowIds: string[] } | null>(null) + const navigatedRef = useRef(false) + // Why layout: the edit handlers reset the highlight to 0, and this replaces it before the menu paints. + useLayoutEffect(() => { + const prev = shownRef.current + if (editKey === null) { + shownRef.current = null + navigatedRef.current = false + return + } + const rowIds = rowKey.split('\0') + shownRef.current = { editKey, rowIds } + if (prev?.editKey !== editKey) { + navigatedRef.current = false + setActiveSuggestion(preferredIndex) + return + } + // Rows changed with no edit (a catalog refresh): keep a row the user arrowed to, else follow the best match. + setActiveSuggestion((current) => { + const kept = navigatedRef.current ? rowIds.indexOf(prev.rowIds[current]) : -1 + return kept === -1 ? preferredIndex : kept + }) + }, [editKey, rowKey, preferredIndex, setActiveSuggestion]) + + return useCallback( + (action: SetStateAction) => { + navigatedRef.current = true + setActiveSuggestion(action) + }, + [setActiveSuggestion] + ) +} diff --git a/src/shared/native-chat/input/native-chat-picker-items.ts b/src/shared/native-chat/input/native-chat-picker-items.ts index d3e7077f017..2e29c3c3079 100644 --- a/src/shared/native-chat/input/native-chat-picker-items.ts +++ b/src/shared/native-chat/input/native-chat-picker-items.ts @@ -246,6 +246,24 @@ function rankItems( .map((entry) => entry.item) } +// Commands render before skills, and Enter uses this index into that same order. +// An exact name in the later section has to beat a fuzzy name in the earlier one. +export function preferredNativeChatPickerIndex( + items: readonly NativeChatPickerItem[], + query: string +): number { + let bestIndex = 0 + let bestRank = Number.POSITIVE_INFINITY + for (let index = 0; index < items.length; index += 1) { + const rank = getMatchRank(items[index], query, true) + if (rank !== null && rank < bestRank) { + bestRank = rank + bestIndex = index + } + } + return bestIndex +} + function getMatchRank( item: Pick, query: string,