Skip to content

Commit

Permalink
refactor: clean up libs
Browse files Browse the repository at this point in the history
  • Loading branch information
nikbrunner committed Sep 1, 2024
1 parent 4f8ff21 commit 4198ccf
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 93 deletions.
4 changes: 2 additions & 2 deletions lua/black-atom/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ local default_config = {
---Sets options for the BlackAtom.Config during runtime
---@param options BlackAtom.Config
---@return nil
function M:set(options)
function M.set(options)
local validate = require("black-atom.lib.validate")

local current_config = vim.g.black_atom_core_config or {}
Expand All @@ -52,7 +52,7 @@ end

---Returns the current BlackAtom.Config
---@return BlackAtom.Config | any
function M:get()
function M.get()
return vim.g.black_atom_core_config
end

Expand Down
38 changes: 12 additions & 26 deletions lua/black-atom/highlights/lsp.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
local lib = require("black-atom.lib")

---@type BlackAtom.HighlightsSpec
local highlight_map_extension = {
map = function(colors, config)
local cond_hl = require("black-atom.lib.highlights").cond_hl
local darken = require("black-atom.lib.color").darken
local styles = config.styles or {}

local diagnostics_error_color = colors.palette.red
local diagnostics_warn_color = colors.palette.yellow
local diagnostics_hint_color = colors.palette.blue
Expand All @@ -22,42 +24,26 @@ local highlight_map_extension = {
DiagnosticWarn = { fg = colors.palette.yellow },

DiagnosticVirtualTextError = {
bg = lib.highlights.conditional_hl(colors.none, {
[config.styles.diagnostics.background] = lib.color.darken(
diagnostics_error_color,
0.1,
colors.ui.bg.primary.main
),
bg = cond_hl(colors.none, {
[styles.diagnostics.background] = darken(diagnostics_error_color, 0.1, colors.ui.bg.primary.main),
}),
fg = diagnostics_error_color,
},
DiagnosticVirtualTextWarn = {
bg = lib.highlights.conditional_hl(colors.none, {
[config.styles.diagnostics.background] = lib.color.darken(
diagnostics_warn_color,
0.1,
colors.ui.bg.primary.main
),
bg = cond_hl(colors.none, {
[styles.diagnostics.background] = darken(diagnostics_warn_color, 0.1, colors.ui.bg.primary.main),
}),
fg = diagnostics_warn_color,
},
DiagnosticVirtualTextInfo = {
bg = lib.highlights.conditional_hl(colors.none, {
[config.styles.diagnostics.background] = lib.color.darken(
diagnostics_info_color,
0.1,
colors.ui.bg.primary.main
),
bg = cond_hl(colors.none, {
[styles.diagnostics.background] = darken(diagnostics_info_color, 0.1, colors.ui.bg.primary.main),
}),
fg = diagnostics_info_color,
},
DiagnosticVirtualTextHint = {
bg = lib.highlights.conditional_hl(colors.none, {
[config.styles.diagnostics.background] = lib.color.darken(
diagnostics_hint_color,
0.1,
colors.ui.bg.primary.main
),
bg = cond_hl(colors.none, {
[styles.diagnostics.background] = darken(diagnostics_hint_color, 0.1, colors.ui.bg.primary.main),
}),
fg = diagnostics_hint_color,
},
Expand Down
44 changes: 19 additions & 25 deletions lua/black-atom/highlights/syntax.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ return {
map = function(colors, config)
local s = colors.syntax
-- TODO: Rename to configurable globally
local configurable = require("black-atom.lib.highlights").extend_hl
local ext_hl = require("black-atom.lib.highlights").ext_hl

-- Treesitter Syntax Highlights (See: `:h treesitter-highlight-groups`)
---@type BlackAtom.Highlights
Expand All @@ -12,22 +12,19 @@ return {
-- TODO: Rename this file to `syntax.lua` and bring in the builtin types as well
-- TODO: Sort

Identifier = configurable({ fg = s.variable.default }, config.styles.syntax.variables),
Identifier = ext_hl({ fg = s.variable.default }, config.styles.syntax.variables),
StorageClass = { link = "Identifier" },
["@variable"] = { link = "Identifier" },
["@variable.builtin"] = { fg = s.variable.builtin },
["@variable.parameter"] = { fg = s.variable.parameter, italic = true, bold = true },
["@variable.member"] = { fg = s.variable.member, italic = true },

String = configurable({ fg = s.string.default }, config.styles.syntax.strings),
String = ext_hl({ fg = s.string.default }, config.styles.syntax.strings),
["@string"] = { link = "String" },
["@string.escape"] = configurable({ fg = s.string.escape }, config.styles.syntax.strings),
["@string.regexp"] = configurable({ fg = s.string.regexp }, config.styles.syntax.strings),
["@string.special"] = configurable({ fg = s.string.default }, config.styles.syntax.strings),
["@string.special.url"] = configurable(
{ fg = s.string.default, underline = true },
config.styles.syntax.strings
),
["@string.escape"] = ext_hl({ fg = s.string.escape }, config.styles.syntax.strings),
["@string.regexp"] = ext_hl({ fg = s.string.regexp }, config.styles.syntax.strings),
["@string.special"] = ext_hl({ fg = s.string.default }, config.styles.syntax.strings),
["@string.special.url"] = ext_hl({ fg = s.string.default, underline = true }, config.styles.syntax.strings),

Boolean = { fg = s.boolean.default },
["@boolean"] = { link = "Boolean" },
Expand All @@ -39,10 +36,10 @@ return {
-- We assign the `Keyword` highlight to some of the other regexpt highlight groups,
-- which are also keywords more of less.
-- To me thats find for now, but I'm open to suggestions.
Keyword = configurable({ fg = s.keyword.default }, config.styles.syntax.keywords),
Keyword = ext_hl({ fg = s.keyword.default }, config.styles.syntax.keywords),
["@keyword"] = { link = "Keyword" },
["@keyword.import"] = configurable({ fg = s.keyword.import }, config.styles.syntax.keywords),
["@keyword.export"] = configurable({ fg = s.keyword.export }, config.styles.syntax.keywords),
["@keyword.import"] = ext_hl({ fg = s.keyword.import }, config.styles.syntax.keywords),
["@keyword.export"] = ext_hl({ fg = s.keyword.export }, config.styles.syntax.keywords),
Statement = { link = "Keyword" },
Conditional = { link = "Keyword" },
Repeat = { link = "Keyword" },
Expand Down Expand Up @@ -78,18 +75,15 @@ return {

["@property"] = { fg = s.property.default },

Function = configurable({ fg = s.func.default }, config.styles.syntax.functions),
Function = ext_hl({ fg = s.func.default }, config.styles.syntax.functions),
["@function"] = { link = "Function" },
["@function.call"] = configurable({ fg = s.func.default, bold = true }, config.styles.syntax.functions),
["@function.method.call"] = configurable(
{ fg = s.func.default, bold = true },
config.styles.syntax.functions
),
["@function.builtin"] = configurable({ fg = s.func.builtin }, config.styles.syntax.functions),
["@function.method"] = configurable({ fg = s.func.method }, config.styles.syntax.functions),
["@function.call"] = ext_hl({ fg = s.func.default, bold = true }, config.styles.syntax.functions),
["@function.method.call"] = ext_hl({ fg = s.func.default, bold = true }, config.styles.syntax.functions),
["@function.builtin"] = ext_hl({ fg = s.func.builtin }, config.styles.syntax.functions),
["@function.method"] = ext_hl({ fg = s.func.method }, config.styles.syntax.functions),

["@method"] = configurable({ fg = s.func.default }, config.styles.syntax.functions),
["@method.call"] = configurable({ fg = s.func.default, bold = true }, config.styles.syntax.functions),
["@method"] = ext_hl({ fg = s.func.default }, config.styles.syntax.functions),
["@method.call"] = ext_hl({ fg = s.func.default, bold = true }, config.styles.syntax.functions),

["@constructor"] = { fg = s.constructor.default, bold = true },

Expand All @@ -98,9 +92,9 @@ return {
["@punctuation.bracket"] = { fg = s.punctuation.bracket },
["@punctuation.delimiter"] = { fg = s.punctuation.delimiter },

Comment = configurable({ fg = s.comment.default }, config.styles.syntax.comments),
Comment = ext_hl({ fg = s.comment.default }, config.styles.syntax.comments),
Todo = { fg = s.comment.todo },
SpecialComment = configurable({ fg = s.comment.doc }, config.styles.syntax.comments),
SpecialComment = ext_hl({ fg = s.comment.doc }, config.styles.syntax.comments),
["@comment"] = { link = "Comment" },
["@comment.todo"] = { link = "Todo" },
["@comment.documentation"] = { link = "SpecialComment" },
Expand Down
19 changes: 8 additions & 11 deletions lua/black-atom/highlights/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
return {
map = function(colors, config)
local bg = require("black-atom.lib.bg")
local configurable = require("black-atom.lib.highlights").extend_hl
local ext_hl = require("black-atom.lib.highlights").ext_hl

local ui = colors.ui
local palette = colors.palette
Expand All @@ -12,10 +12,7 @@ return {
-- Basic Text
Normal = { fg = ui.fg.primary.main, bg = bg.main(config, colors) },
NormalNC = { fg = ui.fg.primary.main, bg = bg.main(config, colors) },
EndOfBuffer = configurable(
{ fg = bg.main(config, colors) },
{ [config.styles.ending_tildes] = ui.fg.neutral }
),
EndOfBuffer = ext_hl({ fg = bg.main(config, colors) }, { [config.styles.ending_tildes] = ui.fg.neutral }),
NonText = { fg = ui.fg.neutral },
Whitespace = { link = "NonText" },
SpecialKey = { link = "NonText" },
Expand Down Expand Up @@ -60,18 +57,18 @@ return {

-- Messages & Mode
Error = { fg = palette.red },
ErrorMsg = configurable({ fg = palette.red }, config.styles.syntax.messages),
MoreMsg = configurable({ fg = palette.blue }, config.styles.syntax.messages),
ModeMsg = configurable({ fg = ui.fg.primary.main }, config.styles.syntax.messages),
WarningMsg = configurable({ fg = palette.yellow }, config.styles.syntax.messages),
ErrorMsg = ext_hl({ fg = palette.red }, config.styles.syntax.messages),
MoreMsg = ext_hl({ fg = palette.blue }, config.styles.syntax.messages),
ModeMsg = ext_hl({ fg = ui.fg.primary.main }, config.styles.syntax.messages),
WarningMsg = ext_hl({ fg = palette.yellow }, config.styles.syntax.messages),

-- Window & Tab Management
Title = { fg = ui.fg.active },
StatusLine = configurable(
StatusLine = ext_hl(
{ fg = ui.fg.primary.main, bg = ui.bg.primary.dark },
{ [config.styles.transparency == "full"] = { bg = colors.none } }
),
StatusLineNC = configurable(
StatusLineNC = ext_hl(
{ fg = ui.fg.neutral, bg = ui.bg.primary.dark },
{ [config.styles.transparency == "full"] = { bg = colors.none } }
),
Expand Down
10 changes: 5 additions & 5 deletions lua/black-atom/init.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local Config = require("black-atom.config")
local config = require("black-atom.config")
local commands = require("black-atom.commands")
local highlights = require("black-atom.highlights")
local lib = require("black-atom.lib")
Expand All @@ -9,9 +9,9 @@ local M = {}
---@return nil
function M.setup(opts)
if not opts then
Config:set(Config.default)
config.set(config.default)
else
Config:set(opts)
config.set(opts)
end

commands.register()
Expand All @@ -21,7 +21,7 @@ end
---@param theme BlackAtom.Theme.Definition
---@return nil
function M.load(theme)
Config:set({ theme = theme.meta.key, collection = theme.meta.collection.key })
config.set({ theme = theme.meta.key, collection = theme.meta.collection.key })
lib.themes.dev_status_warning(theme.meta)

highlights.reset()
Expand All @@ -30,7 +30,7 @@ function M.load(theme)
vim.opt.termguicolors = true
vim.opt.background = theme.meta.appearance

highlights.apply(theme.colors, Config:get())
highlights.apply(theme.colors, config.get())
end

return M
8 changes: 5 additions & 3 deletions lua/black-atom/lib/bg.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
local cond_hl = require("black-atom.lib.highlights").cond_hl

local M = {}

---@param config BlackAtom.Config
Expand All @@ -7,7 +9,7 @@ function M.main(config, colors)
local no_color = colors.none
local bg = colors.ui.bg

return require("black-atom.lib.highlights").conditional_hl(bg.primary.main, {
return cond_hl(bg.primary.main, {
[config.styles.transparency == "partial"] = no_color,
[config.styles.transparency == "full"] = no_color,
})
Expand All @@ -22,7 +24,7 @@ function M.sidebar(config, colors)
local bg = colors.ui.bg
local no_color = colors.none

return require("black-atom.lib.highlights").conditional_hl(bg.primary.dark, {
return cond_hl(bg.primary.dark, {
[not dark_sidebars] = bg.primary.main,
[dark_sidebars and transparency == "partial"] = bg.primary.dark,
[transparency == "full"] = no_color,
Expand All @@ -36,7 +38,7 @@ end
function M.float(config, colors)
local bg = colors.ui.bg

return require("black-atom.lib.highlights").conditional_hl(bg.primary.dark, {
return cond_hl(bg.primary.dark, {
-- I decided to always use the dark background for floating windows
-- But just in case I change my mind, I'll leave this here
})
Expand Down
1 change: 1 addition & 0 deletions lua/black-atom/lib/debug.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ local lib_ui = require("black-atom.lib.ui")

local M = {}

-- TODO: DEV-164 Put these into constants.
M.pathes = {
debug_dir = lib_files.build_path(lib_files.get_plugin_path(), "debug"),
highlights_map_debug_filename = "highlights_debug_map.txt",
Expand Down
36 changes: 18 additions & 18 deletions lua/black-atom/lib/highlights.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,27 @@ local M = {}
---@param highlight BlackAtom.HighlightDefinition
---@param code_style? BlackAtom.HighlightDefinition
---@return BlackAtom.HighlightDefinition
function M.extend_hl(highlight, code_style)
function M.ext_hl(highlight, code_style)
return vim.tbl_deep_extend("force", highlight, code_style)
end

---Returns the appropriate highlight value based on the provided conditions.
---If no conditions evaluate to true, the default highlight value is returned.
---@param default_highlight string The default highlight value to return if no conditions are true.
---@param conditional_highlight_map table<boolean, string>
---@return string
function M.cond_hl(default_highlight, conditional_highlight_map)
local final_highlight = default_highlight

for condition, highlight in pairs(conditional_highlight_map) do
if condition then
final_highlight = highlight
end
end

return final_highlight
end

function M.building_error_notification(message)
local notification_opts = {
title = "Black Atom - Highlights Error",
Expand Down Expand Up @@ -119,21 +136,4 @@ function M.set_term(colors)
vim.g.terminal_color_15 = colors.palette.white
end

---Returns the appropriate highlight value based on the provided conditions.
---If no conditions evaluate to true, the default highlight value is returned.
---@param default_highlight string The default highlight value to return if no conditions are true.
---@param conditional_highlight_map table<boolean, string>
---@return string
function M.conditional_hl(default_highlight, conditional_highlight_map)
local final_highlight = default_highlight

for condition, highlight in pairs(conditional_highlight_map) do
if condition then
final_highlight = highlight
end
end

return final_highlight
end

return M
2 changes: 1 addition & 1 deletion lua/black-atom/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

---@alias BlackAtom.Highlights table<string, BlackAtom.HighlightDefinition>>

---@alias BlackAtom.HighlightMapExtension.map fun(colors: BlackAtom.Theme.Colors, config: BlackAtom.Config): BlackAtom.Highlights
---@alias BlackAtom.HighlightMapExtension.map fun(colors: BlackAtom.Theme.Colors, config. BlackAtom.Config): BlackAtom.Highlights

---@class BlackAtom.HighlightsSpec
---@field enabled? boolean Default: true
Expand Down
4 changes: 2 additions & 2 deletions lua/lualine/themes/black_atom.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ local API = require("black-atom.api")
local Config = require("black-atom.config")

local colors = API:get_colors()
local config = Config:get()
local config = config.get()

local cond_hl = require("black-atom.lib.highlights").conditional_hl
local cond_hl = require("black-atom.lib.highlights").cond_hl

local M = {}

Expand Down

0 comments on commit 4198ccf

Please sign in to comment.