Skip to content

Commit f02d524

Browse files
authored
refactor(palette, spec): cleanup, DRY, add types (#328)
Refactor palette.lua and spec.lua
1 parent 80fb5cc commit f02d524

File tree

5 files changed

+81
-91
lines changed

5 files changed

+81
-91
lines changed

lua/github-theme/init.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ function M.compile(force)
5858
local themes = require('github-theme.palette').themes
5959
local current_theme = config.theme
6060

61-
for _, theme in ipairs(themes) do
61+
for theme in pairs(themes) do
6262
-- Compile current theme last (see discussion in #290)
6363
if theme ~= current_theme then
6464
compiler.compile({ theme = theme })

lua/github-theme/palette.lua

+39-45
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,54 @@
1-
local collect = require('github-theme.lib.collect')
2-
local config = require('github-theme.config')
3-
41
local M = {}
52

3+
---@enum GhTheme.Theme
64
M.themes = {
7-
'github_dark',
8-
'github_dark_colorblind',
9-
'github_dark_default',
10-
'github_dark_dimmed',
11-
'github_dark_high_contrast',
12-
'github_dark_tritanopia',
13-
'github_light',
14-
'github_light_colorblind',
15-
'github_light_default',
16-
'github_light_high_contrast',
17-
'github_light_tritanopia',
5+
github_dark = 'github_dark',
6+
github_dark_colorblind = 'github_dark_colorblind',
7+
github_dark_default = 'github_dark_default',
8+
github_dark_dimmed = 'github_dark_dimmed',
9+
github_dark_high_contrast = 'github_dark_high_contrast',
10+
github_dark_tritanopia = 'github_dark_tritanopia',
11+
github_light = 'github_light',
12+
github_light_colorblind = 'github_light_colorblind',
13+
github_light_default = 'github_light_default',
14+
github_light_high_contrast = 'github_light_high_contrast',
15+
github_light_tritanopia = 'github_light_tritanopia',
1816
}
1917

20-
local function override(color, ovr)
21-
for key, value in pairs(ovr) do
22-
color[key] = value
23-
end
24-
return color
25-
end
26-
27-
function M.load(name)
18+
---@param theme GhTheme.Theme
19+
local function get_palette(theme)
2820
local ovr = require('github-theme.override').palettes
29-
30-
local function apply_ovr(key, palette)
31-
return ovr[key] and override(palette, ovr[key]) or palette
21+
local raw = require('github-theme.palette.' .. theme)
22+
local pal = raw.palette
23+
if ovr.all then
24+
pal = vim.tbl_deep_extend('force', pal, ovr.all)
3225
end
26+
if ovr[theme] then
27+
pal = vim.tbl_deep_extend('force', pal, ovr[theme])
28+
end
29+
pal.meta, pal.generate_spec = raw.meta, raw.generate_spec
30+
return pal
31+
end
3332

34-
if name then
35-
local valid = collect.contains(M.themes, name)
36-
local raw = valid and require('github-theme.palette.' .. name)
37-
or require('github-theme.palette.' .. config.theme)
38-
local palette = raw.palette
39-
palette = apply_ovr('all', palette)
40-
palette = apply_ovr(name, palette)
41-
palette.meta = raw.meta
42-
palette.generate_spec = raw.generate_spec
33+
---Returns the palette for the given `theme`, or all themes (i.e. a map from
34+
---theme name to palette) if `theme` is `nil`.
35+
---@param theme? GhTheme.Theme
36+
function M.load(theme)
37+
if theme ~= nil then
38+
if not M.themes[theme] then
39+
error('invalid theme provided: ' .. theme)
40+
end
4341

44-
return palette
42+
return get_palette(theme)
4543
else
46-
local result = {}
47-
for _, mod in ipairs(M.themes) do
48-
local raw = require('github-theme.palette.' .. mod)
49-
local palette = raw.palette
50-
palette = apply_ovr('all', palette)
51-
palette = apply_ovr(mod, palette)
52-
palette.meta = raw.meta
53-
palette.generate_spec = raw.generate_spec
54-
result[mod] = palette
44+
local all = {}
45+
46+
---@diagnostic disable-next-line: redefined-local
47+
for theme in pairs(M.themes) do
48+
all[theme] = get_palette(theme)
5549
end
5650

57-
return result
51+
return all
5852
end
5953
end
6054

lua/github-theme/spec.lua

+38-42
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
local collect = require('github-theme.lib.collect')
2-
local template = require('github-theme.util.template')
1+
local M = {}
32

43
--#region Types
54

6-
---@class Spec
5+
---@class GhTheme.Spec
76
---@field bg0 string
87
---@field bg1 string
98
---@field bg2 string
@@ -16,18 +15,17 @@ local template = require('github-theme.util.template')
1615
---@field sel0 string
1716
---@field sel1 string
1817
---@field sel2 string
19-
---@field syntax SpecSyntax
20-
---@field diag SpecDiagnostic
21-
---@field diag_bg SpecDiagnosticBg
22-
---@field diff SpecDiff
23-
---@field git SpecGit
18+
---@field syntax GhTheme.Spec.Syntax
19+
---@field diag GhTheme.Spec.Diagnostic
20+
---@field diag_bg GhTheme.Spec.DiagnosticBg
21+
---@field diff GhTheme.Spec.Diff
22+
---@field git GhTheme.Spec.Git
2423

25-
---@class SpecSyntax
24+
---@class GhTheme.Spec.Syntax
2625
---@field bracket string
2726
---@field builtin0 string
2827
---@field builtin1 string
2928
---@field builtin2 string
30-
---@field builtin3 string
3129
---@field comment string
3230
---@field conditional string
3331
---@field const string
@@ -47,64 +45,62 @@ local template = require('github-theme.util.template')
4745
---@field type string
4846
---@field variable string
4947

50-
---@class SpecDiagnostic
48+
---@class GhTheme.Spec.Diagnostic
5149
---@field error string
5250
---@field warn string
5351
---@field info string
5452
---@field hint string
5553

56-
---@class SpecDiagnosticBg
54+
---@class GhTheme.Spec.DiagnosticBg
5755
---@field error string
5856
---@field warn string
5957
---@field info string
6058
---@field hint string
6159

62-
---@class SpecDiff
60+
---@class GhTheme.Spec.Diff
6361
---@field add string
6462
---@field delete string
6563
---@field change string
6664
---@field text string
6765

68-
---@class SpecGit
66+
---@class GhTheme.Spec.Git
6967
---@field add string
7068
---@field removed string
7169
---@field changed string
7270

7371
--#endregion
7472

75-
local M = {}
76-
77-
local function override(spec, palette, ovr)
78-
ovr = template.parse(ovr, palette)
79-
return collect.deep_extend(spec, ovr)
80-
end
81-
82-
function M.load(name)
73+
---@param theme GhTheme.Theme
74+
local function get_spec(theme)
75+
local template = require('github-theme.util.template')
8376
local ovr = require('github-theme.override').specs
84-
85-
local function apply_ovr(key, spec, palette)
86-
return ovr[key] and override(spec, palette, ovr[key]) or spec
77+
local pal = require('github-theme.palette').load(theme)
78+
local spec = pal.generate_spec(pal)
79+
if ovr.all then
80+
pal = vim.tbl_deep_extend('force', pal, template.parse(ovr.all, pal))
81+
end
82+
if ovr[theme] then
83+
pal = vim.tbl_deep_extend('force', pal, template.parse(ovr[theme], pal))
8784
end
85+
spec.palette = pal
86+
return spec
87+
end
8888

89-
if name then
90-
local palette = require('github-theme.palette').load(name)
91-
local spec = palette.generate_spec(palette)
92-
spec = apply_ovr('all', spec, palette)
93-
spec = apply_ovr(name, spec, palette)
94-
spec.palette = palette
95-
return spec
89+
---Returns the spec for the given `theme`, or all themes (i.e. a map from theme
90+
---name to spec) if `theme` is `nil`.
91+
---@param theme? GhTheme.Theme
92+
function M.load(theme)
93+
if theme ~= nil then
94+
return get_spec(theme)
9695
else
97-
local result = {}
98-
local themes = require('github-theme.palette').themes
99-
for _, mod in ipairs(themes) do
100-
local palette = require('github-theme.palette').load(mod)
101-
local spec = palette.generate_spec(palette)
102-
spec = apply_ovr('all', spec, palette)
103-
spec = apply_ovr(mod, spec, palette)
104-
spec.palette = palette
105-
result[mod] = spec
96+
local all = {}
97+
98+
---@diagnostic disable-next-line: redefined-local
99+
for theme in pairs(M.themes) do
100+
all[theme] = get_spec(theme)
106101
end
107-
return result
102+
103+
return all
108104
end
109105
end
110106

lua/github-theme/util/template.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ end
1818

1919
---Parses and resolves keypath string for configuration template.
2020
---@param str string
21-
---@param spec Spec
21+
---@param spec GhTheme.Spec
2222
---@return any
2323
local function parse_keypath(str, spec)
2424
if type(str) ~= 'string' or str == '' or str:find('^#') then

test/github-theme/config/darken_spec.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe('config > options > darken', function()
1414
end)
1515

1616
describe('> floats', function()
17-
for _, variant in ipairs(require('github-theme.palette').themes) do
17+
for variant in pairs(require('github-theme.palette').themes) do
1818
-- TODO: see #324
1919
local it_or_pending = variant:find('high[-_]*contrast') and pending or it
2020

@@ -49,7 +49,7 @@ describe('config > options > darken', function()
4949

5050
describe('> sidebars', function()
5151
describe('> enable', function()
52-
for _, variant in ipairs(require('github-theme.palette').themes) do
52+
for variant in pairs(require('github-theme.palette').themes) do
5353
-- TODO: see #324
5454
local it_or_pending = variant:find('high[-_]*contrast') and pending or it
5555

0 commit comments

Comments
 (0)