fix: removed chroma from project

This commit is contained in:
Hayden Hargreaves 2026-04-07 10:35:04 -07:00
parent 16d1318c22
commit 7c15f41ab1
7 changed files with 77 additions and 267 deletions

6
go.mod
View File

@ -3,12 +3,11 @@ module git.gophernest.net/azpect/TextEditor
go 1.25.5
require (
github.com/alecthomas/chroma/v2 v2.23.1
github.com/charmbracelet/bubbletea v1.3.10
github.com/charmbracelet/lipgloss v1.1.0
github.com/charmbracelet/x/exp/teatest v0.0.0-20260209132835-6b065b8ba62c
github.com/tree-sitter/go-tree-sitter v0.25.0
github.com/tree-sitter/tree-sitter-javascript v0.25.0
github.com/tree-sitter/tree-sitter-go v0.25.0
)
require (
@ -22,7 +21,6 @@ require (
github.com/clipperhouse/displaywidth v0.9.0 // indirect
github.com/clipperhouse/stringish v0.1.1 // indirect
github.com/clipperhouse/uax29/v2 v2.5.0 // indirect
github.com/dlclark/regexp2 v1.11.5 // indirect
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
github.com/lucasb-eyer/go-colorful v1.3.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
@ -33,7 +31,7 @@ require (
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/muesli/termenv v0.16.0 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/tree-sitter/tree-sitter-go v0.25.0 // indirect
github.com/tree-sitter/tree-sitter-javascript v0.25.0 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
golang.org/x/sys v0.38.0 // indirect
golang.org/x/text v0.28.0 // indirect

10
go.sum
View File

@ -1,9 +1,3 @@
github.com/alecthomas/assert/v2 v2.11.0 h1:2Q9r3ki8+JYXvGsDyBXwH3LcJ+WK5D0gc5E8vS6K3D0=
github.com/alecthomas/assert/v2 v2.11.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k=
github.com/alecthomas/chroma/v2 v2.23.1 h1:nv2AVZdTyClGbVQkIzlDm/rnhk1E9bU9nXwmZ/Vk/iY=
github.com/alecthomas/chroma/v2 v2.23.1/go.mod h1:NqVhfBR0lte5Ouh3DcthuUCTUpDC9cxBOfyMbMQPs3o=
github.com/alecthomas/repr v0.5.2 h1:SU73FTI9D1P5UNtvseffFSGmdNci/O6RsqzeXJtP0Qs=
github.com/alecthomas/repr v0.5.2/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
github.com/aymanbagabas/go-udiff v0.3.1 h1:LV+qyBQ2pqe0u42ZsUEtPiCaUoqgA9gYRDs3vj1nolY=
@ -32,12 +26,8 @@ github.com/clipperhouse/uax29/v2 v2.5.0 h1:x7T0T4eTHDONxFJsL94uKNKPHrclyFI0lm7+w
github.com/clipperhouse/uax29/v2 v2.5.0/go.mod h1:Wn1g7MK6OoeDT0vL+Q0SQLDz/KpfsVRgg6W7ihQeh4g=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dlclark/regexp2 v1.11.5 h1:Q/sSnsKerHeCkc/jSTNq1oCm7KiVgUMZRDUoRu0JQZQ=
github.com/dlclark/regexp2 v1.11.5/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4=
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/lucasb-eyer/go-colorful v1.3.0 h1:2/yBRLdWBZKrf7gB40FoiKfAWYQ0lqNcbuQwVHXptag=
github.com/lucasb-eyer/go-colorful v1.3.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=

View File

@ -13,7 +13,6 @@ import (
"git.gophernest.net/azpect/TextEditor/internal/action"
"git.gophernest.net/azpect/TextEditor/internal/core"
"git.gophernest.net/azpect/TextEditor/internal/style"
"github.com/alecthomas/chroma/v2/styles"
tea "github.com/charmbracelet/bubbletea"
)
@ -883,29 +882,31 @@ func parseSetOption(m action.Model, opt string) error {
// Colorscheme Commands
// --------------------------------------------------
// TODO: Implement this using the new colorschemes
func cmdColorscheme(m action.Model, args []string, force bool) tea.Cmd {
_ = force
// No args, just print the current scheme
if len(args) == 0 {
s := m.Styles().ChromaStyle
if s == nil {
return nil
}
m.SetCommandOutput(&core.CommandOutput{
Lines: []string{s.Name},
Lines: []string{"default"},
Inline: true,
IsError: false,
})
return nil
}
// Args given, set the scheme
name := strings.Join(args, " ")
chromaStyle := styles.Registry[name]
if chromaStyle == nil {
// Theme switching is disabled while migrating away from Chroma.
name := strings.TrimSpace(strings.Join(args, " "))
if name == "" {
m.SetCommandOutput(&core.CommandOutput{
Lines: []string{"colorscheme not found: "},
Inline: true,
IsError: true,
})
return nil
}
if name != "" && strings.ToLower(name) != "default" {
m.SetCommandOutput(&core.CommandOutput{
Lines: []string{fmt.Sprintf("colorscheme not found: %s", name)},
Inline: true,
@ -914,14 +915,14 @@ func cmdColorscheme(m action.Model, args []string, force bool) tea.Cmd {
return nil
}
m.SetStyles(style.ChromaStyles(chromaStyle))
m.SetStyles(style.DefaultStyles())
return nil
}
func cmdListColorschemes(m action.Model, args []string, force bool) tea.Cmd {
_, _ = args, force
colors := styles.Names()
colors := []string{"default"}
m.SetMode(core.CommandOutputMode)
m.SetCommandOutput(&core.CommandOutput{

View File

@ -10,7 +10,6 @@ import (
"git.gophernest.net/azpect/TextEditor/internal/action"
"git.gophernest.net/azpect/TextEditor/internal/core"
"git.gophernest.net/azpect/TextEditor/internal/style"
cStyles "github.com/alecthomas/chroma/v2/styles"
tea "github.com/charmbracelet/bubbletea"
)
@ -5515,22 +5514,24 @@ func TestCmdColorscheme(t *testing.T) {
t.Run("valid name updates styles on model", func(t *testing.T) {
m := action.NewMockModel()
m.SetStyles(style.DefaultStyles())
before := m.StylesVal.BackgroundStyle.Render(" ")
cmdColorscheme(m, []string{"onedark"}, false)
cmdColorscheme(m, []string{"default"}, false)
name := m.Styles().ChromaStyle.Name
if name != "onedark" {
t.Error("expected styles to change after setting a valid colorscheme")
after := m.StylesVal.BackgroundStyle.Render(" ")
if after != before {
t.Error("expected default styles to remain stable after applying default")
}
})
t.Run("same valid name applied twice produces same styles", func(t *testing.T) {
m := action.NewMockModel()
cmdColorscheme(m, []string{"monokai"}, false)
cmdColorscheme(m, []string{"default"}, false)
first := m.StylesVal.BackgroundStyle.Render(" ")
cmdColorscheme(m, []string{"monokai"}, false)
cmdColorscheme(m, []string{"default"}, false)
second := m.StylesVal.BackgroundStyle.Render(" ")
if first != second {
@ -5541,7 +5542,7 @@ func TestCmdColorscheme(t *testing.T) {
t.Run("valid name sets no error output", func(t *testing.T) {
m := action.NewMockModel()
cmdColorscheme(m, []string{"monokai"}, false)
cmdColorscheme(m, []string{"default"}, false)
if m.CommandOutputVal != nil && m.CommandOutputVal.IsError {
t.Error("expected no error output for a valid colorscheme name")
@ -5551,7 +5552,7 @@ func TestCmdColorscheme(t *testing.T) {
t.Run("valid name returns nil tea.Cmd", func(t *testing.T) {
m := action.NewMockModel()
cmd := cmdColorscheme(m, []string{"monokai"}, false)
cmd := cmdColorscheme(m, []string{"default"}, false)
if cmd != nil {
t.Error("expected nil tea.Cmd for colorscheme command")
@ -5651,7 +5652,7 @@ func TestCmdColorscheme(t *testing.T) {
t.Run("extra args beyond name do not panic", func(t *testing.T) {
m := action.NewMockModel()
cmdColorscheme(m, []string{"monokai", "extra", "args"}, false)
cmdColorscheme(m, []string{"default", "extra", "args"}, false)
})
// --------------------------------------------------
@ -5661,10 +5662,9 @@ func TestCmdColorscheme(t *testing.T) {
t.Run("force flag with valid name still sets styles", func(t *testing.T) {
m := action.NewMockModel()
cmdColorscheme(m, []string{"monokai"}, true)
cmdColorscheme(m, []string{"default"}, true)
name := m.Styles().ChromaStyle.Name
if name != "monokai" {
if m.CommandOutputVal != nil && m.CommandOutputVal.IsError {
t.Error("expected styles to change with force=true and valid name")
}
})
@ -5742,7 +5742,7 @@ func TestCmdListColorschemes(t *testing.T) {
cmdListColorschemes(m, []string{}, false)
lines := m.CommandOutputVal.Lines
known := []string{"monokai", "github-dark", "dracula"}
known := []string{"default"}
for _, name := range known {
found := false
for _, l := range lines {
@ -5762,7 +5762,7 @@ func TestCmdListColorschemes(t *testing.T) {
cmdListColorschemes(m, []string{}, false)
expected := cStyles.Names()
expected := []string{"default"}
if len(m.CommandOutputVal.Lines) != len(expected) {
t.Errorf("expected %d colorschemes, got %d", len(expected), len(m.CommandOutputVal.Lines))
}
@ -5785,7 +5785,7 @@ func TestCmdListColorschemes(t *testing.T) {
m2 := action.NewMockModel()
cmdListColorschemes(m1, []string{}, false)
cmdListColorschemes(m2, []string{"monokai", "extra"}, true)
cmdListColorschemes(m2, []string{"default", "extra"}, true)
if len(m1.CommandOutputVal.Lines) != len(m2.CommandOutputVal.Lines) {
t.Error("expected args and force to have no effect on list output")

View File

@ -5,7 +5,6 @@ import (
"git.gophernest.net/azpect/TextEditor/internal/input"
"git.gophernest.net/azpect/TextEditor/internal/style"
"git.gophernest.net/azpect/TextEditor/internal/syntax"
"github.com/alecthomas/chroma/v2/styles"
)
type ModelBuilder struct {
@ -14,8 +13,7 @@ type ModelBuilder struct {
// NewModelBuilder: Builds and returns a new model, using the default color scheme (kanagawa-wave).
func NewModelBuilder() *ModelBuilder {
chromaStyle := styles.Get("kanagawa-wave")
editorStyles := style.ChromaStyles(chromaStyle)
editorStyles := style.DefaultStyles()
return &ModelBuilder{
model: Model{

223
internal/style/style.go Executable file → Normal file
View File

@ -1,11 +1,7 @@
package style
import (
"strings"
"git.gophernest.net/azpect/TextEditor/internal/core"
"github.com/alecthomas/chroma/v2"
"github.com/alecthomas/chroma/v2/lexers"
"github.com/charmbracelet/lipgloss"
)
@ -23,7 +19,7 @@ type Styles struct {
// Visual mode
VisualHighlight lipgloss.Style
VisualAnchor lipgloss.Style // debugging
VisualAnchor lipgloss.Style
// Status bar
StatusBar lipgloss.Style
@ -34,132 +30,72 @@ type Styles struct {
CommandOutputBorder lipgloss.Style
CommandContinueMessage lipgloss.Style
// General Styles
LineStyle lipgloss.Style // This is a simple background with no text coloring
BackgroundStyle lipgloss.Style // This is just the background
// Chroma data
ChromaStyle *chroma.Style
// General styles
LineStyle lipgloss.Style
BackgroundStyle lipgloss.Style
}
// DefaultStyles: Returns the default editor color scheme.
func DefaultStyles() Styles {
return Styles{
CursorNormal: lipgloss.NewStyle().Reverse(true),
CursorInsert: lipgloss.NewStyle().Underline(true),
CursorCommand: lipgloss.NewStyle().Reverse(true),
CursorReplace: lipgloss.NewStyle().Underline(true),
Gutter: lipgloss.NewStyle().
Background(lipgloss.Color("236")).
Foreground(lipgloss.Color("243")),
GutterCurrentLine: lipgloss.NewStyle().
Background(lipgloss.Color("236")).
Foreground(lipgloss.Color("#d69d00")),
VisualHighlight: lipgloss.NewStyle().
Background(lipgloss.Color("#7a6a00")),
VisualAnchor: lipgloss.NewStyle().
Background(lipgloss.Color("#a89020")),
StatusBar: lipgloss.NewStyle().
Background(lipgloss.Color("236")).
Foreground(lipgloss.Color("243")),
StatusBarActive: lipgloss.NewStyle().
Background(lipgloss.Color("62")).
Foreground(lipgloss.Color("230")),
CommandError: lipgloss.NewStyle().
Foreground(lipgloss.Color("#e3203a")),
CommandOutputBorder: lipgloss.NewStyle().
Background(lipgloss.Color("#000000")),
CommandContinueMessage: lipgloss.NewStyle().
Foreground(lipgloss.Color("#546fba")),
ChromaStyle: nil,
}
}
func ChromaStyles(chromaStyle *chroma.Style) Styles {
bgString := chromaStyle.Get(chroma.Background).Background.String()
lineNumbers := chromaStyle.Get(chroma.LineTableTD)
lineHighlight := chromaStyle.Get(chroma.LineHighlight)
bg := lipgloss.Color("#1f2335")
fg := lipgloss.Color("#dcd7ba")
return Styles{
CursorNormal: lipgloss.NewStyle().
Background(lipgloss.Color(bgString)).
Reverse(true),
Background(fg).
Foreground(bg),
CursorInsert: lipgloss.NewStyle().
Background(lipgloss.Color(bgString)).
Bold(true).
Background(bg).
Foreground(fg).
Underline(true),
CursorCommand: lipgloss.NewStyle().
Background(lipgloss.Color(bgString)).
Reverse(true),
Background(fg).
Foreground(bg),
CursorReplace: lipgloss.NewStyle().
Background(lipgloss.Color(bgString)).
Background(bg).
Foreground(fg).
Underline(true),
Gutter: lipgloss.NewStyle().
Background(lipgloss.Color(
darkenColor(lineNumbers.Background, 0.9).String()),
).
Foreground(lipgloss.Color(lineNumbers.Colour.String())),
Background(lipgloss.Color("#181b2a")).
Foreground(lipgloss.Color("#7e8399")),
GutterCurrentLine: lipgloss.NewStyle().
Background(lipgloss.Color(
darkenColor(lineNumbers.Background, 0.9).String()),
).
Foreground(lipgloss.Color(lineNumbers.Colour.String())),
Background(lipgloss.Color("#181b2a")).
Foreground(lipgloss.Color("#e6c384")),
VisualHighlight: lipgloss.NewStyle().
Background(lipgloss.Color(lineHighlight.Background.String())).
Foreground(lipgloss.Color(lineHighlight.Colour.String())),
Background(lipgloss.Color("#2f334d")),
VisualAnchor: lipgloss.NewStyle().
Background(lipgloss.Color(lineHighlight.Background.String())).
Foreground(lipgloss.Color(lineHighlight.Colour.String())),
Background(lipgloss.Color("#3a3f5f")),
StatusBar: lipgloss.NewStyle().
Background(lipgloss.Color(bgString)).
Foreground(lipgloss.Color("243")),
Background(lipgloss.Color("#181b2a")).
Foreground(lipgloss.Color("#8ea4a2")),
StatusBarActive: lipgloss.NewStyle().
Background(lipgloss.Color(bgString)).
Foreground(lipgloss.Color("230")),
Background(lipgloss.Color("#223249")).
Foreground(lipgloss.Color("#9ec1cf")),
CommandError: lipgloss.NewStyle().
Background(lipgloss.Color(bgString)).
Foreground(lipgloss.Color("#e3203a")),
Foreground(lipgloss.Color("#e82424")),
CommandOutputBorder: lipgloss.NewStyle().
Background(
lipgloss.Color(
darkenColor(
chromaStyle.Get(chroma.Background).Background, 0.5).
String(),
),
),
Background(lipgloss.Color("#11131d")),
CommandContinueMessage: lipgloss.NewStyle().
Background(lipgloss.Color(bgString)).
Foreground(lipgloss.Color("#546fba")),
Foreground(lipgloss.Color("#7aa2f7")),
LineStyle: lipgloss.NewStyle().
Foreground(lipgloss.Color(chromaStyle.Get(chroma.Line).Colour.String())).
Background(lipgloss.Color(bgString)),
Foreground(fg).
Background(bg),
BackgroundStyle: lipgloss.NewStyle().Background(lipgloss.Color(bgString)),
ChromaStyle: chromaStyle,
BackgroundStyle: lipgloss.NewStyle().
Background(bg),
}
}
@ -177,106 +113,29 @@ func (s Styles) DefaultCursorStyle(mode core.Mode) lipgloss.Style {
}
}
// Styles.CursorStyle: Returns a cursor style derived from a chroma style. This function should preferred
// over the DefaultCursorStyle, but in cases where there is no style to apply, the DefaultCursorStyle
// will always work.
func (s Styles) CursorStyle(mode core.Mode, style lipgloss.Style) lipgloss.Style {
// Styles.CursorStyle: Returns a cursor style derived from the text style.
func (s Styles) CursorStyle(mode core.Mode, textStyle lipgloss.Style) lipgloss.Style {
switch mode {
case core.NormalMode, core.VisualLineMode, core.VisualBlockMode, core.VisualMode:
return lipgloss.NewStyle().
Background(style.GetForeground()).
Foreground(style.GetBackground())
Background(textStyle.GetForeground()).
Foreground(textStyle.GetBackground())
case core.ReplaceMode, core.WaitingMode:
return lipgloss.NewStyle().
Background(style.GetBackground()).
Foreground(style.GetForeground()).
Background(textStyle.GetBackground()).
Foreground(textStyle.GetForeground()).
Underline(true)
default:
return lipgloss.NewStyle().
Background(s.BackgroundStyle.GetBackground()).
Foreground(style.GetForeground()).
Foreground(textStyle.GetForeground()).
Underline(true)
}
}
// Styles.VisualHighlightWithTextColor: Works analogously to CursorStyle vs DefaultCursorStyle. When a
// style is available, this function should be used, so the text color will be rendered in front
// of the background. Otherwise, the VisualHighlight property will always work.
func (s Styles) VisualHighlightWithTextColor(style lipgloss.Style) lipgloss.Style {
// Styles.VisualHighlightWithTextColor: Applies visual background while preserving text color.
func (s Styles) VisualHighlightWithTextColor(textStyle lipgloss.Style) lipgloss.Style {
return lipgloss.NewStyle().
Background(s.VisualHighlight.GetBackground()).
Foreground(style.GetForeground())
}
// Styles.MakeStyleMap: Generates a style map for a single line. A style map is a mapping from
// column a lipgloss style. Cursor styles are not handled by this map, but they can be derived
// by inverting the background and foreground (and rolling back to the default).
func (s Styles) MakeStyleMap(lexer chroma.Lexer, line string) []lipgloss.Style {
m := make([]lipgloss.Style, len(line))
if s.ChromaStyle == nil {
return m
}
iter, err := lexer.Tokenise(nil, line)
if err != nil {
panic(err)
}
col := 0
for _, token := range iter.Tokens() {
entry := s.ChromaStyle.Get(token.Type)
s := lipgloss.NewStyle().
Background(lipgloss.Color(entry.Background.String())).
Foreground(lipgloss.Color(entry.Colour.String()))
for _, char := range token.Value {
if char == '\n' {
continue
}
if col < len(m) {
m[col] = s
}
col++
}
}
return m
}
// darkenColor: Uses a factor (0.0 to 1.0) to darken a color using its opacity.
func darkenColor(c chroma.Colour, factor float64) chroma.Colour {
r := uint8(float64(c.Red()) * factor)
g := uint8(float64(c.Green()) * factor)
b := uint8(float64(c.Blue()) * factor)
return chroma.NewColour(r, g, b)
}
// GetLexer: Uses buffer meta data or content to pick a lexer for use in applying
// highlights.
func GetLexer(buf *core.Buffer) chroma.Lexer {
var lexer chroma.Lexer
if buf.Filetype != "" {
lexer = lexers.Get(strings.TrimPrefix(buf.Filetype, "."))
}
if lexer == nil && buf.Filename != "" {
lexer = lexers.Match(buf.Filename)
}
if lexer == nil && len(buf.Lines) > 0 {
// Get first few lines for content analysis
var content strings.Builder
for i := 0; i < min(len(buf.Lines), 10); i++ {
content.WriteString(buf.Lines[i].String() + "\n")
}
lexer = lexers.Analyse(content.String())
}
if lexer == nil {
lexer = lexers.Fallback
}
lexer = chroma.Coalesce(lexer) // Merge tokens together
return lexer
Foreground(textStyle.GetForeground())
}

View File

@ -1,43 +1,7 @@
package theme
import (
"embed"
"fmt"
"github.com/alecthomas/chroma/v2"
"github.com/alecthomas/chroma/v2/styles"
)
//go:embed themes/*
var themeFS embed.FS
// RegisterAll: Registers all XML theme files embedded in the themes/ directory
// with chroma's style registry. After calling this, styles.Get() will recognize
// any theme defined in those files.
// RegisterAll is retained as a no-op for compatibility while Chroma-based
// theme loading is removed.
func RegisterAll() error {
entries, err := themeFS.ReadDir("themes")
if err != nil {
return fmt.Errorf("failed to read embedded themes directory: %w", err)
}
for _, entry := range entries {
if entry.IsDir() {
continue
}
f, err := themeFS.Open("themes/" + entry.Name())
if err != nil {
return fmt.Errorf("failed to open theme %s: %w", entry.Name(), err)
}
style, err := chroma.NewXMLStyle(f)
f.Close()
if err != nil {
return fmt.Errorf("failed to parse theme %s: %w", entry.Name(), err)
}
styles.Register(style)
}
return nil
}