Skip to content

markdown: stop snapping the editor back to the preview scroll position - #345112

Closed
Sundar Shahi Thakuri (sundarshahi) wants to merge 1 commit into
microsoft:mainfrom
sundarshahi:fix/markdown-editor-scroll-snap
Closed

Sundar Shahi Thakuri (sundarshahi) wants to merge 1 commit into
microsoft:mainfrom
sundarshahi:fix/markdown-editor-scroll-snap

Conversation

@sundarshahi

@sundarshahi Sundar Shahi Thakuri (sundarshahi) commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #317033

Motivation

After the Markdown preview of a file has been scrolled once, every later switch back to that file's text editor scrolls the editor to the preview's last position. Wherever the user had scrolled in the editor is lost on each tab switch, until the window is reloaded.

MarkdownPreviewManager scrolls a markdown text editor to TopmostLineMonitor.getPreviousStaticEditorLineByUri whenever it becomes active. That line is set by StaticMarkdownPreview on every preview scroll, but unlike getPreviousTextEditorLineByUri (the other direction), the getter never removes it. So the handoff meant for "preview → editor" is replayed on every later activation.

Changes made

  • getPreviousStaticEditorLineByUri now removes the entry it returns, the same way getPreviousTextEditorLineByUri does. Opening the text editor from a scrolled preview still lands on the preview's position once. Later tab switches keep the editor's own position.

This is the editor-side half. #342822 addresses the preview-side scroll position (#147718) in preview-src/index.ts and does not touch this code.

Known limit: with a static preview open beside the text editor, the stored line is still applied once. For example, you scroll the preview without focusing it, then scroll the editor elsewhere, switch tabs and come back: the editor jumps to the preview's position one time instead of on every switch. Fully fixing that needs the stored line cleared when the user scrolls the text editor. That depends on the order of the active-editor and visible-range events when an editor opens, which I couldn't check outside the extension host, so I left it out of this PR. I can follow up with it if you want that behaviour.

How to test

  1. Open a long markdown file in the preview (Open With... > Markdown Preview) and scroll down.
  2. Reopen it in the text editor. It opens at the preview's position.
  3. Scroll the text editor somewhere else, switch to another tab, then back.
  4. The editor stays where you scrolled it. Before this change it jumped back to the preview's position.

Validation

  • New test src/test/topmostLineMonitor.test.ts: the stored preview line is returned once, then undefined. It fails with the change reverted (the second read returns 10).
  • The extension's tsconfig.json typechecks with no errors, and eslint passes on both files.
  • I ran the new test locally with mocha against a minimal vscode stub, not in the extension host.

`getPreviousStaticEditorLineByUri` kept the preview's last scroll line, so
every later activation of the markdown text editor scrolled it back to that
line, overwriting where the user had scrolled. Consume the line on read, like
`getPreviousTextEditorLineByUri` does for the other direction, so the preview
to editor handoff happens once.

Fixes microsoft#317033
Copilot AI balanced review requested due to automatic review settings October 30, 2026 01:17

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues were identified for the change making the preview-to-editor scroll handoff apply only once.

No open issues 🎉

What changed in this PR

Fixes #317033 by making the Markdown preview-to-editor scroll handoff apply only once, so later tab switches preserve the editor’s own scroll position. This complements the separate preview-side fix in #342822.

Changes:

  • Remove the stored preview scroll position when it is retrieved.
  • Add a regression test verifying that the position is returned once, then undefined.
File Description
extensions/​markdown-language-features/​src/​test/​topmostLineMonitor.test.ts Tests that the preview scroll position is consumed once.
extensions/​markdown-language-features/​src/​preview/​topmostLineMonitor.ts Clears the stored position on retrieval to prevent repeated editor scrolling.

Review effort: Balanced

@alexdima

Copy link
Copy Markdown
Member

merged via #350010

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Markdown keeps scrolling to a specific position when switching tabs

3 participants