build-a-text-editor / lesson-15.md
Lesson 15 · Editing at the cursor

Typing a character

Now the editor earns its name - you type. Today you insert a character at the cursor and advance the cursor past it, the operation behind every keystroke that adds text.

The goal

Insert a character at the cursor's offset and move the cursor one column to the right.

Start here - the target
TO DO
Scenario: Inserting a character at the cursor
Givenan editor over "ac" with the cursor at Row 0, Col 1
WhenInsertChar('b') is called
ThenText() is "abc" and the cursor is at Row 0, Col 2
Andstarting from an empty buffer at (0,0), InsertChar('h') then InsertChar('i') gives Text() "hi" with the cursor at Col 2
Background

This is the operation the whole editor exists for: put a character where the cursor is and move the cursor past it. It is built entirely from pieces you already have - Offset() turns the cursor’s (row, col) into the buffer position, Buf.Insert splits a piece to drop the character in, and advancing Col by one leaves the cursor sitting just after what you typed, ready for the next keystroke.

That the cursor advances is what makes typing feel right - characters flow left to right as you enter them, each new one appearing at the cursor and pushing it along. Because the buffer’s insert is exact and the offset translation is exact, typing h then i into an empty buffer yields precisely "hi" with the cursor at column 2. Every key you press for the rest of the project routes through some variation of this: locate the offset, change the buffer, move the cursor.

Make it work
func (e *Editor) InsertChar(c byte) {
e.Buf.Insert(e.Offset(), string(c)) // insert at the cursor
e.Col++ // step past what you typed
}
CheckpointDONE
You can type characters into the buffer. Commit and stop here.