Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci(primitives): transition to new css primitives #338

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 145 additions & 0 deletions .github/workflows/csstolua.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
local res = {}

local function is_valid_ident(ident)
local keyword = {
['do'] = true,
['end'] = true,
['if'] = true,
['then'] = true,
['local'] = true,
['function'] = true,
['return'] = true,
['while'] = true,
['repeat'] = true,
['until'] = true,
['for'] = true,
['in'] = true,
['true'] = true,
['false'] = true,
['nil'] = true,
}

if type(ident) ~= 'string' or keyword[ident] then
return false
end

return ident:find('^[_%a][_%w]*$') ~= nil
end

local function set(cssvar, v)
local before, last = cssvar:match('^(.+)%-+(.+)$')
print(('\ncss variable: %s'):format(cssvar))
print(('css variable prefix: %s'):format(before))
print(('css variable suffix: %s'):format(last))
print(('css variable value: %s'):format(v))

-- Top-level key
if not last then
res[tonumber(cssvar) or cssvar] = v
return
end

last = tonumber(last) or last
local cur = res
for k in before:gmatch('[^%-_]+') do
k = tonumber(k) or k
cur[k] = cur[k] or {}
cur = cur[k]
end

-- Path is too short: append `default`
if type(cur[last]) == 'table' then
local suffix = 'default'
print(('keypath is too short, appending suffix "%s"'):format(suffix))
print(('new name: %s-%s'):format(cssvar, suffix))
cur, last = cur[last], suffix
end

-- Check that duplicates have the same value
if cur[last] ~= nil and cur[last] ~= v then
error(([[
variable appears multiple times with different values:
- %s
- %s
]]):format(cur[last], v))
end

cur[last] = v
end

local function print_recur(value, _ind)
_ind = _ind or 0

if type(value) == 'table' then
io.write('m {')
_ind = _ind + 2

for k, v in pairs(value) do
local fmt = '[%q] = '
if type(k) == 'number' then
fmt = '[%s] = '
elseif is_valid_ident(k) then
fmt = '%s = '
end
io.write(('\n%s' .. fmt):format((' '):rep(_ind), k))
print_recur(v, _ind)
io.write(',')
end

_ind = _ind - 2
io.write(('\n%s}'):format((' '):rep(_ind)))
else
io.write(('%q'):format(value))
end
end

local defs = {}
for ln in io.lines() do
local k, v = ln:match('^%s*%-%-(%w.-)%s*:%s*(.-)%s*;%s*$')
if k then
table.insert(defs, { k, v })
end
end

-- Since we are un-flattening, ensure that longer keys (whose prefix could
-- match another key) are visited first.
table.sort(defs, function(a, b)
return a[1] > b[1]
end)

for _, kv in ipairs(defs) do
set(unpack(kv))
end

-- Add `scale` key for convenience and backwards-compatibility
assert(res.scale == nil)
res.scale = {}
for color, scale in pairs(res.base.color) do
if type(scale) == 'table' then
res.scale[color] = {}
for i, v in pairs(scale) do
res.scale[color][i + 1] = v
end
else
res.scale[color] = scale
end
end

-- NOTE: the metatable `mt` helps to catch errors (e.g. during CI tests)
io.write([=[
local mt = {
__index = function(_, k)
error('invalid index: ' .. k)
end,
}
---@generic T
---@param tbl T
---@return T
local function m(tbl)
return setmetatable(tbl, mt)
end
local M = ]=])

print_recur(res)

io.write('\n')
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
name: Get/Update Primer Color Primitives
name: Get/Update Primer Primitives

env:
_DEST_DIR: '${{ github.workspace }}/lua/github-theme/palette/primitives'
_JSON_DIR: '${{ github.workspace }}/node_modules/@primer/primitives/dist/json/colors'
_SRC_DIR: '${{ github.workspace }}/node_modules/@primer/primitives/dist/internalCss'
_LICENSE_GLOB: '${{ github.workspace }}/node_modules/@primer/primitives/[Ll][Ii][Cc][Ee][Nn][Ss][Ee]*'
_PRIMITIVES_PKGJSON: '${{ github.workspace }}/node_modules/@primer/primitives/package.json'
_CSSTOLUA: '${{ github.workspace }}/.github/workflows/csstolua.lua'

on:
workflow_dispatch:
Expand All @@ -22,7 +23,7 @@ on:
- scripts/**/*

jobs:
get-colors:
install-primitives:
runs-on: ubuntu-latest
permissions: # NOTE: `write` is not applied for PR's from forks
checks: write
Expand All @@ -34,6 +35,9 @@ jobs:

steps:
- uses: actions/checkout@v4
- uses: rhysd/action-setup-vim@v1
with:
neovim: true

- uses: actions/setup-node@v4
with:
Expand All @@ -44,21 +48,22 @@ jobs:

- name: Generate Lua files
run: |
set -u +f
set -eu +f
shopt -s nocaseglob failglob
license="$(<$_LICENSE_GLOB)"
rm -r "$_DEST_DIR" || :
mkdir -p "$_DEST_DIR"
cd "$_JSON_DIR"
cd "$_SRC_DIR"

if jq -e .version "$_PRIMITIVES_PKGJSON"; then
version="M._VERSION = vim.json.decode([=[$(jq -e .version "$_PRIMITIVES_PKGJSON")]=], { luanil = { object = false, array = false } })"
fi

for file in *.json; do
cat >|"${_DEST_DIR}/${file%.json}.lua" <<EOF
for file in *.css; do
values="$(nvim -l "$_CSSTOLUA" < "$file")"
cat >| "${_DEST_DIR}/$(tr '-' '_' <<< "${file%.css}.lua")" <<EOF
-- NOTE: THIS IS AN AUTO-GENERATED FILE. DO NOT EDIT BY-HAND.
local M = vim.json.decode([=[$(<"$file")]=], { luanil = { object = false, array = false } })
${values}
${version-}
M._LICENSE = [=[
$license]=]
Expand Down Expand Up @@ -90,8 +95,8 @@ jobs:
with:
delete-branch: true
branch: generated/primitives/${{ github.ref_name }}
commit-message: 'deps: update color primitives'
title: 'deps: update color primitives'
commit-message: 'deps(primer): update primitives'
title: 'deps(primer): update primitives'
body: |
| | |
| ----------------- | ------------------------ |
Expand Down
4 changes: 2 additions & 2 deletions lua/github-theme/group/modules/treesitter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function M.get(spec, config, _opts)
:gsub('^github_(.-)$', '%1')
)

local pl = primitives.prettylights
local pl = primitives.color.prettylights
local syn = spec.syntax
local stl = config.styles
local P = spec.palette
Expand Down Expand Up @@ -229,7 +229,7 @@ If you want to stay on nvim 0.7, disable the module, or track on 'v0.0.x' branch
['@module.c_sharp'] = { fg = pl.syntax.variable },

-- Gitignore
['@string.special.path.gitignore'] = { fg = pl.syntax.entity }, -- Non-special chars in file pattern
['@string.special.path.gitignore'] = { fg = pl.syntax.entity.default }, -- Non-special chars in file pattern

-- Go
-- ['@function.call.go'] = { link = '@constant' },
Expand Down
66 changes: 32 additions & 34 deletions lua/github-theme/palette/github_dark.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,54 @@ local meta = {
light = false,
}

local primitives =
require('github-theme.palette.primitives.' .. meta.name:gsub('^github%W*', '', 1))

local pl = primitives.prettylights
local primitives = require('github-theme.palette.primitives.dark')
local pl = primitives.color.prettylights
local scale = primitives.scale

C.WHITE = C(scale.white)
C.BLACK = C(scale.black)
C.BG = C(scale.gray[7])
local BG = C(scale.gray[7])
C.BG = C(scale.neutral[7])
local BG = C(scale.neutral[7])

local palette = {
scale = scale,

orange = scale.orange[4],

black = { base = scale.gray[10], bright = scale.gray[9] },
gray = { base = scale.gray[5], bright = scale.gray[5] },
black = { base = scale.neutral[10], bright = scale.neutral[9] },
gray = { base = scale.neutral[5], bright = scale.neutral[5] },
blue = { base = scale.blue[4], bright = scale.blue[3] },
green = { base = scale.green[4], bright = scale.green[3] },
magenta = { base = scale.purple[4], bright = scale.purple[3] },
pink = { base = scale.pink[4], bright = scale.pink[3] },
red = { base = scale.red[4], bright = scale.red[3] },
white = { base = scale.gray[3], bright = scale.gray[3] },
white = { base = scale.neutral[3], bright = scale.neutral[3] },
yellow = { base = scale.yellow[4], bright = scale.yellow[3] },
cyan = { base = '#76e3ea', bright = '#b3f0ff' },

fg = {
default = '#e6edf3',
muted = '#7d8590',
subtle = scale.gray[5],
subtle = scale.neutral[5],
on_emphasis = scale.white,
},

canvas = {
default = scale.gray[7],
overlay = scale.gray[9],
inset = scale.gray[8],
subtle = scale.gray[9],
default = scale.neutral[7],
overlay = scale.neutral[9],
inset = scale.neutral[8],
subtle = scale.neutral[9],
},

border = {
default = scale.gray[9],
muted = scale.gray[8],
default = scale.neutral[9],
muted = scale.neutral[8],
subtle = BG:blend(C.from_rgba(240, 246, 252, 1), 0.1):to_css(),
},

neutral = {
emphasis_plus = scale.gray[5],
emphasis = scale.gray[5],
emphasis_plus = scale.neutral[5],
emphasis = scale.neutral[5],
muted = BG:blend(C.from_rgba(110, 118, 129, 1), 0.4):to_css(),
subtle = BG:blend(C.from_rgba(110, 118, 129, 1), 0.1):to_css(),
},
Expand Down Expand Up @@ -126,16 +124,16 @@ local palette = {
local function generate_spec(pal)
-- stylua: ignore start
local spec = {
bg0 = BG:blend(C(pal.canvas.inset), 0.75):to_css(), -- Dark bg (popup and float)
bg1 = pal.canvas.default, -- Default bg
bg0 = BG:blend(C(primitives.bgColor.inset), 0.75):to_css(), -- Dark bg (popup and float)
bg1 = primitives.bgColor.default, -- Default bg
bg2 = BG:blend(C(pal.neutral.emphasis), 0.1):to_css(), -- Lighter bg (colorcolumn Folds)
bg3 = pal.scale.gray[6], -- Lighter bg (cursor line)
bg4 = pal.scale.gray[4], -- Conceal
bg3 = pal.scale.neutral[6], -- Lighter bg (cursor line)
bg4 = pal.scale.neutral[4], -- Conceal

fg0 = pal.fg.subtle, -- Lighter fg
fg1 = pal.fg.default, -- Default fg
fg1 = primitives.fgColor.default, -- Default fg
fg2 = pal.fg.muted, -- Darker fg (status line)
fg3 = pal.scale.gray[5], -- Darker fg (line numbers, fold columns)
fg3 = pal.scale.neutral[5], -- Darker fg (line numbers, fold columns)

sel0 = BG:blend(C(pal.accent.fg), 0.30):to_css(), -- Visual selection bg
sel1 = BG:blend(C(pal.accent.muted), 0.90):to_css(), -- Popup sel bg
Expand All @@ -144,26 +142,26 @@ local function generate_spec(pal)

spec.syntax = {
bracket = spec.fg1, -- Brackets and Punctuation
builtin0 = pl.syntax.constant, -- Builtin variable
builtin0 = pl.syntax.constant.default, -- Builtin variable
builtin1 = pl.syntax.keyword, -- Builtin type
builtin2 = pl.syntax.constant, -- Builtin const
builtin2 = pl.syntax.constant.default, -- Builtin const
comment = pl.syntax.comment, -- Comment
conditional = pl.syntax.keyword, -- Conditional and loop
const = pl.syntax.constant, -- Constants, imports and booleans
const = pl.syntax.constant.default, -- Constants, imports and booleans
dep = pal.scale.red[3], -- Deprecated
field = pl.syntax.constant, -- Field
func = pl.syntax.entity, -- Functions and Titles
field = pl.syntax.constant.default, -- Field
func = pl.syntax.entity.default, -- Functions and Titles
ident = spec.fg1, -- Identifiers
keyword = pl.syntax.keyword, -- Keywords
number = pl.syntax.constant, -- Numbers
operator = pl.syntax.constant, -- Operators
number = pl.syntax.constant.default, -- Numbers
operator = pl.syntax.constant.default, -- Operators
param = spec.fg1, -- Parameters
preproc = pl.syntax.keyword, -- PreProc
regex = pl.syntax.string, -- Regex
regex = pl.syntax.string.default, -- Regex
statement = pl.syntax.keyword, -- Statements
string = pl.syntax.string, -- Strings
string = pl.syntax.string.default, -- Strings
type = pl.syntax.variable, -- Types
tag = pl.syntax.entityTag, -- Tags
tag = pl.syntax.entity.tag, -- Tags
variable = spec.fg1, -- Variables
}

Expand Down
Loading
Loading