Undo/dirty correctness from the stress-testing review

- Redoing an insert and undoing a delete put the cursor at col + size
  on the start row even when the text spans lines, i.e. past the end of
  the line. Later edits and their undo records then went to the wrong
  place (a join recorded at column 2 of 'a' re-split the text wrongly)
  and delete-word-prev read past the line (heap-buffer-overflow under
  ASan). The cursor now goes after the text's last newline, and undo and
  redo clamp it to the buffer.
- A recovered buffer stays modified in every undo state
  (mark_base_unsaved): one edit and its undo made it look clean, and
  closing it then discarded the recovered text and its journal.
- Save-and-quit records the saved undo state.
- Visual-line selections are clamped to the buffer when read; the
  visual newline counts the cursor's own line only if it was selected.
  A stale selection put the cursor rows past the end, where typed text
  could not be undone.
- The yank size cap accounts for visual-line yanks (once per line).
- After an exception escapes a command, open undo groups are closed so
  later edits do not join them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015rcVQiiWyTzoNrsvLSaAfB
This commit is contained in:
Claude
2026-09-24 09:16:04 -07:00
committed by Kyle Isom
parent 58706489bb
commit 4dbefcc41d
7 changed files with 213 additions and 12 deletions
+18 -2
View File
@@ -482,15 +482,22 @@ public:
}
// The selected rows, clamped to the buffer: they are stored as row numbers
// and go stale when lines are removed, and commands must never act on rows
// past the end (edits there were misplaced and could not be undone).
[[nodiscard]] std::size_t VisualLineStartY() const
{
return visual_line_anchor_y_ < visual_line_active_y_ ? visual_line_anchor_y_ : visual_line_active_y_;
const std::size_t y = visual_line_anchor_y_ < visual_line_active_y_ ? visual_line_anchor_y_
: visual_line_active_y_;
return clamp_row_(y);
}
[[nodiscard]] std::size_t VisualLineEndY() const
{
return visual_line_anchor_y_ < visual_line_active_y_ ? visual_line_active_y_ : visual_line_anchor_y_;
const std::size_t y = visual_line_anchor_y_ < visual_line_active_y_ ? visual_line_active_y_
: visual_line_anchor_y_;
return clamp_row_(y);
}
@@ -513,6 +520,15 @@ public:
[[nodiscard]] std::string AsString() const;
private:
[[nodiscard]] std::size_t clamp_row_(std::size_t y) const
{
const std::size_t n = Nrows();
return (n == 0) ? 0 : (y < n ? y : n - 1);
}
public:
// Syntax highlighting integration (per-buffer)
[[nodiscard]] std::uint64_t Version() const
{