fix(ui): show command suggestions even on perfect match and sort them (#15287)

This commit is contained in:
Sandy Tao
2025-12-18 14:05:36 -10:00
committed by GitHub
parent 1e10492e55
commit 70696e364b
5 changed files with 209 additions and 11 deletions

View File

@@ -768,6 +768,63 @@ describe('InputPrompt', () => {
unmount();
});
it('should execute perfect match on Enter even if suggestions are showing, if at first suggestion', async () => {
mockedUseCommandCompletion.mockReturnValue({
...mockCommandCompletion,
showSuggestions: true,
suggestions: [
{ label: 'review', value: 'review' }, // Match is now at index 0
{ label: 'review-frontend', value: 'review-frontend' },
],
activeSuggestionIndex: 0,
isPerfectMatch: true,
});
props.buffer.text = '/review';
const { stdin, unmount } = renderWithProviders(<InputPrompt {...props} />, {
uiActions,
});
await act(async () => {
stdin.write('\r');
});
await waitFor(() => {
expect(props.onSubmit).toHaveBeenCalledWith('/review');
});
unmount();
});
it('should autocomplete and NOT execute on Enter if a DIFFERENT suggestion is selected even if perfect match', async () => {
mockedUseCommandCompletion.mockReturnValue({
...mockCommandCompletion,
showSuggestions: true,
suggestions: [
{ label: 'review', value: 'review' },
{ label: 'review-frontend', value: 'review-frontend' },
],
activeSuggestionIndex: 1, // review-frontend selected (not the perfect match at 0)
isPerfectMatch: true, // /review is a perfect match
});
props.buffer.text = '/review';
const { stdin, unmount } = renderWithProviders(<InputPrompt {...props} />, {
uiActions,
});
await act(async () => {
stdin.write('\r');
});
await waitFor(() => {
// Should handle autocomplete for index 1
expect(mockCommandCompletion.handleAutocomplete).toHaveBeenCalledWith(1);
// Should NOT submit
expect(props.onSubmit).not.toHaveBeenCalled();
});
unmount();
});
it('should submit directly on Enter when a complete leaf command is typed', async () => {
mockedUseCommandCompletion.mockReturnValue({
...mockCommandCompletion,

View File

@@ -589,7 +589,12 @@ export const InputPrompt: React.FC<InputPromptProps> = ({
}
// If the command is a perfect match, pressing enter should execute it.
if (completion.isPerfectMatch && keyMatchers[Command.RETURN](key)) {
// We prioritize execution unless the user is explicitly selecting a different suggestion.
if (
completion.isPerfectMatch &&
keyMatchers[Command.RETURN](key) &&
(!completion.showSuggestions || completion.activeSuggestionIndex <= 0)
) {
handleSubmit(buffer.text);
return;
}