Skip to content

Commit 6ab57fa

Browse files
authored
Remove modifier checks from prompt history navigation (#5467)
1 parent a9a87c2 commit 6ab57fa

File tree

2 files changed

+85
-27
lines changed

2 files changed

+85
-27
lines changed

webview-ui/src/components/chat/__tests__/ChatTextArea.spec.tsx

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,13 +445,15 @@ describe("ChatTextArea", () => {
445445
})
446446
})
447447

448-
it("should navigate to previous prompt on arrow up", () => {
448+
it("should navigate to previous prompt on arrow up when cursor is at beginning", () => {
449449
const setInputValue = vi.fn()
450450
const { container } = render(
451451
<ChatTextArea {...defaultProps} setInputValue={setInputValue} inputValue="" />,
452452
)
453453

454454
const textarea = container.querySelector("textarea")!
455+
// Ensure cursor is at the beginning
456+
textarea.setSelectionRange(0, 0)
455457

456458
// Simulate arrow up key press
457459
fireEvent.keyDown(textarea, { key: "ArrowUp" })
@@ -755,6 +757,86 @@ describe("ChatTextArea", () => {
755757
fireEvent.keyDown(textarea, { key: "ArrowUp" })
756758
expect(setInputValue).toHaveBeenCalledWith("Message 2")
757759
})
760+
761+
it("should not navigate history with arrow up when cursor is not at beginning", () => {
762+
const setInputValue = vi.fn()
763+
const { container } = render(
764+
<ChatTextArea {...defaultProps} setInputValue={setInputValue} inputValue="Some text here" />,
765+
)
766+
767+
const textarea = container.querySelector("textarea")!
768+
// Set cursor to middle of text (not at beginning)
769+
textarea.setSelectionRange(5, 5)
770+
771+
// Clear any calls from initial render
772+
setInputValue.mockClear()
773+
774+
// Simulate arrow up key press
775+
fireEvent.keyDown(textarea, { key: "ArrowUp" })
776+
777+
// Should not navigate history, allowing default behavior (move cursor to start)
778+
expect(setInputValue).not.toHaveBeenCalled()
779+
})
780+
781+
it("should navigate history with arrow up when cursor is at beginning", () => {
782+
const setInputValue = vi.fn()
783+
const { container } = render(
784+
<ChatTextArea {...defaultProps} setInputValue={setInputValue} inputValue="Some text here" />,
785+
)
786+
787+
const textarea = container.querySelector("textarea")!
788+
// Set cursor to beginning of text
789+
textarea.setSelectionRange(0, 0)
790+
791+
// Clear any calls from initial render
792+
setInputValue.mockClear()
793+
794+
// Simulate arrow up key press
795+
fireEvent.keyDown(textarea, { key: "ArrowUp" })
796+
797+
// Should navigate to history since cursor is at beginning
798+
expect(setInputValue).toHaveBeenCalledWith("Third prompt")
799+
})
800+
801+
it("should navigate history with Command+Up when cursor is at beginning", () => {
802+
const setInputValue = vi.fn()
803+
const { container } = render(
804+
<ChatTextArea {...defaultProps} setInputValue={setInputValue} inputValue="Some text here" />,
805+
)
806+
807+
const textarea = container.querySelector("textarea")!
808+
// Set cursor to beginning of text
809+
textarea.setSelectionRange(0, 0)
810+
811+
// Clear any calls from initial render
812+
setInputValue.mockClear()
813+
814+
// Simulate Command+Up key press
815+
fireEvent.keyDown(textarea, { key: "ArrowUp", metaKey: true })
816+
817+
// Should navigate to history since cursor is at beginning (same as regular Up)
818+
expect(setInputValue).toHaveBeenCalledWith("Third prompt")
819+
})
820+
821+
it("should not navigate history with Command+Up when cursor is not at beginning", () => {
822+
const setInputValue = vi.fn()
823+
const { container } = render(
824+
<ChatTextArea {...defaultProps} setInputValue={setInputValue} inputValue="Some text here" />,
825+
)
826+
827+
const textarea = container.querySelector("textarea")!
828+
// Set cursor to middle of text (not at beginning)
829+
textarea.setSelectionRange(5, 5)
830+
831+
// Clear any calls from initial render
832+
setInputValue.mockClear()
833+
834+
// Simulate Command+Up key press
835+
fireEvent.keyDown(textarea, { key: "ArrowUp", metaKey: true })
836+
837+
// Should not navigate history, allowing default behavior (same as regular Up)
838+
expect(setInputValue).not.toHaveBeenCalled()
839+
})
758840
})
759841
})
760842

webview-ui/src/components/chat/hooks/usePromptHistory.ts

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -139,32 +139,8 @@ export const usePromptHistory = ({
139139
const isAtBeginning = selectionStart === 0 && selectionEnd === 0
140140
const isAtEnd = selectionStart === value.length && selectionEnd === value.length
141141

142-
// Check for modifier keys (Alt or Cmd/Ctrl)
143-
const hasModifier = event.altKey || event.metaKey || event.ctrlKey
144-
145-
// Handle explicit history navigation with Alt+Up/Down
146-
if (hasModifier && (event.key === "ArrowUp" || event.key === "ArrowDown")) {
147-
event.preventDefault()
148-
149-
if (event.key === "ArrowUp") {
150-
// Save current input if starting navigation
151-
if (historyIndex === -1) {
152-
setTempInput(inputValue)
153-
}
154-
return navigateToHistory(historyIndex + 1, textarea, "start")
155-
} else {
156-
// ArrowDown
157-
if (historyIndex > 0) {
158-
return navigateToHistory(historyIndex - 1, textarea, "end")
159-
} else if (historyIndex === 0) {
160-
returnToCurrentInput(textarea, "end")
161-
return true
162-
}
163-
}
164-
}
165-
166-
// Handle smart navigation without modifiers
167-
if (!hasSelection && !hasModifier) {
142+
// Handle smart navigation
143+
if (!hasSelection) {
168144
// Only navigate history with UP if cursor is at the very beginning
169145
if (event.key === "ArrowUp" && isAtBeginning) {
170146
event.preventDefault()

0 commit comments

Comments
 (0)