-
Notifications
You must be signed in to change notification settings - Fork 87
fix(ui): stabilize agent note navigation #251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -81,11 +81,16 @@ export function useAppKeyboardShortcuts({ | |
| }: UseAppKeyboardShortcutsOptions) { | ||
| const activeMenuIdRef = useRef(activeMenuId); | ||
| const focusAreaRef = useRef(focusArea); | ||
| // Keep navigation shortcuts resilient if the keyboard hook changes how it stores callbacks. | ||
| const moveToAnnotatedHunkRef = useRef(moveToAnnotatedHunk); | ||
| const moveToHunkRef = useRef(moveToHunk); | ||
| const pagerModeRef = useRef(pagerMode); | ||
| const showHelpRef = useRef(showHelp); | ||
|
|
||
| activeMenuIdRef.current = activeMenuId; | ||
| focusAreaRef.current = focusArea; | ||
| moveToAnnotatedHunkRef.current = moveToAnnotatedHunk; | ||
| moveToHunkRef.current = moveToHunk; | ||
| pagerModeRef.current = pagerMode; | ||
| showHelpRef.current = showHelp; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Prompt To Fix With AIThis is a comment left during a code review.
Path: src/ui/hooks/useAppKeyboardShortcuts.ts
Line: 84-95
Comment:
**Partial ref-ization of keyboard callbacks**
`moveToAnnotatedHunk` and `moveToHunk` are now dispatched through refs, but the other action callbacks captured by the same keyboard handler closure — `scrollDiff`, `scrollCodeHorizontally`, `cycleTheme`, `toggleAgentNotes`, etc. — are still read from the stale closure directly. If the stated motivation (guarding against OpenTUI changing how it stores callbacks) ever becomes real, these would be vulnerable by the same logic. Either apply the ref pattern consistently to all callbacks or document exactly which ones OpenTUI is known to capture stably so future readers understand the asymmetry.
How can I resolve this? If you propose a fix, please make it concise. |
||
|
|
||
|
|
@@ -362,22 +367,22 @@ export function useAppKeyboardShortcuts({ | |
| } | ||
|
|
||
| if (key.name === "[") { | ||
| runAndCloseMenu(() => moveToHunk(-1)); | ||
| runAndCloseMenu(() => moveToHunkRef.current(-1)); | ||
| return; | ||
| } | ||
|
|
||
| if (key.name === "]") { | ||
| runAndCloseMenu(() => moveToHunk(1)); | ||
| runAndCloseMenu(() => moveToHunkRef.current(1)); | ||
| return; | ||
| } | ||
|
|
||
| if (key.sequence === "{") { | ||
| runAndCloseMenu(() => moveToAnnotatedHunk(-1)); | ||
| runAndCloseMenu(() => moveToAnnotatedHunkRef.current(-1)); | ||
| return; | ||
| } | ||
|
|
||
| if (key.sequence === "}") { | ||
| runAndCloseMenu(() => moveToAnnotatedHunk(1)); | ||
| runAndCloseMenu(() => moveToAnnotatedHunkRef.current(1)); | ||
| } | ||
| }; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,6 +29,7 @@ export function findNextHunkCursor( | |
| currentFileId: string | undefined, | ||
| currentHunkIndex: number, | ||
| delta: number, | ||
| streamCursors: HunkCursor[] = cursors, | ||
| ): HunkCursor | null { | ||
| if (cursors.length === 0) { | ||
| return null; | ||
|
|
@@ -40,9 +41,54 @@ export function findNextHunkCursor( | |
| const nextIndex = | ||
| currentIndex >= 0 | ||
| ? Math.min(Math.max(currentIndex + delta, 0), cursors.length - 1) | ||
| : delta >= 0 | ||
| ? 0 | ||
| : cursors.length - 1; | ||
| : findNearestCursorIndex(cursors, streamCursors, currentFileId, currentHunkIndex, delta); | ||
|
|
||
| return cursors[nextIndex] ?? null; | ||
| } | ||
|
|
||
| /** Resolve relative movement when the current hunk is not in the target cursor subset. */ | ||
| function findNearestCursorIndex( | ||
| cursors: HunkCursor[], | ||
| streamCursors: HunkCursor[], | ||
| currentFileId: string | undefined, | ||
| currentHunkIndex: number, | ||
| delta: number, | ||
| ) { | ||
| if (!currentFileId) { | ||
| return delta >= 0 ? 0 : cursors.length - 1; | ||
| } | ||
|
|
||
| const currentStreamIndex = streamCursors.findIndex( | ||
| (cursor) => cursor.fileId === currentFileId && cursor.hunkIndex === currentHunkIndex, | ||
| ); | ||
| if (currentStreamIndex < 0) { | ||
| return delta >= 0 ? 0 : cursors.length - 1; | ||
| } | ||
|
|
||
| const streamIndexByCursor = new Map( | ||
| streamCursors.map((cursor, index) => [`${cursor.fileId}\0${cursor.hunkIndex}`, index] as const), | ||
| ); | ||
| const cursorStreamIndex = (cursor: HunkCursor) => | ||
| streamIndexByCursor.get(`${cursor.fileId}\0${cursor.hunkIndex}`) ?? -1; | ||
| const indexedCursors = cursors | ||
| .map((cursor, index) => ({ index, streamIndex: cursorStreamIndex(cursor) })) | ||
| .filter(({ streamIndex }) => streamIndex >= 0); | ||
|
|
||
| if (indexedCursors.length === 0) { | ||
| return delta >= 0 ? 0 : cursors.length - 1; | ||
| } | ||
|
|
||
| if (delta >= 0) { | ||
| const nextCursor = indexedCursors.find(({ streamIndex }) => streamIndex > currentStreamIndex); | ||
| return nextCursor?.index ?? indexedCursors[indexedCursors.length - 1]!.index; | ||
|
Comment on lines
+83
to
+85
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When the current hunk is not in Prompt To Fix With AIThis is a comment left during a code review.
Path: src/ui/lib/hunks.ts
Line: 81-83
Comment:
**Forward `}` from beyond the last annotated hunk jumps backward, not to first**
When the current hunk is not in `annotatedCursors` and its stream position is past all annotated hunks, `indexedCursors.find(...)` returns `undefined` and the fallback resolves to the last annotated cursor — effectively a backward jump. The old code would have jumped to `cursors[0]` (first annotated hunk) in this case. The new behaviour is arguably more useful (nearest rather than wrap-to-start), and the test suite covers it, but it's a visible UX change worth calling out explicitly in a comment for future maintainers.
How can I resolve this? If you propose a fix, please make it concise. |
||
| } | ||
|
|
||
| for (let index = indexedCursors.length - 1; index >= 0; index -= 1) { | ||
| const indexedCursor = indexedCursors[index]!; | ||
| if (indexedCursor.streamIndex < currentStreamIndex) { | ||
| return indexedCursor.index; | ||
| } | ||
| } | ||
|
|
||
| return indexedCursors[0]!.index; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
additionLines/deletionLinesappears unreachablehunk.additionCountandhunk.deletionCountare used without a nullish fallback throughout the codebase (codeColumns.ts,pierre.ts,useHighlightedDiff.ts), which implies Pierre always populates them. The?? hunk.additionLinesarm silently falls back to the old changed-line-only count, so ifadditionCountwere ever absent the function would quietly return a narrower range rather than an error — the same bug this PR is fixing. A comment explaining whenadditionCountcan be absent, or an assertion/log, would make the intent clearer.Prompt To Fix With AI