Edits are worthless if you cannot write them back - and the editor has to know when there are unsaved changes. Today you save the buffer's text and track a dirty flag that every edit sets and every save clears.
Write the buffer's text to a writer, and track a dirty flag set by edits and cleared by save and load.
Saving is loading run backwards: flatten the piece table into one string with
Text() and hand those bytes to a writer. The piece table did its clever work
in memory - splitting and trimming pieces so no byte was ever copied while editing -
but a file on disk is a flat sequence, so at save time the pieces are walked once and
concatenated. Writing to a generic writer mirrors the generic reader from loading,
so the whole load-edit-save cycle is exercisable without touching the file system.
Riding along with save is the dirty flag, the single boolean that answers “are
there changes on screen that are not yet on disk?” Every editing operation sets it,
because each one moves the buffer away from what was last saved; save and
load clear it, because afterward the buffer matches a file. It is one bit, but it
drives the [modified] marker on the status line next lesson and the “you have
unsaved changes” prompt before quitting later. The honest work here is being
thorough - every edit method must set the flag, or the editor will cheerfully claim
a changed file is saved.
func (e *Editor) Save(w io.Writer) error {_, err := io.WriteString(w, e.Buf.Text())e.Dirty = false // disk now matches the bufferreturn err}// set e.Dirty = true at the end of every edit (InsertChar, Enter,// Backspace, DeleteForward); set it false in Load too.