Skip to content

Markdown editor loses scroll position when switching tabs #329625

Open

Agent suggestions

Public preview

Description

Type: Bug

Description

While editing a long Markdown (.md) document and switching between it and source code files (such as .py), the Markdown editor does not preserve its scroll position.

Simply switching tabs causes the Markdown document to scroll to a different location. This behavior makes editing large documentation files difficult because the current reading/editing position is lost.

The issue occurs regardless of whether the Markdown Preview is open.

The problem does not occur with source code editors. Switching between source code files preserves the scroll position correctly. The Markdown editor should behave the same way.

Steps to Reproduce

  1. Open a long Markdown (.md) file.
  2. Scroll somewhere in the middle of the document.
  3. Open a source code file (for example, a Python .py file).
  4. Switch back to the Markdown tab.

Expected Behavior

The Markdown editor should preserve its exact scroll position when switching between editor tabs.

Actual Behavior

The Markdown editor scrolls to a different position after switching tabs. The amount and direction of the scroll appear inconsistent and unpredictable.

Additional Information

  • The issue occurs only with the Markdown editor.
  • Opening or closing the Markdown Preview does not affect the behavior.
  • The problem occurs during normal editor tab switching without making any changes to the document.
  • The issue is most noticeable with long Markdown documentation files.

VS Code version: Code 1.132.0 (df53daa, 2026-08-04T15:30:20Z)
OS version: Windows_NT x64 10.0.19045
Modes:

System Info
Item Value
CPUs Intel(R) Xeon(R) W-2123 CPU @ 3.60GHz (8 x 3600)
GPU Status 2d_canvas: enabled
GPU0: VENDOR= 0x10de, DEVICE=0x1e07 [NVIDIA GeForce RTX 2080 Ti], DRIVER_VENDOR=NVIDIA, DRIVER_VERSION=32.0.15.9144 ACTIVE
GPU1: VENDOR= 0x1414, DEVICE=0x008c [Microsoft Basic Render Driver], DRIVER_VERSION=10.0.19041.5794
Machine model name:
Machine model version:
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: enabled
multiple_raster_threads: enabled_on
opengl: enabled_on
rasterization: enabled
raw_draw: disabled_off_ok
skia_graphite: disabled_off
trees_in_viz: disabled_off
video_decode: enabled
video_encode: enabled
webgl: enabled
webgpu: enabled
webnn: disabled_off
Load (avg) undefined
Memory (System) 127.73GB (98.65GB free)
Process Argv --crash-reporter-id 04e4fb0a-b13c-4d31-8fe3-e262850da13c
Screen Reader no
VM 0%
Extensions (7)
Name Identifier Author Version
Ruff charliermarsh.ruff charliermarsh 2026.68.0
Blender Development jacqueslucke.blender-development JacquesLucke 0.0.31
Python Debugger ms-python.debugpy ms-python 2026.6.0
Python ms-python.python ms-python 2026.4.0
Pylance ms-python.vscode-pylance ms-python 2026.3.1
Python Environments ms-python.vscode-python-envs ms-python 1.36.0
YAML redhat.vscode-yaml redhat 1.24.0

Activity

  1. ulugbekna commented on Aug 12, 2026

    @ulugbekna
    Contributor

    Investigation findings

    I reproduced this against the reporter's exact VS Code commit, df53daabb18cd157bdb08c7f01c34df936cf12f4, using the official Darwin arm64 build and a 2,598-line Markdown file. The underlying defect is in retained custom-editor webview layout, so it is not specific to Markdown Preview.

    Confirmed root cause

    The Markdown custom editor is registered with retainContextWhenHidden: true. When its tab is hidden, VS Code keeps the webview alive but its CSS-anchor-sized overlay loses the active editor anchor and falls back to the browser's default iframe dimensions, 300x150.

    That temporary viewport change:

    1. reflows the long Markdown document;
    2. changes the inner #editor scroll container's scrollTop;
    3. dispatches native scroll events;
    4. causes the Markdown webview to persist the transient offset through acquireVsCodeApi().setState().

    The Markdown webview's document.visibilityState remains visible during this CSS visibility: hidden transition, so its visibilitychange handling does not detect ordinary editor-tab switching. Scroll restoration only runs when the webview document is created, not when a retained webview is revealed.

    Relevant code:

    Measurements from the exact build

    I set scrollTop = 12000, switched from the Markdown custom editor to a TypeScript editor in the same group, then switched back:

    Stage Inner viewport Document height scrollTop
    Markdown active 844x675 60,905 12,000
    Text tab active; retained webview hidden 300x150 157,287 31,866.5
    First visible reveal sample Outer iframe 844x675, inner still 300x150 157,327 31,906.5
    Settled roughly 150ms later 844x675 60,905 12,000

    For this static document on macOS, Chromium eventually reversed the reflow and returned to the original offset. However, VS Code first exposed the webview while its inner document was still at the wrong size and position, producing the visible jump. The amount varies with scroll location and document wrapping, matching the reported inconsistent behavior. Dynamic layout, timing, and the Windows focus path can make the transient value persist.

    Causal validation

    As a control, I froze the retained overlay at its last active 844x675 size while hidden. Across the same tab switch:

    • the inner viewport stayed 844x675;
    • document height stayed 60,905;
    • scrollTop stayed exactly 12,000;
    • the jump disappeared.

    This confirms the hidden 300x150 reflow is the primary cause.

    Related core issue

    This is the scroll-state consequence of:

    #326482 addresses revealing a retained iframe before a connected anchor is bound. Its regression coverage should inspect the inner webview viewport and scroll state, not only the outer iframe rectangle: in this trace the outer iframe was already 844x675 while the inner document was still laid out at 300x150.

    Recommended fix

    1. In shared webview infrastructure, preserve the last resolved viewport dimensions while a retained webview is hidden, or otherwise prevent the iframe from laying out at 300x150.
    2. In the Markdown editor, explicitly track webviewPanel.visible, suppress persistence of layout-induced scroll events while hidden/revealing, and restore the last stable visible position after dimensions settle.
    3. Consider persisting a topmost source anchor/offset in addition to raw pixels so view state remains stable across legitimate reflow.
    4. Add a cross-platform regression test that repeatedly switches between a long Markdown custom editor and a text editor and asserts both the topmost content and final scrollTop remain unchanged.

    There may also be a secondary Windows-specific focus contribution related to #324758: VS Code restores webview focus through contentWindow.focus(), while directly focusing the large EditContext-bearing Markdown root without preventScroll can move a native scroll container. The 300x150 reflow above is directly confirmed; the focus contribution still needs an isolated Windows run.

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

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions