Skip to content

Commit

Permalink
Merge pull request #9 from ful1e5/master
Browse files Browse the repository at this point in the history
- Fixes inactiveStatusLine bug
- Allows nested color overrides
  • Loading branch information
monsonjeremy authored Jul 28, 2021
2 parents 3241b37 + ef69a67 commit 53713a9
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 86 deletions.
28 changes: 17 additions & 11 deletions lua/onedark/theme.lua
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ function M.setup(config)
-- LspDiagnosticsSignHint = { }, -- Used for "Hint" signs in sign column
}


theme.plugins = {

-- These groups are for the neovim tree-sitter highlights.
Expand All @@ -204,7 +203,7 @@ function M.setup(config)
TSDanger = { fg = c.bg, bg = c.error },
TSConstructor = { fg = c.red }, -- For constructor calls and definitions: `= { }` in Lua, and Java constructors.
-- TSConditional = { }; -- For keywords related to conditionnals.
TSConstant = { fg = c.yellow }; -- For constants
TSConstant = { fg = c.yellow }, -- For constants
-- TSConstBuiltin = { }; -- For constant that are built in the language: `nil` in Lua.
-- TSConstMacro = { }; -- For constants that are defined by macros: `NULL` in C.
-- TSError = { }; -- For syntax/parser errors.
Expand All @@ -214,12 +213,12 @@ function M.setup(config)
-- TSFunction = { }; -- For function (calls and definitions).
-- TSFuncBuiltin = { }; -- For builtin functions: `table.insert` in Lua.
-- TSFuncMacro = { }; -- For macro defined fuctions (calls and definitions): each `macro_rules` in Rust.
TSInclude = { fg = c.purple }; -- For includes: `#include` in C, `use` or `extern crate` in Rust, or `require` in Lua.
TSInclude = { fg = c.purple }, -- For includes: `#include` in C, `use` or `extern crate` in Rust, or `require` in Lua.
TSKeyword = { fg = c.purple, style = config.keywordStyle }, -- For keywords that don't fall in previous categories.
TSKeywordFunction = { fg = c.purple, style = config.functionStyle }, -- For keywords used to define a fuction.
TSLabel = { fg = c.blue }, -- For labels: `label:` in C and `:label:` in Lua.
-- TSMethod = { }; -- For method calls and definitions.
TSNamespace = { fg = c.red }; -- For identifiers referring to modules and namespaces.
TSNamespace = { fg = c.red }, -- For identifiers referring to modules and namespaces.
-- TSNone = { }; -- TODO: docs
-- TSNumber = { }; -- For all numbers
TSOperator = { fg = c.fg }, -- For any operator: `+`, but also `->` and `*` in C.
Expand All @@ -239,7 +238,7 @@ function M.setup(config)
TSVariable = { style = config.variableStyle }, -- Any variable name that does not have another highlight.
TSVariableBuiltin = { fg = c.red }, -- Variable names that are defined by the languages, like `this` or `self`.

TSTag = { fg = c.red }; -- Tags like html tag names.
TSTag = { fg = c.red }, -- Tags like html tag names.
-- TSTagDelimiter = { }; -- Tag delimiter like `<` `>` `/`
-- TSText = { }; -- For strings considered text in a markup language.
TSTextReference = { fg = c.red }, -- FIXME
Expand Down Expand Up @@ -304,7 +303,7 @@ function M.setup(config)
NvimTreeIndentMarker = { fg = c.fg_gutter },
NvimTreeImageFile = { fg = c.fg_sidebar },
NvimTreeSymlink = { fg = c.purple },
NvimTreeFolderName= { fg = c.blue },
NvimTreeFolderName = { fg = c.blue },
LspDiagnosticsError = { fg = c.error },
LspDiagnosticsWarning = { fg = c.warning },
LspDiagnosticsInformation = { fg = c.info },
Expand Down Expand Up @@ -342,7 +341,7 @@ function M.setup(config)

-- ALE
ALEWarningSign = { fg = c.yellow },
ALEErrorSign = { fg = c.red },
ALEErrorSign = { fg = c.red },

-- Hop
HopNextKey = { fg = c.purple, style = "bold" },
Expand All @@ -356,12 +355,19 @@ function M.setup(config)
if config.hideInactiveStatusline then
local inactive = { style = "underline", bg = c.bg, fg = c.bg, sp = c.border }

-- StatusLineNC
-- StatusLine
theme.base.StatusLineNC = inactive

-- LuaLine
for _, section in ipairs({ "a", "b", "c" }) do
theme.plugins["lualine_" .. section .. "_inactive"] = inactive
if vim.o.statusline ~= nil and string.find(vim.o.statusline, "lualine") then
-- Fix VertSplit & StatusLine crossover when lualine is active
-- https://github.com/ful1e5/onedark.nvim/issues/2
-- https://github.com/hoob3rt/lualine.nvim/issues/274
theme.base.StatusLine = { bg = c.bg }

-- LuaLine
for _, section in pairs({ "a", "b", "c" }) do
theme.plugins["lualine_" .. section .. "_inactive"] = inactive
end
end
end

Expand Down
119 changes: 44 additions & 75 deletions lua/onedark/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,13 @@ function util.blend(fg, bg, alpha)
return string.format("#%02X%02X%02X", blendChannel(1), blendChannel(2), blendChannel(3))
end

function util.darken(hex, amount, bg)
return util.blend(hex, bg or util.bg, math.abs(amount))
end
function util.lighten(hex, amount, fg)
return util.blend(hex, fg or util.fg, math.abs(amount))
end
function util.darken(hex, amount, bg) return util.blend(hex, bg or util.bg, math.abs(amount)) end
function util.lighten(hex, amount, fg) return util.blend(hex, fg or util.fg, math.abs(amount)) end

function util.brighten(color, percentage)
local hsl = hsluv.hex_to_hsluv(color)
local larpSpace = 100 - hsl[3]
if percentage < 0 then
larpSpace = hsl[3]
end
if percentage < 0 then larpSpace = hsl[3] end
hsl[3] = hsl[3] + larpSpace * percentage
return hsluv.hsluv_to_hex(hsl)
end
Expand All @@ -55,9 +49,7 @@ function util.invertColor(color)
if color ~= "NONE" then
local hsl = hsluv.hex_to_hsluv(color)
hsl[3] = 100 - hsl[3]
if hsl[3] < 40 then
hsl[3] = hsl[3] + (100 - hsl[3]) * 0.3
end
if hsl[3] < 40 then hsl[3] = hsl[3] + (100 - hsl[3]) * 0.3 end
return hsluv.hsluv_to_hex(hsl)
end
return color
Expand All @@ -73,26 +65,16 @@ function util.randomColor(color)
end

function util.getColor(color)
if vim.o.background == "dark" then
return color
end
if not util.colorCache[color] then
util.colorCache[color] = util.invertColor(color)
end
if vim.o.background == "dark" then return color end
if not util.colorCache[color] then util.colorCache[color] = util.invertColor(color) end
return util.colorCache[color]
end

-- local ns = vim.api.nvim_create_namespace("onedark")
function util.highlight(group, color)
if color.fg then
util.colorsUsed[color.fg] = true
end
if color.bg then
util.colorsUsed[color.bg] = true
end
if color.sp then
util.colorsUsed[color.sp] = true
end
if color.fg then util.colorsUsed[color.fg] = true end
if color.bg then util.colorsUsed[color.bg] = true end
if color.sp then util.colorsUsed[color.sp] = true end

local style = color.style and "gui=" .. color.style or "gui=NONE"
local fg = color.fg and "guifg=" .. util.getColor(color.fg) or "guifg=NONE"
Expand Down Expand Up @@ -121,9 +103,7 @@ function util.debug(colors)
if type(color) == "table" then
util.debug(color)
else
if util.colorsUsed[color] == nil then
print("not used: " .. name .. " : " .. color)
end
if util.colorsUsed[color] == nil then print("not used: " .. name .. " : " .. color) end
end
end
end
Expand All @@ -148,7 +128,8 @@ function util.autocmds(config)
if sidebar == "terminal" then
vim.cmd([[ autocmd TermOpen * setlocal winhighlight=Normal:NormalSB,SignColumn:SignColumnSB]])
else
vim.cmd([[ autocmd FileType ]] .. sidebar .. [[ setlocal winhighlight=Normal:NormalSB,SignColumn:SignColumnSB]])
vim.cmd([[ autocmd FileType ]] .. sidebar ..
[[ setlocal winhighlight=Normal:NormalSB,SignColumn:SignColumnSB]])
end
end
vim.cmd([[augroup end]])
Expand All @@ -161,16 +142,10 @@ end
---@param str string template string
---@param table table key value pairs to replace in the string
function util.template(str, table)
return (str:gsub("($%b{})", function(w)
return table[w:sub(3, -2)] or w
end))
return (str:gsub("($%b{})", function(w) return table[w:sub(3, -2)] or w end))
end

function util.syntax(syntax)
for group, colors in pairs(syntax) do
util.highlight(group, colors)
end
end
function util.syntax(syntax) for group, colors in pairs(syntax) do util.highlight(group, colors) end end

---@param colors ColorScheme
function util.terminal(colors)
Expand Down Expand Up @@ -209,53 +184,53 @@ function util.terminal(colors)
end

function util.light_colors(colors)
if type(colors) == "string" then
return util.getColor(colors)
end
if type(colors) == "string" then return util.getColor(colors) end
local ret = {}
for key, value in pairs(colors) do
ret[key] = util.light_colors(value)
end
for key, value in pairs(colors) do ret[key] = util.light_colors(value) end
return ret
end

---@param theme Theme
function util.load(theme)
-- only needed to clear when not the default colorscheme
if vim.g.colors_name then
vim.cmd("hi clear")
end
-- if vim.fn.exists("syntax_on") then
-- vim.cmd("syntax reset")
-- end
vim.cmd("hi clear")
if vim.fn.exists("syntax_on") then vim.cmd("syntax reset") end

vim.o.termguicolors = true
vim.g.colors_name = "onedark"
-- vim.api.nvim__set_hl_ns(ns)
-- load base theme
util.syntax(theme.base)
util.syntax(theme.plugins)
util.terminal(theme.colors)
util.autocmds(theme.config)

-- load syntax for plugins and terminal async
vim.defer_fn(function()
util.terminal(theme.colors)
util.syntax(theme.plugins)
util.autocmds(theme.config)
end, 0)
end

---@param config Config
---@param colors ColorScheme
---@param config Config
function util.color_overrides(colors, config)
if type(config.colors) == "table" then
for key, value in pairs(config.colors) do
if not colors[key] then
error("Color " .. key .. " does not exist")
end
if string.sub(value, 1, 1) == "#" then
-- hex override
colors[key] = value
if not colors[key] then error("Color " .. key .. " does not exist") end

-- Patch: https://github.com/ful1e5/onedark.nvim/issues/6
if type(colors[key]) == "table" then
util.color_overrides(colors[key], { colors = value })
else
-- another group
if not colors[value] then
error("Color " .. value .. " does not exist")
if value:lower() == "none" then
-- set to none
colors[key] = "NONE"
elseif string.sub(value, 1, 1) == "#" then
-- hex override
colors[key] = value
else
-- another group
if not colors[value] then error("Color " .. value .. " does not exist") end
colors[key] = colors[value]
end
colors[key] = colors[value]
end
end
end
Expand All @@ -268,17 +243,13 @@ function util.light(brightness)
if type(hl[key]) == "number" then
local hex = string.format("#%06x", hl[key])
local color = util.invertColor(hex)
if brightness then
color = util.brighten(hex, brightness)
end
if brightness then color = util.brighten(hex, brightness) end
table.insert(def, "gui" .. def_key .. "=" .. color)
end
end
if hl_name ~= "" and #def > 0 then
for _, style in pairs({ "bold", "italic", "underline", "undercurl", "reverse" }) do
if hl[style] then
table.insert(def, "gui=" .. style)
end
if hl[style] then table.insert(def, "gui=" .. style) end
end

vim.cmd("highlight! " .. hl_name .. " " .. table.concat(def, " "))
Expand All @@ -300,9 +271,7 @@ function util.random()
end
if hl_name ~= "" and #def > 0 then
for _, style in pairs({ "bold", "italic", "underline", "undercurl", "reverse" }) do
if hl[style] then
table.insert(def, "gui=" .. style)
end
if hl[style] then table.insert(def, "gui=" .. style) end
end

vim.cmd("highlight! " .. hl_name .. " " .. table.concat(def, " "))
Expand Down

0 comments on commit 53713a9

Please sign in to comment.