1
1
local config = require (' github-theme.config' )
2
+ local override = require (' github-theme.override' )
3
+ local keys = { ' palettes' , ' specs' , ' groups' }
4
+ local did_setup = false
5
+ local M = {}
2
6
3
7
local function read_file (filepath )
4
8
local file = io.open (filepath , ' r' )
@@ -17,99 +21,91 @@ local function write_file(filepath, content)
17
21
end
18
22
end
19
23
20
- local M = {}
21
-
22
24
function M .reset ()
23
25
require (' github-theme.config' ).reset ()
24
26
require (' github-theme.override' ).reset ()
25
27
end
26
28
27
29
--- Compiles all themes/styles with their current settings.
30
+ --- @param force boolean true by default
28
31
--- @return nil
29
- function M .compile ()
30
- require (' github-theme.lib.log' ).clear ()
31
- local compiler = require (' github-theme.lib.compiler' )
32
- local themes = require (' github-theme.palette' ).themes
33
- local current_theme = config .theme
34
- for _ , theme in ipairs (themes ) do
35
- -- Compile current theme last (see discussion in #290)
36
- if theme ~= current_theme then
37
- compiler .compile ({ theme = theme })
38
- end
39
- end
40
- compiler .compile ({ theme = current_theme })
41
- end
32
+ function M .compile (force )
33
+ local util = require (' github-theme.util' )
34
+ util .ensure_dir (config .options .compile_path )
42
35
43
- -- Avoid g:colors_name reloading
44
- local lock = false
45
- local did_setup = false
36
+ local cached_path = util .join_paths (config .options .compile_path , ' cache' )
37
+ local cached = read_file (cached_path )
38
+ local git_path =
39
+ vim .fn .fnamemodify (vim .fn .resolve (debug.getinfo (1 ).source :sub (2 )), ' :p:h:h:h' )
40
+ local git = vim .fn .getftime (util .join_paths (git_path , ' .git' ))
46
41
47
- function M .load (opts )
48
- if lock then
49
- return
42
+ -- This is needed because neither `opts` nor `config` necessarily contain
43
+ -- everything we need to hash. For example, `opts` may not contain all
44
+ -- overrides and config currently in use (`setup()` might have been called
45
+ -- before, or maybe overrides were set directly and outside of `setup()`), and
46
+ -- `config` does not contain any of the overrides in use. Therefore, we need
47
+ -- to create a new table which contains everything in-use.
48
+ local dummy = { options = config .options }
49
+ for _ , k in ipairs (keys ) do
50
+ dummy [k ] = override [k ]
50
51
end
51
52
52
- if not did_setup then
53
- M .setup ()
53
+ local hash = require (' github-theme.lib.hash' )(dummy ) .. (git == - 1 and git_path or git )
54
+
55
+ if force ~= false or cached ~= hash then
56
+ require (' github-theme.lib.log' ).clear ()
57
+ local compiler = require (' github-theme.lib.compiler' )
58
+ local themes = require (' github-theme.palette' ).themes
59
+ local current_theme = config .theme
60
+
61
+ for _ , theme in ipairs (themes ) do
62
+ -- Compile current theme last (see discussion in #290)
63
+ if theme ~= current_theme then
64
+ compiler .compile ({ theme = theme })
65
+ end
66
+ end
67
+
68
+ compiler .compile ({ theme = current_theme })
69
+ write_file (cached_path , hash )
54
70
end
55
71
56
- opts = opts or {}
72
+ getmetatable (override ).__index .changed_since_last_compile = false
73
+ end
57
74
75
+ function M .load (opts )
76
+ opts = opts or {}
58
77
local _ , compiled_file = config .get_compiled_info (opts )
59
- lock = true
60
-
61
78
local f = loadfile (compiled_file )
62
- if not f then
63
- M .compile ()
79
+
80
+ if not did_setup or override .changed_since_last_compile or not f then
81
+ M .setup ()
64
82
f = loadfile (compiled_file )
65
83
end
66
84
67
85
--- @diagnostic disable-next-line : need-check-nil
68
86
f ()
69
-
70
87
require (' github-theme.autocmds' ).set_autocmds ()
71
- lock = false
72
88
end
73
89
74
- M .setup = function (opts )
75
- did_setup = true
90
+ --- Applies any new config or overrides then (re)compiles if needed.
91
+ --- @param opts ? table
92
+ function M .setup (opts )
76
93
opts = opts or {}
77
-
78
- local override = require (' github-theme.override' )
94
+ did_setup = true
79
95
80
96
-- New configs
81
97
if opts .options then
82
98
config .set_options (opts .options )
83
99
end
84
100
85
- if opts .palettes ~= nil then
86
- override .palettes = opts .palettes
87
- end
88
-
89
- if opts .specs ~= nil then
90
- override .specs = opts .specs
91
- end
92
-
93
- if opts .groups ~= nil then
94
- override .groups = opts .groups
95
- end
96
-
97
- local util = require (' github-theme.util' )
98
- util .ensure_dir (config .options .compile_path )
99
-
100
- local cached_path = util .join_paths (config .options .compile_path , ' cache' )
101
- local cached = read_file (cached_path )
102
-
103
- local git_path =
104
- vim .fn .fnamemodify (vim .fn .resolve (debug.getinfo (1 ).source :sub (2 )), ' :p:h:h:h' )
105
- local git = vim .fn .getftime (util .join_paths (git_path , ' .git' ))
106
- local hash = require (' github-theme.lib.hash' )(opts ) .. (git == - 1 and git_path or git )
107
-
108
- if cached ~= hash then
109
- M .compile ()
110
- write_file (cached_path , hash )
101
+ for _ , k in ipairs (keys ) do
102
+ local v = opts [k ]
103
+ if v ~= nil then
104
+ override [k ] = v
105
+ end
111
106
end
112
107
108
+ M .compile (false )
113
109
require (' github-theme.util.deprecation' ).check_deprecation (opts )
114
110
end
115
111
0 commit comments