Skip to content

Measure the line grid with the strut the lines are laid out with - #122

Open
koldoon wants to merge 1 commit into
reqable:mainfrom
koldoon:fix/line-height-grid
Open

Measure the line grid with the strut the lines are laid out with#122
koldoon wants to merge 1 commit into
reqable:mainfrom
koldoon:fix/line-height-grid

Conversation

@koldoon

@koldoon koldoon commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

What this fixes

_preferredLineHeight is the grid the editor places lines on. It is measured by
a TextPainter without the strut, while paragraphs are laid out with
forceStrutHeight: true. Those are different numbers — for Consolas at 13.09
logical pixels, 21 against 18 — so lines are spaced three points further apart
than the height they are actually given.

Two visible consequences, both of which our users reported:

  • Selection looks striped. Dragged across several lines, the boxes cannot
    reach each other across the gap. An empty line makes it plain: getRangeRects
    returns a box of a full preferredLineHeight for it, so the distance between
    highlighted lines changes depending on whether a line has any text on it.
  • Scrolling bounces. The three points per line accumulate into the
    correction the layout applies to the scroll offset (correctBy in
    _updateDisplayRenderParagraphs). Paging up pushed the offset above the top
    of the document — we measured −75 — and the physics sprang it back.

The change

Two places measure that grid, and both now use the same strut as the paragraphs:

  • _CodeParagraphProvider.updateBaseStyle
  • _CodeFieldRender._calculatePreferredLineHeight

Selection boxes are snapped to that same grid, so adjacent boxes tile exactly.

Notes

  • The grid becomes the height a line really occupies, which for most fonts is
    smaller than before at the same fontHeight. Anyone who wants the previous
    spacing can raise CodeEditorStyle.fontHeight; we did.
  • Verified on macOS with Consolas and Ubuntu at several sizes: the grid step and
    the paragraph line height agree exactly, and glyphs stay inside the row.

Found while building a file manager on re_editor. Happy to adjust anything.


Related: #123 — the three points per line this PR removes are exactly what accumulates into the correction that pushes the scroll offset out of range there. Either PR stands on its own; together they make the bounce impossible rather than merely survivable.

`_preferredLineHeight` is the grid the editor places lines on. It was
measured by a `TextPainter` without the strut, while paragraphs are laid out
with `forceStrutHeight: true`. Those are different numbers — for Consolas at
13.09 logical pixels, 21 against 18 — so lines were spaced three points
further apart than the height they were actually given.

Two visible consequences:

* a selection dragged across several lines looked striped: the boxes could
  not reach each other across the gap;
* the three points per line accumulated into the correction the layout
  applies to the scroll offset, pushing the offset above the top of the
  document. The physics sprang it back, so scrolling up by a page ended in a
  bounce.

Two places measure that grid — `_CodeParagraphProvider.updateBaseStyle` and
`_CodeFieldRender._calculatePreferredLineHeight` — and both now use the same
strut as the paragraphs.

Selection boxes are snapped to the grid as well. `BoxHeightStyle.max` measures
the glyph runs, which are shorter than the line; an empty line made the
difference plain, since `getRangeRects` already returns a box of a full
`preferredLineHeight` for it.
@MegatronKing

Copy link
Copy Markdown
Member

Thank you for your PR. The pubspec.lock file is also included in the commit and needs to be removed.

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.

2 participants