Skip to content

Commit 35eba15

Browse files
committed
feat(extras): add vim colorscheme to extras
1 parent 1c49d0d commit 35eba15

File tree

3 files changed

+99
-9
lines changed

3 files changed

+99
-9
lines changed

lua/astrotheme/extras/init.lua

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@
55
local M = {}
66

77
-- map of plugin name to plugin extension
8-
--- @type table<string, {ext:string, url:string, label:string, subdir?: string, sep?:string}>
8+
--- @type table<string, {ext:string, url:string, label:string, subdir?: string}>
99
-- stylua: ignore
1010
M.extras = {
1111
-- Keep all entries here aligned by the first `=` sign
1212
wezterm = { ext = "toml", url = "https://wezfurlong.org/wezterm/config/files.html", label = "WezTerm" },
13+
vim = { ext = "vim", url = "https://vimhelp.org/", label = "Vim", subdir = "colors"},
1314
}
1415

1516
function M.setup()
16-
local config, util = require("astrotheme").config, require "astrotheme.lib.util"
17+
local util = require "astrotheme.lib.util"
1718

1819
-- map of style to style name
1920
local palettes = {
@@ -31,16 +32,11 @@ function M.setup()
3132
local info = M.extras[extra]
3233
local plugin = require("astrotheme.extras." .. extra)
3334
for palette, palette_name in pairs(palettes) do
35+
local config = require("astrotheme.lib.config").default
3436
config.palette, config.plugin_default = palette, true
3537
local colors = util.set_palettes(config)
3638
local highlights = util.get_highlights(colors, config)
37-
local fname = extra
38-
.. (info.subdir and "/" .. info.subdir .. "/" or "")
39-
.. "/astrotheme"
40-
.. (info.sep or "_")
41-
.. palette
42-
.. "."
43-
.. info.ext
39+
local fname = extra .. (info.subdir and "/" .. info.subdir or "") .. "/" .. palette .. "." .. info.ext
4440
colors["_upstream_url"] = "https://github.com/AstroNvim/astrotheme/raw/main/extras/" .. fname
4541
colors["_style_name"] = "AstroTheme " .. palette_name
4642
colors["_name"] = "astrotheme_" .. palette

lua/astrotheme/extras/vim.lua

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
local M = {}
2+
3+
local mapping = {
4+
fg = "guifg",
5+
bg = "guibg",
6+
sp = "guisp",
7+
}
8+
9+
--- @param colors AstroThemePalette
10+
--- @param highlights table<string, vim.api.keyset.highlight>
11+
--- @param opts AstroThemeOpts
12+
function M.generate(colors, highlights, opts)
13+
opts.plugin_default = false
14+
for p, n in pairs(require "astrotheme.groups.plugins") do
15+
if not p:find "nvim" then opts.plugins[n] = true end
16+
end
17+
highlights = require("astrotheme.lib.util").get_highlights(colors, opts)
18+
local lines = {
19+
([[
20+
let g:colors_name = "%s"
21+
hi clear
22+
]]):format(colors._style),
23+
}
24+
25+
highlights = vim.deepcopy(highlights)
26+
for name in pairs(highlights) do
27+
if name:sub(1, 1) == "@" then highlights[name] = nil end
28+
end
29+
local names = vim.tbl_keys(highlights)
30+
table.sort(names)
31+
32+
local used = {}
33+
for _, name in ipairs(names) do
34+
local hl = highlights[name]
35+
if type(hl) == "string" then hl = { link = hl } end
36+
37+
if not hl.link then
38+
local props = {}
39+
40+
-- fg/bg/sp
41+
for k, v in pairs(hl) do
42+
if mapping[k] then props[#props + 1] = ("%s=%s"):format(mapping[k], v) end
43+
end
44+
45+
-- gui
46+
local gui = {}
47+
for _, attr in ipairs {
48+
"bold",
49+
"underline",
50+
"undercurl",
51+
"italic",
52+
"strikethrough",
53+
"underdouble",
54+
"underdotted",
55+
"underdashed",
56+
"inverse",
57+
"standout",
58+
"nocombine",
59+
"altfont",
60+
} do
61+
if hl[attr] then gui[#gui + 1] = attr end
62+
end
63+
if #gui > 0 then props[#props + 1] = ("gui=%s"):format(table.concat(gui, ",")) end
64+
65+
if #props > 0 then
66+
if not hl.bg then props[#props + 1] = "guibg=NONE" end
67+
table.sort(props)
68+
used[name] = true
69+
lines[#lines + 1] = ("hi %s %s"):format(name, table.concat(props, " "))
70+
else
71+
print("astrotheme: invalid highlight group: " .. name)
72+
end
73+
end
74+
end
75+
76+
for _, name in ipairs(names) do
77+
local hl = highlights[name]
78+
if type(hl) == "string" then hl = { link = hl } end
79+
80+
if hl.link then
81+
if hl.link:sub(1, 1) ~= "@" and highlights[hl.link] and used[hl.link] then
82+
lines[#lines + 1] = ("hi! link %s %s"):format(name, hl.link)
83+
end
84+
end
85+
end
86+
87+
return table.concat(lines, "\n")
88+
end
89+
90+
return M

lua/astrotheme/types.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@
7373
---@field syntax AstroThemePaletteSyntax? Colors that control syntax related highlight groups
7474
---@field term AstroThemePaletteTerm? Colors that control colors set for in-editor terminals
7575
---@field ui AstroThemePaletteUI? Colors that are used throughout the general user interface
76+
---@field _upstream_url string?
77+
---@field _style_name string?
78+
---@field _name string?
79+
---@field _style string?
7680

7781
---@class AstroThemeHighlightOpts
7882
---@field modify_hl_groups fun(hl: AstroThemeHighlights, c: AstroThemePalette)? Function for fine control over highlight setting. The first parameter is the table of highlights to modify directly, the second is the current palette of colors

0 commit comments

Comments
 (0)