Skip to content

Reload from disk as one undoable edit, keeping the caret (#1279) - #1286

Merged
buggins merged 1 commit into
masterfrom
reload-1279
Nov 27, 2026
Merged

buggins merged 1 commit into
masterfrom
reload-1279

Conversation

@buggins

@buggins buggins commented Nov 27, 2026

Copy link
Copy Markdown
Owner

Fixes #1279 (part of #1274, phase 2 of #783)

Reloading after an external change used to mean load(): the undo history was cleared and the caret jumped to the start. As decided by the owner, a reload is now one undo step: the history stays valid, the selection is cleared, and the caret is kept, clamped to the new text.

EditableContent.reload(source)

  • Reads the file again (the stable read from Atomic load: capture the file state with the content from one read #1276; files over maxInMemoryLoadSize are decoded while read).
  • Replaces only the lines between the common first and last lines (reloadReplacement()), so bookmarks and breakpoints on unchanged lines stay with their lines.
  • The replacement is one undoable edit with the new action EditAction.Reload:
    • undo restores the text from before the reload, including unsaved edits, then the earlier edits;
    • typing is never merged into a reload step.
  • Afterwards the content is saved, with the new fileState, format and invalid-character data. Listeners see all of this already in the reload notification, so there's no modified/unmodified flicker. Undoing the reload makes the content modified.
  • Works for read-only content.
  • Returns false and leaves the text unchanged when the content didn't come from a file, or the file can't be read or decoded.
  • The undo history keeps the replaced lines, like any large edit (documented).

Editors

For Reload, and for its undo and redo, from any source:

  • the selection is cleared;
  • the caret stays where it was, clamped to the row count and the row's length;
  • the scroll position stays, and smart indent doesn't run.

Line icons on whole-line edits (also normal editing)

LineIcons.updateLinePositions() treated the line a range ends at (column 0) as removed. Selecting lines 2–3 up to the start of line 4 and deleting them dropped line 4's bookmark or breakpoint, while line 2's stayed and ended up on the old line 4. Pasting whole lines at a line start left that line's icon on the first inserted line.

For edits whose positions are all at column 0:

  • that line now moves with the following lines;
  • icons on removed lines are removed;
  • inserted lines push the icons down.

Other edits behave as before.

Also

  • load(InputStream) and reload() share decodeText().
  • performOperation()'s replace is factored into applyReplace().

Tests

  • reloadReplacement(), 12 cases (middle, either end, empty before or after, repeated lines). Each is applied, undone and redone with a bookmark on every old line: the unchanged first and last lines keep theirs at their new positions, and no line gets two. Fails with the old updateLinePositions().
  • reload():
    • text, modified state and file state after the reload; listeners see the saved and new file state in the reload notification;
    • a bookmark moving with its line;
    • typing afterwards as its own undo step;
    • undo and redo through the reload and the history before it;
    • no merge into the reload step;
    • unchanged content, another encoding (UTF-16), a large file, read-only content, a deleted file, content without a file.
  • Mutations that undo the icon, merge and notification-order fixes each fail the tests.

Checked

  • dub test with LDC 1.36 (37 modules), DMD 2.107 (37), the -preview=dip1000 build (37), -c console (34), and on Windows (38). D-Scanner clean.
  • GUI on Windows, with a temporary uncommitted hook in example1: an EditBox with the caret at 30:20 and a selection 28:5–30:20; the file is shortened to 35 short lines and reloaded:
Step Caret Selection Lines Modified
Before 30:20 28:5–30:20 40 no
After reload 30:8 (end of the shorter line) none 35 no
After undo 30:8 none 40 yes
After redo 30:8 none 35 no

modifiedStateChange fired only for the undo and the redo. The screenshots match, and there were no leak lines at exit.

🤖 Generated with Claude Code

EditableContent.reload() reads the file again (the stable read of
#1276; large files decoded while read) and replaces only the lines
between the common first and last lines (reloadReplacement) as one
undoable edit with the new action EditAction.Reload. The undo history
stays: undo restores the text from before the reload, then earlier
edits. The content is then saved, with the new fileState, format and
invalid characters, already when listeners get the reload edit (no
modified flicker); undoing it makes the content modified. Read-only
content reloads too. Typing is never merged into a reload step.

Editors handle Reload (also its undo and redo) by clearing the
selection and keeping the caret, clamped to the new text; the scroll
position stays, no smart indent.

LineIcons.updateLinePositions() handles whole-line edits (all
positions at column 0): the line a range ends at moves with the
following lines instead of losing its icon, icons on removed lines are
removed, inserted lines push the icons down. This also fixes deleting
or pasting whole lines in normal editing.

load(InputStream) and reload share decodeText().

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@buggins buggins self-assigned this Nov 27, 2026
@buggins
buggins marked this pull request as ready for review November 27, 2026 22:25
@buggins
buggins merged commit 93f8a5a into master Nov 27, 2026
13 checks passed
@buggins
buggins deleted the reload-1279 branch November 27, 2026 22:36
@buggins buggins mentioned this pull request Nov 27, 2026
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.

Reload from disk keeping the caret, selection, scroll position and undo history

1 participant