MarkdownViewer sometimes lands far from the anchor after `go("doc.md#heading")` when the table of contents is shown
まだ誰も着手していません。
評価
調査の方向性
Start with the repro script in the issue, run it with the table of contents shown and then with --no-toc to confirm the timing dependence. The relevant code is Markdown.load and goto_anchor in the Markdown widget, plus the table of contents Tree sizing on idle. Done means the heading lands at the top consistently across repeated runs, with the test suite still passing.
索引モデルが issue の本文から書いたものです。
説明
Hi! I hit this while writing generated tests against Textual's widgets.
After MarkdownViewer.go("doc.md#target") (or a link to doc.md#target), the heading usually ends up at the top of the viewer. But in a few runs out of a hundred it ends up 50 or more rows lower, off screen. It only happens with the table of contents shown.
import asyncio
import os
import sys
import tempfile
from pathlib import Path
import textual
from textual.app import App, ComposeResult
from textual.widgets import MarkdownViewer
RUNS = int(sys.argv[1]) if len(sys.argv) > 1 else 50
SHOW_TOC = "--no-toc" not in sys.argv
PARAGRAPH = " ".join(["Some words that wrap differently at different widths."] * 6)
DOCUMENT = "\n\n".join(
f"## Section {n} with a fairly long heading title\n\n{PARAGRAPH}" for n in range(12)
) + "\n\n## Target\n\nThe target.\n\n" + "\n\n".join([PARAGRAPH] * 10)
class ViewerApp(App):
def compose(self) -> ComposeResult:
yield MarkdownViewer(show_table_of_contents=SHOW_TOC)
async def run_once() -> int:
app = ViewerApp()
async with app.run_test(size=(100, 30)) as pilot:
viewer = app.query_one(MarkdownViewer)
await viewer.go("doc.md#target")
await pilot.pause(0.5)
heading = viewer.query_one("#" + viewer.document._table_of_contents[-1][2])
# Rows between the top of the viewer and the target heading (2 when the heading is at the top).
return heading.region.y - viewer.region.y
os.chdir(tempfile.mkdtemp())
Path("doc.md").write_text(DOCUMENT)
print("Textual", textual.__version__)
print("table of contents:", "shown" if SHOW_TOC else "hidden")
results: dict[int, int] = {}
sequence = []
for _ in range(RUNS):
offset = asyncio.run(run_once())
sequence.append(offset)
results[offset] = results.get(offset, 0) + 1
print("per run:", sequence)
for offset, count in sorted(results.items()):
print(f"target heading {offset} rows from the top: {count} of {RUNS}")
python t17.py 200, then python t17.py 100 --no-toc, on Textual 8.2.7:
Textual 8.2.7
table of contents: shown
target heading 2 rows from the top: 190 of 200
target heading 50 rows from the top: 10 of 200
Textual 8.2.7
table of contents: hidden
target heading 2 rows from the top: 100 of 100
and on main at 5e5b7ef:
Textual 8.2.8
table of contents: shown
target heading 2 rows from the top: 191 of 200
target heading 15 rows from the top: 1 of 200
target heading 50 rows from the top: 7 of 200
target heading 146 rows from the top: 1 of 200
Textual 8.2.8
table of contents: hidden
target heading 2 rows from the top: 100 of 100
"2 rows" is the normal result (the heading's top margin). In a bad run, viewer.scroll_y is 96 while the heading's virtual_region.y is 146 and the table of contents is 51 cells wide; in a good run scroll_y is 144.
As far as I can tell, this is what happens. Markdown.load calls goto_anchor, whose scroll_visible(top=True) waits for the screen's next refresh. At the same time, the table of contents is rebuilt, and its Tree (width: auto) works out its lines on idle. If the scroll is taken before the table of contents has widened, the document is still wider and wraps less, so the heading is higher. Then the table of contents widens, the paragraphs above the heading wrap onto more lines, and the heading moves down by about 50 rows. Which comes first depends on timing (in my runs, bad results were more common while the machine was busy).
I'm not sure what the best fix is: scroll to the anchor once the layout has settled after a load, or have the viewer re-run the anchor scroll when the table of contents changes size soon after a load. I'm happy to try either if you have a preference.
- 主要言語
- Python
- スター
- 37.7k
- フォーク
- 1.5k
- PR マージ指標
- 30日以内にマージされた PR はありません
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
Textualize/textual のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
Textualize/textual#6790 · コメント 2 件 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 80/100
Textualize/textual#6789 · コメント 1 件 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
Textualize/textual#6787 · コメント 1 件 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
Textualize/textual#6782 · コメント 1 件 ·
-
難易度 1/5 1時間未満 初心者へのやさしさ 85/100
Textualize/textual#6778 · コメント 1 件 ·
Textualize/textual の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
mohanraj00/entelos#200 ·
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 68/100
-
sweep
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
bketelsen/truenas-mcp#77 ·
メンテナーはふだん 1 日以内に返信
-
Console writes URL credentials to the local log, though AGENTS.md says secrets never reach diskオープン
難易度 1/5 1時間未満 初心者へのやさしさ 86/100
sandbox-quantum/switch#2163 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
openedx/xblock-lti-consumer#765 ·
メンテナーはふだん 1 日以内に返信