build-a-text-editor / lesson-01.md
Lesson 01 · The piece-table buffer

An empty buffer that holds text

Every editor is built around one thing - a buffer that holds the text you are editing. Today you create the smallest possible buffer so the surface the whole project grows on exists from day one.

The goal

Create a buffer from some initial text whose Text and Len report exactly what it holds.

Start here - the target
TO DO
Scenario: Reading a buffer's contents and length
Givena buffer created from the text "hello"
Whenits Text and Len are read
ThenText() is "hello" and Len() is 5
Anda buffer created from "" has Text() "" and Len() 0
Background

A text editor is, underneath everything, a thing that holds a sequence of characters and lets you change it. Before cursors, screens, or files matter, that sequence has to exist as something you can construct and read back. Today the buffer just wraps the original text it was created with, and Text() hands it straight back.

Keeping it this simple pins the contract the rest of the project leans on: a buffer is created from some starting text, Text() returns the whole thing, and Len() reports how many bytes it holds. The clever internal representation - the piece table that makes edits fast and exact - arrives next lesson, but its public face is exactly this: give me the text, tell me how long it is.

Make it work
// the whole editor grows around this type
type Buffer struct { original string }
func New(text string) *Buffer { return &Buffer{original: text} }
func (b *Buffer) Text() string { return b.original }
func (b *Buffer) Len() int { return len(b.original) }
CheckpointDONE
You have a buffer you can construct and read back. Commit and stop here.