-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
176 lines (158 loc) · 6.04 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"fmt"
"log/slog"
"os"
"zed-neovim/vim"
"zed-neovim/zed"
)
const paletteFile = "default/palette.json"
type Color struct {
Name string
Foreground string
Background string
Special string
Blend int
Attr vim.HlAttr
}
// Flatten resolves the links on the scheme and makes sure all highlight groups
// are explicitly defined.
// It writes the colors to the given map. This makes so colors from different
// schemes can be merged in the chosen order by calling the function multiple
// times.
func resolve(groups vim.HlGroups, palette vim.Palette, colors map[string]Color) {
linkGroups := make(vim.HlGroups)
defaultGroups := make(vim.HlGroups)
for name, group := range groups {
// handle links in second pass
if group.IsLink {
if group.IsDefault {
defaultGroups[group.Name] = group
} else {
linkGroups[group.Name] = group
}
continue
}
colors[name] = Color{
Name: name,
Foreground: palette[group.Attrs.Guifg],
Background: palette[group.Attrs.Guibg],
Special: palette[group.Attrs.Guisp],
Blend: group.Attrs.Blend,
Attr: group.Attrs.Gui,
}
}
for _, group := range defaultGroups {
resolvedGroup, ok := resolveLinks(colors, defaultGroups, group, make(map[string]bool))
if !ok {
continue
}
if newColors, ok := colors[resolvedGroup.ToGroup]; ok {
newColors.Name = group.Name
colors[group.Name] = newColors
}
}
for _, group := range linkGroups {
resolvedGroup, ok := resolveLinks(colors, linkGroups, group, make(map[string]bool))
if !ok {
continue
}
if newColors, ok := colors[resolvedGroup.ToGroup]; ok {
newColors.Name = group.Name
colors[group.Name] = newColors
}
}
}
func resolveLinks(colors map[string]Color, groups vim.HlGroups, group vim.HlGroup, seen map[string]bool) (vim.HlGroup, bool) {
if group.ToGroup == "" {
return group, true
}
if seen[group.ToGroup] {
return group, false
}
seen[group.ToGroup] = true
nextGroup, ok := groups[group.ToGroup]
if ok {
return resolveLinks(colors, groups, nextGroup, seen)
}
return group, true
}
func main() {
slog.Info("Loading palette", slog.String("file", paletteFile))
paletteF, err := os.Open(paletteFile)
if err != nil {
slog.Error("Failed to open palette file", slog.Any("error", err))
os.Exit(1)
}
palette, err := vim.LoadPalette(paletteF)
if err != nil {
slog.Error("Failed to load palette", slog.Any("error", err))
}
schemes := map[string]string{
"both": "default/both.vim",
"dark": "default/dark.vim",
"light": "default/light.vim",
"cmdline": "default/cmdline.vim",
}
colorSchemes, err := loadColorSchemesFromFiles(schemes)
if err != nil {
slog.Error("Failed to load colors", slog.Any("error", err))
}
darkColors := make(map[string]Color)
resolve(colorSchemes["dark"], palette, darkColors)
resolve(colorSchemes["both"], palette, darkColors)
resolve(colorSchemes["cmdline"], palette, darkColors)
darkColors["zed.syntax.constant.builtin"] = darkColors["@constant"]
darkColors["zed.syntax.method"] = darkColors["@lsp.type.method"]
darkColors["zed.syntax.property"] = darkColors["@lsp.type.property"]
darkColors["zed.syntax.type"] = darkColors["@lsp.type.type"]
darkColors["zed.syntax.type.builtin"] = darkColors["Special"]
darkColors["zed.syntax.variable"] = darkColors["@lsp.type.variable"]
darkColors["zed.syntax.variable.member"] = darkColors["@lsp.type.variable"]
lightColors := make(map[string]Color)
resolve(colorSchemes["light"], palette, lightColors)
resolve(colorSchemes["both"], palette, lightColors)
resolve(colorSchemes["cmdline"], palette, lightColors)
lightColors["zed.syntax.constant.builtin"] = lightColors["@constant"]
lightColors["zed.syntax.method"] = lightColors["@lsp.type.method"]
lightColors["zed.syntax.property"] = lightColors["@lsp.type.property"]
lightColors["zed.syntax.type"] = lightColors["@lsp.type.type"]
lightColors["zed.syntax.type.builtin"] = lightColors["Special"]
lightColors["zed.syntax.variable"] = lightColors["@lsp.type.variable"]
lightColors["zed.syntax.variable.member"] = lightColors["@lsp.type.variable"]
darkColorsAccented := make(map[string]Color)
for k := range darkColors {
darkColorsAccented[k] = darkColors[k]
}
darkColorsAccented["zed.syntax.constant.builtin"] = darkColors["Special"]
darkColorsAccented["zed.syntax.method"] = darkColors["@lsp.type.function"]
darkColorsAccented["zed.syntax.property"] = darkColors["Identifier"]
darkColorsAccented["zed.syntax.type"] = darkColors["Special"]
darkColorsAccented["zed.syntax.type.builtin"] = darkColors["Special"]
darkColorsAccented["zed.syntax.variable.member"] = darkColors["Identifier"]
lightColorsAccented := make(map[string]Color)
for k := range lightColors {
lightColorsAccented[k] = lightColors[k]
}
lightColorsAccented["zed.syntax.constant.builtin"] = lightColors["Special"]
lightColorsAccented["zed.syntax.method"] = lightColors["@lsp.type.function"]
lightColorsAccented["zed.syntax.property"] = lightColors["Identifier"]
lightColorsAccented["zed.syntax.type"] = lightColors["Special"]
lightColorsAccented["zed.syntax.type.builtin"] = lightColors["Special"]
lightColorsAccented["zed.syntax.variable.member"] = lightColors["Identifier"]
// for _, color := range colors {
// fmt.Printf("%s: %s %s %s %d %v\n", color.Name, color.Foreground, color.Background, color.Special, color.Blend, color.Attr)
// }
darkColorsZedStyle := toZedStyle(darkColors)
lightColorsZedStyle := toZedStyle(lightColors)
darkColorsAccentedZedStyle := toZedStyle(darkColorsAccented)
lightColorsAccentedZedStyle := toZedStyle(lightColorsAccented)
theme := zed.NewTheme("Neovim default", "Kim Nørgaard")
addTheme(theme, "Neovim default dark", zed.AppearanceContentDark, &darkColorsZedStyle)
addTheme(theme, "Neovim default light", zed.AppearanceContentLight, &lightColorsZedStyle)
addTheme(theme, "Neovim default accented dark", zed.AppearanceContentDark, &darkColorsAccentedZedStyle)
addTheme(theme, "Neovim default accented light", zed.AppearanceContentLight, &lightColorsAccentedZedStyle)
if data, err := zed.MarshalJSON(theme); err == nil {
fmt.Println(string(data))
}
}