Fix several bugs in prompt history (#734)

Co-authored-by: Marat Boshernitsan <maratb@google.com>
This commit is contained in:
Marat Boshernitsan
2025-06-03 23:01:26 -07:00
committed by GitHub
parent c313762ba0
commit 7de790fbf2
4 changed files with 136 additions and 22 deletions

View File

@@ -4,7 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/
import React, { useCallback } from 'react';
import React, { useCallback, useEffect, useState } from 'react';
import { Text, Box, useInput, useStdin } from 'ink';
import { Colors } from '../colors.js';
import { SuggestionsDisplay } from './SuggestionsDisplay.js';
@@ -19,7 +19,7 @@ import { isAtCommand, isSlashCommand } from '../utils/commandUtils.js';
import { SlashCommand } from '../hooks/slashCommandProcessor.js';
import { Config } from '@gemini-code/core';
interface InputPromptProps {
export interface InputPromptProps {
onSubmit: (value: string) => void;
userMessages: readonly string[];
onClearScreen: () => void;
@@ -54,6 +54,8 @@ export const InputPrompt: React.FC<InputPromptProps> = ({
);
const suggestionsWidth = Math.max(60, Math.floor(terminalSize.columns * 0.8));
const [justNavigatedHistory, setJustNavigatedHistory] = useState(false);
const { stdin, setRawMode } = useStdin();
const buffer = useTextBuffer({
@@ -84,14 +86,35 @@ export const InputPrompt: React.FC<InputPromptProps> = ({
[onSubmit, buffer, resetCompletionState],
);
const customSetTextAndResetCompletionSignal = useCallback(
(newText: string) => {
buffer.setText(newText);
setJustNavigatedHistory(true);
},
[buffer, setJustNavigatedHistory],
);
const inputHistory = useInputHistory({
userMessages,
onSubmit: handleSubmitAndClear,
isActive: !completion.showSuggestions,
currentQuery: buffer.text,
onChange: buffer.setText,
onChange: customSetTextAndResetCompletionSignal,
});
// Effect to reset completion if history navigation just occurred and set the text
useEffect(() => {
if (justNavigatedHistory) {
resetCompletionState();
setJustNavigatedHistory(false);
}
}, [
justNavigatedHistory,
buffer.text,
resetCompletionState,
setJustNavigatedHistory,
]);
const completionSuggestions = completion.suggestions;
const handleAutocomplete = useCallback(
(indexToUse: number) => {
@@ -276,8 +299,8 @@ export const InputPrompt: React.FC<InputPromptProps> = ({
// Standard arrow navigation within the buffer
if (key.upArrow && !completion.showSuggestions) {
if (
buffer.visualCursor[0] === 0 &&
buffer.visualScrollRow === 0 &&
(buffer.allVisualLines.length === 1 || // Always navigate for single line
(buffer.visualCursor[0] === 0 && buffer.visualScrollRow === 0)) &&
inputHistory.navigateUp
) {
inputHistory.navigateUp();
@@ -288,7 +311,8 @@ export const InputPrompt: React.FC<InputPromptProps> = ({
}
if (key.downArrow && !completion.showSuggestions) {
if (
buffer.visualCursor[0] === buffer.allVisualLines.length - 1 &&
(buffer.allVisualLines.length === 1 || // Always navigate for single line
buffer.visualCursor[0] === buffer.allVisualLines.length - 1) &&
inputHistory.navigateDown
) {
inputHistory.navigateDown();