Skip to content

Commit

Permalink
feat: Compiling theme for blazingly fast load
Browse files Browse the repository at this point in the history
This feature request requested by @adrian5 in the #226 discussion.

Alongside, I have deprecated the `dev` configuration and replaced it
with the `vim.g.github_theme_debug` vim global. For more information,
please check `:h github-theme.changelog-06052023`.
  • Loading branch information
ful1e5 committed May 6, 2023
1 parent 40edd5a commit 2c782a3
Show file tree
Hide file tree
Showing 20 changed files with 417 additions and 35 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `sidebars`-> `options.darken.sidebars.list`
- `colors`-> `palettes` or `specs`
- `overrides` -> `groups`
- `dev` -> `vim.g.github_theme_debug`

### New Configuration

- `options.compile_file_suffix` -> Option for setting compiled file suffix.
- `options.compile_path` -> Option for setting compile files path.
- `options.terminal_colors` -> Option for toggling builtin terminal highlights.
- `options.module_default` -> Option for toggling plugins highlights
- `options.modules` -> Option for individual plugins related configuration

### What's New?

- Compiling colorscheme for blazingly fast load.
- `lib.deprecation` - A Deprecation library to print Deprecation Messages.
- `lib.log` - A logging library to print log Messages.
- The `config.default` module has been changed to a variable and moved inside the `config` module.
Expand All @@ -47,8 +52,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- #160 closed due to inactivity.
- #191 closed due to inactivity.
- #201 fixed
- #193 closed with conversation.
- #201 fixed
- #209 fixed
- #213 fixed
- #228 fixed
- #231 fixed
Expand Down
3 changes: 3 additions & 0 deletions colors/github_dark.vim
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
lua << EOF

if vim.g.github_theme_debug then
require("github-theme.util.reload")()
end
require("github-theme.config").set_theme('github_dark')
require("github-theme").load()

Expand Down
3 changes: 3 additions & 0 deletions colors/github_dark_colorblind.vim
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
lua << EOF

if vim.g.github_theme_debug then
require("github-theme.util.reload")()
end
require("github-theme.config").set_theme('github_dark_colorblind')
require("github-theme").load()

Expand Down
3 changes: 3 additions & 0 deletions colors/github_dark_dimmed.vim
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
lua << EOF

if vim.g.github_theme_debug then
require("github-theme.util.reload")()
end
require('github-theme.config').set_theme('github_dark_dimmed')
require('github-theme').load()

Expand Down
3 changes: 3 additions & 0 deletions colors/github_dark_high_contrast.vim
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
lua << EOF

if vim.g.github_theme_debug then
require("github-theme.util.reload")()
end
require('github-theme.config').set_theme('github_dark_high_contrast')
require('github-theme').load()

Expand Down
3 changes: 3 additions & 0 deletions colors/github_dark_tritanopia.vim
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
lua << EOF

if vim.g.github_theme_debug then
require("github-theme.util.reload")()
end
require('github-theme.config').set_theme('github_dark_tritanopia')
require('github-theme').load()

Expand Down
3 changes: 3 additions & 0 deletions colors/github_light.vim
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
lua << EOF

if vim.g.github_theme_debug then
require("github-theme.util.reload")()
end
require("github-theme.config").set_theme('github_light')
require("github-theme").load()

Expand Down
3 changes: 3 additions & 0 deletions colors/github_light_colorblind.vim
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
lua << EOF

if vim.g.github_theme_debug then
require("github-theme.util.reload")()
end
require("github-theme.config").set_theme('github_light_colorblind')
require("github-theme").load()

Expand Down
3 changes: 3 additions & 0 deletions colors/github_light_high_contrast.vim
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
lua << EOF

if vim.g.github_theme_debug then
require("github-theme.util.reload")()
end
require('github-theme.config').set_theme('github_light_high_contrast')
require('github-theme').load()

Expand Down
3 changes: 3 additions & 0 deletions colors/github_light_tritanopia.vim
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
lua << EOF

if vim.g.github_theme_debug then
require("github-theme.util.reload")()
end
require('github-theme.config').set_theme('github_light_tritanopia')
require('github-theme').load()

Expand Down
16 changes: 16 additions & 0 deletions doc/gt_changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@

# Changelog


*github-theme.changelog-06052023*
Date: May 06, 2023

I(ful1e5) removed the `dev` configuration from `setup({...})` and added the
`vim.g.github_theme_debug` Vim global option for theme debugging. This option
creates a Lua file in the Neovim cache directory and write debugging logs to
it.

Previously, the `dev` option was used for debugging and assigning new
highlights to the theme, or for overriding defined highlights. Now, with the
removal of the `dev` option, you can override any highlight without enabling
any option.



*github-theme.changelog-23042023*
Date: April 23, 2023

Expand Down
3 changes: 2 additions & 1 deletion doc/gt_deprecated.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ SETUP OPTIONS
- *'sidebars'* Deprecated. Use |'options.darken.sidebars.list'| Instead.
- *'colors'* Deprecated. Use |'specs'| and |'palettes'| Instead.
- *'overrides'* Deprecated. Use |'groups'| Instead.
- *'dev'* Deprecated. Use |'vim.g.github_theme_dev'| Instead.


GLOBAL OPTIONS
Expand All @@ -50,7 +51,7 @@ GLOBAL OPTIONS
- *'vim.g.github_function_style'* Use |setup| Lua function instead.
- *'vim.g.github_keyword_style'* Use |setup| Lua function instead.
- *'vim.g.github_variable_style'* Use |setup| Lua function instead.
- *'vim.g.github_dev'* Use |setup| Lua function instead.
- *'vim.g.github_dev'* Use |vim.g.github_theme_debug| instead.


MODULES
Expand Down
20 changes: 17 additions & 3 deletions lua/github-theme/config.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
local collect = require('github-theme.lib.collect')
local util = require('github-theme.util')

local M = { theme = 'github_dark', has_options = true }
local M = { theme = 'github_dark', has_options = false }

local defaults = {
dev = false,
transparent = false,
compile_file_suffix = '_compiled',
compile_path = util.join_paths(util.cache_home, 'github-theme'),
hide_end_of_buffer = true,
hide_nc_statusline = true,
terminal_colors = true,
transparent = false,
module_default = true,
styles = {
comments = 'italic',
Expand Down Expand Up @@ -85,4 +87,16 @@ function M.reset()
M.options = collect.deep_copy(defaults)
end

function M.get_compiled_info(opts)
local output_path = opts.output_path or M.options.compile_path
local file_suffix = opts.file_suffix or M.options.compile_file_suffix
local style = opts.name or M.theme
return output_path, util.join_paths(output_path, style .. file_suffix)
end

function M.hash()
local hash = require('github-theme.lib.hash')(M.options)
return hash and hash or 0
end

return M
80 changes: 56 additions & 24 deletions lua/github-theme/init.lua
Original file line number Diff line number Diff line change
@@ -1,19 +1,44 @@
local M = {}

local config = require('github-theme.config')
local highlight = require('github-theme.lib.highlight').highlight
local dep = require('github-theme.util.deprecation')

local did_setup = false
local function read_file(filepath)
local file = io.open(filepath, 'r')
if file then
local content = file:read()
file:close()
return content
end
end

local function write_file(filepath, content)
local file = io.open(filepath, 'wb')
if file then
file:write(content)
file:close()
end
end

local M = {}

function M.reset()
require('github-theme.config').reset()
require('github-theme.override').reset()
end

function M.compile()
require('github-theme.lib.log').clear()

local compiler = require('github-theme.lib.compiler')
local themes = require('github-theme.palette').themes
for _, style in ipairs(themes) do
compiler.compile({ style = style })
end
end

-- Avoid g:colors_name reloading
local lock = false
local did_setup = false

M.load = function(opts)
function M.load(opts)
if lock then
return
end
Expand All @@ -22,21 +47,19 @@ M.load = function(opts)
M.setup()
end

lock = true
opts = opts or {}

local spec = require('github-theme.spec').load(config.theme)
local groups = require('github-theme.group').from(spec)
local background = spec.palette.meta.light and 'light' or 'dark'
local _, compiled_file = config.get_compiled_info(opts)
lock = true

if vim.g.colors_name then
vim.cmd('hi clear')
local f = loadfile(compiled_file)
if not f then
M.compile()
f = loadfile(compiled_file)
end
vim.o.termguicolors = true
vim.g.colors_name = config.theme
vim.o.background = background

highlight(groups)
require('github-theme.autocmds').set()
---@diagnostic disable-next-line: need-check-nil
f()

lock = false
end
Expand All @@ -47,12 +70,6 @@ M.setup = function(opts)

local override = require('github-theme.override')

-- TODO: Remove these individual conditions when migration
-- from old config to 'opts.options' has been DONE.
if opts.dev then
config.set_options({ dev = opts.dev })
end

-- New configs
if opts.options then
config.set_options(opts.options)
Expand All @@ -70,7 +87,22 @@ M.setup = function(opts)
override.groups = opts.groups
end

dep.check_deprecation(opts)
local util = require('github-theme.util')
util.ensure_dir(config.options.compile_path)

local cached_path = util.join_paths(config.options.compile_path, 'cache')
local cached = read_file(cached_path)

local git_path = util.join_paths(debug.getinfo(1).source:sub(2, -23), '.git')
local git = vim.fn.getftime(git_path)
local hash = require('github-theme.lib.hash')(opts) .. (git == -1 and git_path or git)

if cached ~= hash then
M.compile()
write_file(cached_path, hash)
end

require('github-theme.util.deprecation').check_deprecation(opts)
end

return M
Loading

0 comments on commit 2c782a3

Please sign in to comment.