Gim/internal/editor/model_builder.go
Hayden Hargreaves 273be90d42 feat: HUGE refactor of colorschemes, untested.
Now we can load them in via JSON files at launch time. They are embded
in the final exe though...
2026-04-08 11:59:49 -07:00

147 lines
4.5 KiB
Go

package editor
import (
"git.gophernest.net/azpect/TextEditor/internal/core"
"git.gophernest.net/azpect/TextEditor/internal/input"
"git.gophernest.net/azpect/TextEditor/internal/syntax"
"git.gophernest.net/azpect/TextEditor/internal/theme"
"git.gophernest.net/azpect/TextEditor/internal/theme/themes"
)
type ModelBuilder struct {
model Model
}
// NewModelBuilder: Builds and returns a new model, using the default color scheme (kanagawa-wave).
func NewModelBuilder() *ModelBuilder {
editorTheme := themes.NewDefaultTheme()
// Embed the themes
var embededThemes map[string]theme.EditorTheme
embededThemesJson, err := theme.LoadEmbeddedThemesJSON()
if err == nil {
embededThemes = theme.MapEmbededThemeToEditorTheme(embededThemesJson)
}
// Always have a default theme
embededThemes["default"] = themes.NewDefaultTheme()
return &ModelBuilder{
model: Model{
buffers: []*core.Buffer{},
windows: []*core.Window{},
activeWindowId: -1,
mode: core.NormalMode,
termWidth: 0,
termHeight: 0,
input: input.NewHandler(),
insertCount: 0,
insertKeys: []string{},
insertAction: nil,
command: "",
commandCursor: 0,
commandOutput: nil,
settings: core.NewDefaultSettings(),
registers: core.DefaultRegisters(),
syntax: syntax.NewTreeSitterEngine(editorTheme),
currentTheme: "default",
themes: embededThemes,
},
}
}
// ModelBuilder.WithBuffers: Set the buffers for the model. Buffers represent
// the in-memory text content of files being edited.
func (mb *ModelBuilder) WithBuffers(buffers []*core.Buffer) *ModelBuilder {
mb.model.buffers = buffers
return mb
}
// ModelBuilder.AddBuffer: Add a single buffer to the model's buffer list.
func (mb *ModelBuilder) AddBuffer(buffer *core.Buffer) *ModelBuilder {
mb.model.buffers = append(mb.model.buffers, buffer)
return mb
}
// ModelBuilder.WithWindows: Set the windows for the model. Windows are viewports
// that display buffer content with their own cursor position and scroll state.
func (mb *ModelBuilder) WithWindows(windows []*core.Window) *ModelBuilder {
mb.model.windows = windows
return mb
}
// ModelBuilder.AddWindow: Add a single window to the model's window list.
func (mb *ModelBuilder) AddWindow(window *core.Window) *ModelBuilder {
mb.model.windows = append(mb.model.windows, window)
return mb
}
// ModelBuilder.WithActiveWindowId: Set the ID of the currently active window.
// This determines which window receives input and displays the cursor.
func (mb *ModelBuilder) WithActiveWindowId(id int) *ModelBuilder {
mb.model.activeWindowId = id
return mb
}
// ModelBuilder.WithMode: Set the editor mode (Normal, Insert, Visual, etc).
func (mb *ModelBuilder) WithMode(mode core.Mode) *ModelBuilder {
mb.model.mode = mode
return mb
}
// ModelBuilder.WithTermSize: Set the terminal dimensions in columns and rows.
func (mb *ModelBuilder) WithTermSize(width, height int) *ModelBuilder {
mb.model.termWidth = width
mb.model.termHeight = height
return mb
}
// ModelBuilder.WithTermWidth: Set the terminal width in columns.
func (mb *ModelBuilder) WithTermWidth(width int) *ModelBuilder {
mb.model.termWidth = width
return mb
}
// ModelBuilder.WithTermHeight: Set the terminal height in rows.
func (mb *ModelBuilder) WithTermHeight(height int) *ModelBuilder {
mb.model.termHeight = height
return mb
}
// ModelBuilder.WithSettings: Set the editor settings (tabstop, scrolloff, etc).
func (mb *ModelBuilder) WithSettings(settings core.EditorSettings) *ModelBuilder {
mb.model.settings = settings
return mb
}
// ModelBuilder.WithRegisters: Set the register map for yank/delete/paste operations.
func (mb *ModelBuilder) WithRegisters(registers map[rune]core.Register) *ModelBuilder {
mb.model.registers = registers
return mb
}
// ModelBuilder.WithCommand: Set the command line text.
func (mb *ModelBuilder) WithCommand(command string) *ModelBuilder {
mb.model.command = command
return mb
}
// ModelBuilder.WithCommandCursor: Set the cursor position in the command line.
func (mb *ModelBuilder) WithCommandCursor(cursor int) *ModelBuilder {
mb.model.commandCursor = cursor
return mb
}
// ModelBuilder.WithCommandOutput: Set the command line output.
func (mb *ModelBuilder) WithCommandOutput(out *core.CommandOutput) *ModelBuilder {
mb.model.commandOutput = out
return mb
}
// ModelBuilder.Build: Build and return the configured Model instance.
func (mb *ModelBuilder) Build() *Model {
m := &mb.model
m.bindBufferSyntaxHooks(m.buffers)
return m
}