Skip to content

Commit 3c85cf4

Browse files
authored
fix(app): only navigate prompt history at input boundaries (anomalyco#13690)
1 parent 878ddc6 commit 3c85cf4

3 files changed

Lines changed: 20 additions & 27 deletions

File tree

packages/app/src/components/prompt-input.tsx

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -911,31 +911,13 @@ export const PromptInput: Component<PromptInputProps> = (props) => {
911911
if (!collapsed) return
912912

913913
const cursorPosition = getCursorPosition(editorRef)
914-
const textLength = promptLength(prompt.current())
915914
const textContent = prompt
916915
.current()
917916
.map((part) => ("content" in part ? part.content : ""))
918917
.join("")
919918
const direction = event.key === "ArrowUp" ? "up" : "down"
920-
if (!canNavigateHistoryAtCursor(direction, textContent, cursorPosition)) return
921-
const isEmpty = textContent.trim() === "" || textLength <= 1
922-
const hasNewlines = textContent.includes("\n")
923-
const inHistory = store.historyIndex >= 0
924-
const atStart = cursorPosition <= (isEmpty ? 1 : 0)
925-
const atEnd = cursorPosition >= (isEmpty ? textLength - 1 : textLength)
926-
const allowUp = isEmpty || atStart || (!hasNewlines && !inHistory) || (inHistory && atEnd)
927-
const allowDown = isEmpty || atEnd || (!hasNewlines && !inHistory) || (inHistory && atStart)
928-
929-
if (direction === "up") {
930-
if (!allowUp) return
931-
if (navigateHistory("up")) {
932-
event.preventDefault()
933-
}
934-
return
935-
}
936-
937-
if (!allowDown) return
938-
if (navigateHistory("down")) {
919+
if (!canNavigateHistoryAtCursor(direction, textContent, cursorPosition, store.historyIndex >= 0)) return
920+
if (navigateHistory(direction)) {
939921
event.preventDefault()
940922
}
941923
return

packages/app/src/components/prompt-input/history.test.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ describe("prompt-input history", () => {
7373
expect(original[1].selection?.startLine).toBe(1)
7474
})
7575

76-
test("canNavigateHistoryAtCursor only allows multiline boundaries", () => {
76+
test("canNavigateHistoryAtCursor only allows prompt boundaries", () => {
7777
const value = "a\nb\nc"
7878

7979
expect(canNavigateHistoryAtCursor("up", value, 0)).toBe(true)
@@ -85,7 +85,16 @@ describe("prompt-input history", () => {
8585
expect(canNavigateHistoryAtCursor("up", value, 5)).toBe(false)
8686
expect(canNavigateHistoryAtCursor("down", value, 5)).toBe(true)
8787

88-
expect(canNavigateHistoryAtCursor("up", "abc", 1)).toBe(true)
89-
expect(canNavigateHistoryAtCursor("down", "abc", 1)).toBe(true)
88+
expect(canNavigateHistoryAtCursor("up", "abc", 0)).toBe(true)
89+
expect(canNavigateHistoryAtCursor("down", "abc", 3)).toBe(true)
90+
expect(canNavigateHistoryAtCursor("up", "abc", 1)).toBe(false)
91+
expect(canNavigateHistoryAtCursor("down", "abc", 1)).toBe(false)
92+
93+
expect(canNavigateHistoryAtCursor("up", "abc", 0, true)).toBe(true)
94+
expect(canNavigateHistoryAtCursor("up", "abc", 3, true)).toBe(true)
95+
expect(canNavigateHistoryAtCursor("down", "abc", 0, true)).toBe(true)
96+
expect(canNavigateHistoryAtCursor("down", "abc", 3, true)).toBe(true)
97+
expect(canNavigateHistoryAtCursor("up", "abc", 1, true)).toBe(false)
98+
expect(canNavigateHistoryAtCursor("down", "abc", 1, true)).toBe(false)
9099
})
91100
})

packages/app/src/components/prompt-input/history.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ const DEFAULT_PROMPT: Prompt = [{ type: "text", content: "", start: 0, end: 0 }]
44

55
export const MAX_HISTORY = 100
66

7-
export function canNavigateHistoryAtCursor(direction: "up" | "down", text: string, cursor: number) {
8-
if (!text.includes("\n")) return true
7+
export function canNavigateHistoryAtCursor(direction: "up" | "down", text: string, cursor: number, inHistory = false) {
98
const position = Math.max(0, Math.min(cursor, text.length))
10-
if (direction === "up") return !text.slice(0, position).includes("\n")
11-
return !text.slice(position).includes("\n")
9+
const atStart = position === 0
10+
const atEnd = position === text.length
11+
if (inHistory) return atStart || atEnd
12+
if (direction === "up") return position === 0
13+
return position === text.length
1214
}
1315

1416
export function clonePromptParts(prompt: Prompt): Prompt {

0 commit comments

Comments
 (0)