Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions src/browser/components/pr-review.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2494,7 +2494,7 @@ const DiffViewer = memo(function DiffViewer({
if (state.selectionAnchor === null) {
store.setSelectionAnchor(state.focusedLine, side);
}
store.setFocusedLine(lineNum, side);
store.setFocusedLine(lineNum, side, "mouse");
const startLine = Math.min(anchor, lineNum);
const endLine = Math.max(anchor, lineNum);
if (startLine !== endLine) {
Expand All @@ -2510,7 +2510,7 @@ const DiffViewer = memo(function DiffViewer({
isDraggingRef.current = true;
dragAnchorRef.current = lineNum;
dragSideRef.current = side;
store.setFocusedLine(lineNum, side);
store.setFocusedLine(lineNum, side, "mouse");
store.setSelectionAnchor(lineNum, side);
setIsDraggingState(true);
},
Expand Down Expand Up @@ -2667,7 +2667,7 @@ const DiffViewer = memo(function DiffViewer({
}

if (closestLine !== null) {
store.setFocusedLine(closestLine, dragSide);
store.setFocusedLine(closestLine, dragSide, "mouse");
}
};

Expand Down Expand Up @@ -2702,6 +2702,7 @@ const DiffViewer = memo(function DiffViewer({
// Selection state for CSS-based highlighting (no per-row subscriptions)
const focusedLine = usePRReviewSelector((s) => s.focusedLine);
const focusedLineSide = usePRReviewSelector((s) => s.focusedLineSide);
const focusSource = usePRReviewSelector((s) => s.focusSource);
const selectionAnchor = usePRReviewSelector((s) => s.selectionAnchor);
const selectionAnchorSide = usePRReviewSelector((s) => s.selectionAnchorSide);

Expand Down Expand Up @@ -2756,8 +2757,10 @@ const DiffViewer = memo(function DiffViewer({
}
}

// 2. Scroll to focused line (after selection update)
if (focusedLine && !isDraggingState) {
// 2. Scroll to focused line (after selection update). Only for
// keyboard/programmatic focus — re-centering on mouse clicks breaks
// double-click word selection.
if (focusedLine && !isDraggingState && focusSource !== "mouse") {
const rowIndex = getRowIndexForLine(focusedLine, focusedLineSide);
if (rowIndex !== undefined) {
virtualizer.scrollToIndex(rowIndex, {
Expand All @@ -2775,6 +2778,7 @@ const DiffViewer = memo(function DiffViewer({
}, [
focusedLine,
focusedLineSide,
focusSource,
selectionAnchor,
selectionAnchorSide,
isDraggingState,
Expand Down Expand Up @@ -3125,7 +3129,7 @@ const DiffLineRow = memo(function DiffLineRow({
state.focusedLineSide ?? lineSide
);
}
store.setFocusedLine(lineNum, lineSide);
store.setFocusedLine(lineNum, lineSide, "mouse");
const startLine = Math.min(anchor, lineNum);
const endLine = Math.max(anchor, lineNum);
if (startLine !== endLine) {
Expand Down Expand Up @@ -3154,7 +3158,7 @@ const DiffLineRow = memo(function DiffLineRow({
}

// Normal click: focus the line (clear any selection)
store.setFocusedLine(lineNum, lineSide);
store.setFocusedLine(lineNum, lineSide, "mouse");
store.setSelectionAnchor(null, null);
},
[lineNum, lineSide, store]
Expand Down Expand Up @@ -3693,6 +3697,20 @@ const InlineCommentForm = memo(function InlineCommentForm({
textRef.current = text;

const [submitting, setSubmitting] = useState(false);
const formRef = useRef<HTMLDivElement>(null);

// Make sure the freshly opened form is fully visible with the smallest
// possible scroll (no re-centering).
useEffect(() => {
requestAnimationFrame(() => {
formRef.current?.scrollIntoView({
block: "nearest",
behavior: "instant",
});
});
// Only on mount, when the form is opened
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

// Persist draft text when the form unmounts without submitting
useEffect(() => {
Expand Down Expand Up @@ -3799,6 +3817,7 @@ const InlineCommentForm = memo(function InlineCommentForm({

return (
<div
ref={formRef}
data-inline-comment-form
className="mx-4 my-3 rounded-lg border border-border bg-card overflow-hidden shadow-sm"
style={{ fontFamily: "var(--font-sans)" }}
Expand Down
12 changes: 12 additions & 0 deletions src/browser/contexts/pr-review/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,18 @@ test("setFocusedLine updates line focus", () => {
const state = store.getSnapshot();
expect(state.focusedLine).toBe(42);
expect(state.focusedLineSide).toBe("new");
expect(state.focusSource).toBe("keyboard");
});

test("setFocusedLine records mouse source", () => {
const store = createStore();
store.selectFile("src/index.ts");

store.setFocusedLine(42, "new", "mouse");
expect(store.getSnapshot().focusSource).toBe("mouse");

store.setFocusedLine(43, "new");
expect(store.getSnapshot().focusSource).toBe("keyboard");
});

test("setFocusedLine clears skip block focus", () => {
Expand Down
22 changes: 21 additions & 1 deletion src/browser/contexts/pr-review/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,9 @@ interface PRReviewState {
// Line selection
focusedLine: number | null;
focusedLineSide: "old" | "new" | null; // 'old' for delete lines, 'new' for insert/context
// How the current focus was set, so the UI can react differently (e.g. only
// keyboard focus gets scrolled to center)
focusSource: "mouse" | "keyboard";
selectionAnchor: number | null;
selectionAnchorSide: "old" | "new" | null;
focusedSkipBlockIndex: number | null; // Index of focused skip block for keyboard navigation
Expand Down Expand Up @@ -613,6 +616,7 @@ export class PRReviewStore {
...initialState,
files: sortedFiles,
viewerCanMergeAsAdmin: false,
focusSource: "keyboard",

// PR data (loaded separately)
reviews: [],
Expand Down Expand Up @@ -1890,11 +1894,13 @@ export class PRReviewStore {

setFocusedLine = (
line: number | null,
side: "old" | "new" | null = "new"
side: "old" | "new" | null = "new",
source: "mouse" | "keyboard" = "keyboard"
) => {
this.set({
focusedLine: line,
focusedLineSide: line !== null ? side : null,
focusSource: source,
focusedSkipBlockIndex: null, // Clear skip block focus when focusing a line
});
};
Expand Down Expand Up @@ -2184,6 +2190,7 @@ export class PRReviewStore {
this.set({
focusedLine: nextItem.lineNum,
focusedLineSide: nextItem.side,
focusSource: "keyboard",
focusedSkipBlockIndex: null,
selectionAnchor: null,
selectionAnchorSide: null,
Expand Down Expand Up @@ -2231,6 +2238,7 @@ export class PRReviewStore {
this.set({
focusedLine: nextNav.lineNum,
focusedLineSide: nextNav.side,
focusSource: "keyboard",
focusedPendingCommentId: null,
focusedCommentId: null,
selectionAnchor: null,
Expand All @@ -2248,6 +2256,7 @@ export class PRReviewStore {
this.set({
focusedLine: pendingLine,
focusedLineSide: "new",
focusSource: "keyboard",
focusedPendingCommentId: null,
selectionAnchor: null,
selectionAnchorSide: null,
Expand Down Expand Up @@ -2284,6 +2293,7 @@ export class PRReviewStore {
this.set({
focusedLine: nextNav.lineNum,
focusedLineSide: nextNav.side,
focusSource: "keyboard",
focusedCommentId: null,
selectionAnchor: null,
selectionAnchorSide: null,
Expand Down Expand Up @@ -2311,6 +2321,7 @@ export class PRReviewStore {
this.set({
focusedLine: commentLine,
focusedLineSide: "new",
focusSource: "keyboard",
focusedCommentId: null,
selectionAnchor: null,
selectionAnchorSide: null,
Expand Down Expand Up @@ -2458,6 +2469,7 @@ export class PRReviewStore {
focusedSkipBlockIndex: nextPair.skipIndex,
focusedLine: null,
focusedLineSide: null,
focusSource: "keyboard",
selectionAnchor: null,
selectionAnchorSide: null,
focusedCommentId: null,
Expand Down Expand Up @@ -2522,6 +2534,7 @@ export class PRReviewStore {
this.set({
focusedLine: nextLine,
focusedLineSide: nextSide,
focusSource: "keyboard",
selectionAnchor: selectionAnchor ?? focusedLine ?? nextLine,
selectionAnchorSide:
selectionAnchorSide ?? focusedLineSide ?? nextSide,
Expand All @@ -2533,6 +2546,7 @@ export class PRReviewStore {
this.set({
focusedLine: nextLine,
focusedLineSide: nextSide,
focusSource: "keyboard",
selectionAnchor: null,
selectionAnchorSide: null,
focusedSkipBlockIndex: null,
Expand Down Expand Up @@ -2629,6 +2643,7 @@ export class PRReviewStore {
this.set({
focusedLine: nextLine,
focusedLineSide: nextSide,
focusSource: "keyboard",
selectionAnchor: selectionAnchor ?? focusedLine ?? nextLine,
selectionAnchorSide: selectionAnchorSide ?? focusedLineSide ?? nextSide,
focusedSkipBlockIndex: null,
Expand All @@ -2639,6 +2654,7 @@ export class PRReviewStore {
this.set({
focusedLine: nextLine,
focusedLineSide: nextSide,
focusSource: "keyboard",
selectionAnchor: null,
selectionAnchorSide: null,
focusedSkipBlockIndex: null,
Expand Down Expand Up @@ -3039,6 +3055,7 @@ export class PRReviewStore {
// Focus the line the comment was on so user can continue with keyboard
focusedLine: commentLine ?? null,
focusedLineSide: commentLine ? "new" : null,
focusSource: "keyboard",
});
this.recomputeCommentRangeLookup();
};
Expand Down Expand Up @@ -3109,6 +3126,7 @@ export class PRReviewStore {
// Focus the line the comment was on so user can continue with keyboard
focusedLine: commentLine ?? null,
focusedLineSide: commentLine ? "new" : null,
focusSource: "keyboard",
});
};

Expand Down Expand Up @@ -3369,6 +3387,7 @@ export class PRReviewStore {
this.set({
focusedLine: end,
focusedLineSide: "new",
focusSource: "keyboard",
selectionAnchor: start,
selectionAnchorSide: "new",
focusedSkipBlockIndex: null,
Expand All @@ -3381,6 +3400,7 @@ export class PRReviewStore {
this.set({
focusedLine: line,
focusedLineSide: "new",
focusSource: "keyboard",
selectionAnchor: null,
selectionAnchorSide: null,
focusedSkipBlockIndex: null,
Expand Down
Loading