diff --git a/CHANGELOG.md b/CHANGELOG.md index e84db7f15..27cdf2e92 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed +- 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) + ## [4.16.0] - 2026-03-24 ### Changed diff --git a/packages/web/src/features/chat/components/chatThread/referencedSourcesListView.tsx b/packages/web/src/features/chat/components/chatThread/referencedSourcesListView.tsx index 8ceecaad0..5f12c6f54 100644 --- a/packages/web/src/features/chat/components/chatThread/referencedSourcesListView.tsx +++ b/packages/web/src/features/chat/components/chatThread/referencedSourcesListView.tsx @@ -114,11 +114,6 @@ const ReferencedSourcesListViewComponent = ({ scrollAreaViewport && selectedReference.range.startLine <= editorRef.view.state.doc.lines ) { - const view = editorRef.view; - const lineNumber = selectedReference.range.startLine; - - const pos = view.state.doc.line(lineNumber).from; - // Expand the file if it's collapsed. setCollapsedFileIds((collapsedFileIds) => collapsedFileIds.filter((id) => id !== fileId)); @@ -139,15 +134,26 @@ const ReferencedSourcesListViewComponent = ({ behavior: 'instant', }); + const view = editorRef.view; + const lineNumber = selectedReference.range.startLine; + requestAnimationFrame(() => { - const coords = view.coordsAtPos(pos); - if (!coords) { - return; - } + // Get the line's position within the CodeMirror document + const pos = view.state.doc.line(lineNumber).from; + const blockInfo = view.lineBlockAt(pos); + const lineTopInCodeMirror = blockInfo.top; + // Get the bounds of both elements const viewportRect = scrollAreaViewport.getBoundingClientRect(); - const lineTopRelativeToScrollArea = coords.top - viewportRect.top + scrollAreaViewport.scrollTop; + const codeMirrorRect = view.dom.getBoundingClientRect(); + + // Calculate the line's position relative to the ScrollArea content + const lineTopRelativeToScrollArea = lineTopInCodeMirror + (codeMirrorRect.top - viewportRect.top) + scrollAreaViewport.scrollTop; + + // Get the height of the visible ScrollArea const scrollAreaHeight = scrollAreaViewport.clientHeight; + + // Calculate the target scroll position to center the line const targetScrollTop = lineTopRelativeToScrollArea - (scrollAreaHeight / 3); scrollAreaViewport.scrollTo({