|
| 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 |
0 commit comments