Skip to content

Commit ac09374

Browse files
fix(web): fix scroll position for references further down in a file
Uses lineBlockAt() instead of coordsAtPos() to get line positions from CodeMirror's internal height map, which works correctly for virtualized lines that haven't been rendered yet. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent c7ee456 commit ac09374

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

packages/web/src/features/chat/components/chatThread/referencedSourcesListView.tsx

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,6 @@ const ReferencedSourcesListViewComponent = ({
114114
scrollAreaViewport &&
115115
selectedReference.range.startLine <= editorRef.view.state.doc.lines
116116
) {
117-
const view = editorRef.view;
118-
const lineNumber = selectedReference.range.startLine;
119-
120-
const pos = view.state.doc.line(lineNumber).from;
121-
122117
// Expand the file if it's collapsed.
123118
setCollapsedFileIds((collapsedFileIds) => collapsedFileIds.filter((id) => id !== fileId));
124119

@@ -139,15 +134,26 @@ const ReferencedSourcesListViewComponent = ({
139134
behavior: 'instant',
140135
});
141136

137+
const view = editorRef.view;
138+
const lineNumber = selectedReference.range.startLine;
139+
142140
requestAnimationFrame(() => {
143-
const coords = view.coordsAtPos(pos);
144-
if (!coords) {
145-
return;
146-
}
141+
// Get the line's position within the CodeMirror document
142+
const pos = view.state.doc.line(lineNumber).from;
143+
const blockInfo = view.lineBlockAt(pos);
144+
const lineTopInCodeMirror = blockInfo.top;
147145

146+
// Get the bounds of both elements
148147
const viewportRect = scrollAreaViewport.getBoundingClientRect();
149-
const lineTopRelativeToScrollArea = coords.top - viewportRect.top + scrollAreaViewport.scrollTop;
148+
const codeMirrorRect = view.dom.getBoundingClientRect();
149+
150+
// Calculate the line's position relative to the ScrollArea content
151+
const lineTopRelativeToScrollArea = lineTopInCodeMirror + (codeMirrorRect.top - viewportRect.top) + scrollAreaViewport.scrollTop;
152+
153+
// Get the height of the visible ScrollArea
150154
const scrollAreaHeight = scrollAreaViewport.clientHeight;
155+
156+
// Calculate the target scroll position to center the line
151157
const targetScrollTop = lineTopRelativeToScrollArea - (scrollAreaHeight / 3);
152158

153159
scrollAreaViewport.scrollTo({

0 commit comments

Comments
 (0)