Skip to content

Commit 1ea2898

Browse files
fix(web): fix scroll position for references further down in a file (#1036)
* 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> * chore: update CHANGELOG for #1036 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent c7ee456 commit 1ea2898

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Fixed
11+
- Fixed scroll position when selecting chat references that point to lines further down in a file. [#1036](https://github.com/sourcebot-dev/sourcebot/pull/1036)
12+
1013
## [4.16.0] - 2026-03-24
1114

1215
### 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)