Repository navigation
Cut launch-time layout on long-line files (#106) - #107
Open
RobertoMachorro wants to merge 4 commits into
Open
RobertoMachorro wants to merge 4 commits into
RobertoMachorro wants to merge 4 commits into
Conversation
- Line-number gutter: lay out in viewWillDraw() instead of inside its own draw, which is where the #106 and #102 launch hangs were stuck. - Current-line highlight: only read existing layout on caret moves, and lay out only in drawBackground. TextKit 1 lays out a whole paragraph at a time, so measuring the caret's line typeset an entire one-line file on every caret move and once more while loading it. - Status bar: defer the line:column count during view updates before counting, not after, so loading no longer scans the whole file. - Holding Shift at launch skips Reopen Previous.
- Size the line-number gutter when text is set instead of a turn after its first draw. The late resize changed the text view's width, so a wrapped file was laid out twice; on the 5 MB-line test file, gutter-on launch drops from ~8.5 s to ~5.75 s busy. - viewWillDraw lays out again if laying out moved the clip view, so the draw never numbers a band it did not lay out. - caretLineRect no longer takes a layOut flag: drawBackground lays out itself, and only over the visible rect when the dirty rect touches it. - Share the view-to-container rect translation between the gutter and the caret highlight. - Tests: a caret move measures the line it arrives on; setting text sizes the gutter right away; the no-resize-mid-draw test still exercises a stale gutter.
This was referenced Nov 3, 2026
- A resize queued by an edit now measures the width when its turn comes, so it can no longer put back an older document's width after invalidateLineNumbers() sized the gutter for new text. - Size the gutter immediately only before the editor is in a window; a reload during a SwiftUI view update waits a turn like any other resize. - showsLineNumberGutter and editorFont skip unchanged values, so a preference change no longer recounts (and re-attributes) every open document. - The caret band treats a dirty rect that only touches the visible rect as adjacent, so an overdraw strip no longer looks cut off. - containerRect(forViewRect:) uses textContainerOrigin on both axes. - Reword the caret-move comment as an assumption, not a verified fact. - Tests: a queued resize does not undo a newer size; a font change sizes the gutter right away.
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The current-line highlight can extend vertically into blank space or other rows for horizontally clipped, unwrapped lines; both extensions need to be restricted to wrapped lines and covered by a regression test.
1 open issue
What changed in this PR
This PR reduces Moped’s launch-time layout work for long-line files, addressing paths implicated in #106 and #102 without claiming to eliminate AppKit’s remaining layout cost.
Changes:
- Moves gutter layout before drawing and sizes the gutter earlier to avoid redundant wrapped-text layout.
- Measures current-line highlights from existing layout and defers status-bar counting during view updates.
- Adds Shift-at-launch reopening bypass, regression tests, and documentation.
| File | Description |
|---|---|
| MopedEditor/Tests/MopedEditorTests/LineNumberRulerTests.swift | Tests layout timing, immediate gutter sizing, and queued resize handling. |
| MopedEditor/Tests/MopedEditorTests/CurrentLineHighlightTests.swift | Tests highlight geometry and caret moves without forced layout. |
| MopedEditor/Sources/MopedEditor/NSTextView+ContainerRect.swift | Adds view-to-text-container coordinate conversion. |
| MopedEditor/Sources/MopedEditor/MopedTextView+CurrentLine.swift | Computes highlight bands from existing layout. |
| MopedEditor/Sources/MopedEditor/MopedTextView.swift | Skips redundant gutter and font assignments. |
| MopedEditor/Sources/MopedEditor/LineNumberRulerView.swift | Moves layout before drawing and updates gutter sizing. |
| Moped/EditorState.swift | Defers cursor counting before scanning text during view updates. |
| Moped/AppDelegate.swift | Allows Shift at launch to bypass reopening documents. |
| DOCUMENTATION.md | Documents the Shift-launch recovery option. |
| CHANGELOG.md | Records launch optimizations and visible behavior changes. |
Review effort: Balanced
A review asked whether a horizontally clipped, unwrapped line could make the band stretch over neighbouring rows. It cannot: the glyph query returns whole line fragments regardless of the rect's x and width, so the caret line's shown range is only ever cut short vertically. Add a regression test for the three unwrapped cases (clipped end, scrolled right on the long line, scrolled right past a short line) and a comment at the query saying why.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Refs #106 and #102. This removes the stack in the reporter's log and cuts Moped's own share of launch time on files with very long lines. It does not make a multi-megabyte single-line file open fast, because most of that time is now AppKit's own layout.
What changed
ensureLayout(forBoundingRect:)inside its own draw. That re-tiled the scroll view during the Core Animation commit, which is the stack in both issues' logs. Layout now happens inviewWillDraw(), and the draw path only reads existing layout.viewWillDraw()lays out again if laying out moved the clip view, so the draw never numbers a band it didn't lay out.boundingRect(forGlyphRange:). TextKit 1 lays out a whole paragraph at a time, so on a one-line file this typeset the entire file on every caret move. It also did so once while the file loaded, before the view had its final width, so that work was thrown away and done again.drawBackground, where AppKit is about to lay out the same area anyway.updateCursorPositioncounted line:column up to the caret before checking whether to defer. While a file loads, the caret briefly sits at the end of the text, so the count scanned the whole file (about 0.9 s at 15 MB) and then discarded the result. The check now comes first.Measurements
Release builds, wrapping on, 20 s runs. Busy means CPU above 50%.
_ensureLayoutCompleteForVisibleRectTesting
swiftlint --strict: 0 violationsswift test --package-path MopedEditor: 235 tests, 0 failures, none skipped./scripts/check_localized_strings.sh: passesLineNumberRulerTests.testLayoutHappensBeforeTheDrawAndNotInsideItCurrentLineHighlightTests, which covers the band shape through a real draw, checks that a caret move lays nothing out, and checks that it measures the line it arrives onLineNumberRulerTests.testSettingTextSizesTheGutterRightAwayNot verified
NSScrollView tile→_setFrameSize:forceScroll:re-wrap, and the cause isn't known.🤖 Generated with Claude Code