Gim/internal/core/buffer_builder.go
Hayden Hargreaves e362c9f118
All checks were successful
Run Test Suite / test (push) Successful in 56s
feat: gap buffer is implemented, tested
Not sure if this is perfect, but it seems to be working
2026-04-02 12:39:30 -07:00

94 lines
2.8 KiB
Go

package core
// Not great, but maybe the best way
var CurrentBufferId int = 1
type BufferBuilder struct {
buffer Buffer
}
// NewBufferBuilder: Creates a new buffer builder. The buffer builder implements a
// builder pattern to create a buffer with the defined properties and values.
func NewBufferBuilder() *BufferBuilder {
return &BufferBuilder{
buffer: Buffer{
Id: 0, // This is set when built
Type: ScatchBuffer, // Default buffer type
Filename: "",
Filetype: "",
Lines: []*GapBuffer{NewEmptyGapBuffer()},
Modified: false,
Loaded: false,
Listed: false,
ReadOnly: false,
UndoStack: NewUndoStack(), // Empty undo stack
},
}
}
// BufferBuilder.WithFilename: Attaches a file name to the buffer that is being built.
func (b *BufferBuilder) WithFilename(filename string) *BufferBuilder {
b.buffer.Filename = filename
return b
}
// BufferBuilder.WithFiletype: Attaches a file type to the buffer that is being built.
func (b *BufferBuilder) WithFiletype(filetype string) *BufferBuilder {
b.buffer.Filetype = filetype
return b
}
// BufferBuilder.WithLines: Attaches a lines to the buffer that is being built.
func (b *BufferBuilder) WithLines(lines []string) *BufferBuilder {
b.buffer.Lines = make([]*GapBuffer, len(lines))
for i, line := range lines {
b.buffer.Lines[i] = NewGapBuffer(line)
}
return b
}
// BufferBuilder.Modified: Sets the modified flag of the buffer being built. By default,
// buffers are built with the modified flag being false.
func (b *BufferBuilder) Modified() *BufferBuilder {
b.buffer.Modified = true
return b
}
// BufferBuilder.Loaded: Sets the loaded flag of the buffer being built. By default,
// buffers are built with the loaded flag being false.
func (b *BufferBuilder) Loaded() *BufferBuilder {
b.buffer.Loaded = true
return b
}
// BufferBuilder.Listed: Sets the listed flag of the buffer being built. By default,
// buffers are built with the listed flag being false.
func (b *BufferBuilder) Listed() *BufferBuilder {
b.buffer.Listed = true
return b
}
// BufferBuilder.ReadOnly: Sets the readonly flag of the buffer being built. By default,
// buffers are built with the readonly flag being false.
func (b *BufferBuilder) ReadOnly() *BufferBuilder {
b.buffer.ReadOnly = true
return b
}
// BufferBuilder.Listed: Sets the type of the buffer being built. By default, buffers
// are build with the ScatchBuffer type.
func (b *BufferBuilder) WithType(t BufferType) *BufferBuilder {
b.buffer.Type = t
return b
}
// BufferBuilder.Build: Build the final buffer and return it to the caller. Final
// step in the process. This is where the ID is set, so many buffers can be "in-progress"
// but the ID will be set when they are built. Meaning, this is not thread safe.
func (b *BufferBuilder) Build() Buffer {
b.buffer.Id = CurrentBufferId
CurrentBufferId++
return b.buffer
}