Skip to content

fix(muya): number empty lines in code blocks (#5294) - #5295

Merged
Jocs merged 3 commits into
developfrom
fix/codeblock-line-numbers-empty-lines-5294
Sep 10, 2026
Merged

Jocs merged 3 commits into
developfrom
fix/codeblock-line-numbers-empty-lines-5294

Conversation

@Jocs

@Jocs Jocs commented Sep 10, 2026

Copy link
Copy Markdown
Member

Summary

Fixes #5294.

With code block line numbers on, an empty code line's number was drawn above the block, below it, or on another line's row, so empty lines looked unnumbered.

Root cause

repositionLineNumberSpans places each gutter number at the top of a collapsed Range at the start of its line. On an empty line that caret position has no client rects in Chromium, so getBoundingClientRect() is all zeros. The number then got top = 0 - firstLineTop (about −99px in the e2e host). When the first line itself was empty, the origin became 0 and the following numbers moved below the block.

The line after a final newline has no character to measure. It was stacked one line-height below the previous number, which puts it on the wrong row when the previous line wraps.

Fix

  • Measure an empty line with a range over its own \n. In Chromium that range has a real rect with the same top and height as a caret on a non-empty line, so the first-line origin still cancels the line-box leading.
  • Measure the line after a final newline from the .mu-trailing-break that CodeBlockContent renders there since [Bug] code block 中换行不能准确表现 #5114. Line-height stacking now only covers lines with nothing to measure, such as an empty code block.
  • A preceding refactor commit moves the measuring walk into measureLineTops, with no behavior change, so the fix keeps repositionLineNumberSpans under the ESLint complexity limit.

Tests

  • New e2e blocks/codeblock-line-numbers-empty-line-5294.spec.ts checks which row each number sits on. It covers an empty line between lines, empty lines at the end and at the top, a syntax-highlighted block, and wrapped code (an empty line after a wrapped line, loaded and typed). All 6 fail on develop and on the refactor commit with the same values, and pass with the fix.
  • codeblock-trailing-newline-5114, options/code-font (gutter re-measure) and codeblock-scroll-3191 e2e still pass. The codeBlockLineNumbers and lineNumbersGutter unit specs pass, and tsc --noEmit, ESLint and madge are clean.

Checked in Chromium only, which is what Electron and the muya e2e CI use.

🤖 Generated with Claude Code

https://claude.ai/code/session_014KtYifHmaETtxfX2ktbdYC

Jocs and others added 3 commits September 10, 2026 21:59
repositionLineNumberSpans walked the code's text nodes and positioned each
gutter span inside the same loop. Move the walk into measureLineTops, which
returns the viewport top of each line, and leave repositionLineNumberSpans
to place the spans relative to the first line and fill in the lines that
could not be measured.

No behavior change. It leaves room to fix #5294 without pushing the
function past the ESLint complexity limit.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_014KtYifHmaETtxfX2ktbdYC
With code block line numbers on, an empty line's number was drawn above
the block, below it, or on another line's row.

Each number is placed at the top of a collapsed range at the start of its
line. On an empty line that caret position has no client rects, so
Chromium reports an all-zero rect and the number landed at minus the first
line's top. Measure an empty line with a range over its own newline
instead: it has a real rect with the same text-box metrics.

The line after a final newline has no character to measure and was stacked
one line-height under the previous number, which is the wrong row when the
previous line wraps. Measure it from the trailing break CodeBlockContent
renders there (#5114).

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_014KtYifHmaETtxfX2ktbdYC
Checks, in real Chromium, which row each gutter number sits on: an empty
line typed between lines, at the end and at the top of a block, a
syntax-highlighted block, and wrapped code with an empty line after a
wrapped line. All six fail on develop.

Adds a lineNumber selector for the gutter spans.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_014KtYifHmaETtxfX2ktbdYC
@Jocs
Jocs merged commit bc3e487 into develop Sep 10, 2026
7 checks passed
@Jocs
Jocs deleted the fix/codeblock-line-numbers-empty-lines-5294 branch September 10, 2026 14:18
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.

[Bug] Code block line numbers are missing on empty lines

1 participant