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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand All @@ -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({
Expand Down
Loading