Skip to content

Commit

Permalink
Merge branch 'main' into release-please--branches--main
Browse files Browse the repository at this point in the history
  • Loading branch information
shortcuts authored Aug 29, 2024
2 parents 1342c19 + fa82d17 commit de23f9a
Show file tree
Hide file tree
Showing 14 changed files with 418 additions and 410 deletions.
83 changes: 42 additions & 41 deletions lua/no-neck-pain/colors.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ local C = {}
---@return number?: the g color
---@return number?: the b color
---@private
local function hexToRGB(hex)
local function hex_to_rgb(hex)
local r, g, b = hex:sub(2, 3), hex:sub(4, 5), hex:sub(6, 7)

return tonumber("0x" .. r), tonumber("0x" .. g), tonumber("0x" .. b)
end

---Blend the given `colorCode` RGB for the given `factor`.
---Blend the given `color_code` RGB for the given `factor`.
---
---@param colorCode string: the color code string, e.g. #ffffff.
---@param color_code string: the color code string, e.g. #ffffff.
---@param factor number: Brighten (positive) or darken (negative) the side buffers background color. Accepted values are [-1..1].
---@return string: the blended color code.
---@private
local function blend(colorCode, factor)
local r, g, b = hexToRGB(colorCode)
local function blend(color_code, factor)
local r, g, b = hex_to_rgb(color_code)
local format = "#%02x%02x%02x"

if factor < 0 then
Expand All @@ -44,15 +44,15 @@ local function blend(colorCode, factor)
)
end

---Tries to match the given `colorCode` to an integration name, defaults to the given `colorCode` if not found.
---Tries to match the given `color_code` to an integration name, defaults to the given `color_code` if not found.
---if a `factor` is provided, the color will be blended (brighten/darken) before being returned.
---
---@param colorCode string: the color code string, e.g. #ffffff.
---@param color_code string: the color code string, e.g. #ffffff.
---@param factor number: Brighten (positive) or darken (negative) the side buffers background color. Accepted values are [-1..1].
---@return string?: the blended color code.
---@private
function C.matchAndBlend(colorCode, factor)
if colorCode == nil or string.lower(colorCode) == "none" then
function C.match_and_blend(color_code, factor)
if color_code == nil or string.lower(color_code) == "none" then
return nil
end

Expand All @@ -66,23 +66,23 @@ function C.matchAndBlend(colorCode, factor)
)
end

if Co.THEMES[colorCode] ~= nil then
colorCode = Co.THEMES[colorCode]
if Co.THEMES[color_code] ~= nil then
color_code = Co.THEMES[color_code]
end

local hexPattern = "^#" .. "[abcdef0-9]" .. ("[abcdef0-9]"):rep(5) .. "$"
colorCode = string.lower(colorCode)
local hex_pattern = "^#" .. "[abcdef0-9]" .. ("[abcdef0-9]"):rep(5) .. "$"
color_code = string.lower(color_code)

assert(
colorCode:match(hexPattern) ~= nil,
string.format("`colorCode` %s does not match the regex %s", colorCode, hexPattern)
color_code:match(hex_pattern) ~= nil,
string.format("`color_code` %s does not match the regex %s", color_code, hex_pattern)
)

if factor == 0 then
return colorCode
return color_code
end

return blend(colorCode, factor or 0)
return blend(color_code, factor or 0)
end

---Parses to color for each buffer parameters, considering transparent backgrounds.
Expand All @@ -91,11 +91,11 @@ end
---@return table: the parsed buffers.
---@private
function C.parse(buffers)
buffers.colors.background = C.matchAndBlend(buffers.colors.background, buffers.colors.blend)
buffers.colors.background = C.match_and_blend(buffers.colors.background, buffers.colors.blend)

for _, side in pairs(Co.SIDES) do
if buffers[side].enabled then
buffers[side].colors.background = C.matchAndBlend(
buffers[side].colors.background = C.match_and_blend(
buffers[side].colors.background,
buffers[side].colors.blend or buffers.colors.blend
) or buffers.colors.background
Expand Down Expand Up @@ -129,61 +129,62 @@ function C.init(win, side)
end

-- init namespace for the current tab
local id, _ = S.setNamespace(S, side)
local id, _ = S.set_namespace(S, side)
local bufnr = vim.api.nvim_win_get_buf(win)

-- create groups to assign them to the namespace
local backgroundGroup = string.format("NoNeckPain_background_tab_%s_side_%s", S.activeTab, side)
local textGroup = string.format("NoNeckPain_text_tab_%s_side_%s", S.activeTab, side)
local background_group =
string.format("NoNeckPain_background_tab_%s_side_%s", S.active_tab, side)
local text_group = string.format("NoNeckPain_text_tab_%s_side_%s", S.active_tab, side)

vim.cmd(
string.format(
"hi! %s guifg=%s guibg=%s",
backgroundGroup,
background_group,
_G.NoNeckPain.config.buffers[side].colors.background,
_G.NoNeckPain.config.buffers[side].colors.background
)
)
vim.cmd(
string.format(
"hi! %s guifg=%s guibg=%s",
textGroup,
text_group,
_G.NoNeckPain.config.buffers[side].colors.text,
_G.NoNeckPain.config.buffers[side].colors.background
)
)

-- assign groups to the namespace
vim.api.nvim_buf_add_highlight(bufnr, id, backgroundGroup, 0, 0, -1)
vim.api.nvim_buf_add_highlight(bufnr, id, textGroup, 0, 0, -1)
vim.api.nvim_buf_add_highlight(bufnr, id, background_group, 0, 0, -1)
vim.api.nvim_buf_add_highlight(bufnr, id, text_group, 0, 0, -1)

-- link nnp and neovim hl groups
local groups = { Normal = textGroup, NormalNC = textGroup }
local groups = { Normal = text_group, NormalNC = text_group }

-- we only set those for non transparent backgrouns to prevent white lines.
if _G.NoNeckPain.config.buffers[side].colors.background ~= "NONE" then
groups = vim.tbl_extend("keep", groups, {
WinSeparator = backgroundGroup,
VertSplit = backgroundGroup,
EndOfBuffer = backgroundGroup,
CursorColumn = backgroundGroup,
CursorLineNr = backgroundGroup,
NonText = backgroundGroup,
SignColumn = backgroundGroup,
Cursor = backgroundGroup,
LineNr = backgroundGroup,
StatusLine = backgroundGroup,
StatusLineNC = backgroundGroup,
WinSeparator = background_group,
VertSplit = background_group,
EndOfBuffer = background_group,
CursorColumn = background_group,
CursorLineNr = background_group,
NonText = background_group,
SignColumn = background_group,
Cursor = background_group,
LineNr = background_group,
StatusLine = background_group,
StatusLineNC = background_group,
})
end

local stringGroups = {}
local string_groups = {}

for hl, group in pairs(groups) do
table.insert(stringGroups, string.format("%s:%s", hl, group))
table.insert(string_groups, string.format("%s:%s", hl, group))
end

A.setWindowOption(win, "winhl", table.concat(stringGroups, ","))
A.set_window_option(win, "winhl", table.concat(string_groups, ","))
end

return C
10 changes: 5 additions & 5 deletions lua/no-neck-pain/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ local defaults = vim.deepcopy(NoNeckPain.options)
---@param fileType string The file extension to leverage.
---
---@private
local function parseDeprecatedScratchPad(side, options, fileType)
local function parse_deprecated_scratchPad(side, options, fileType)
-- set the defaults if the user rely on them
if vim.tbl_count(options) == 0 or options.pathToFile == nil then
options = A.tde(options, defaults.buffers.scratchPad)
Expand Down Expand Up @@ -368,7 +368,7 @@ function NoNeckPain.defaults(options)
options.buffers[side].bo = A.tde(options.buffers[side].bo, options.buffers.bo)
options.buffers[side].wo = A.tde(options.buffers[side].wo, options.buffers.wo)
options.buffers[side].colors = A.tde(options.buffers[side].colors, options.buffers.colors)
options.buffers[side].scratchPad = parseDeprecatedScratchPad(
options.buffers[side].scratchPad = parse_deprecated_scratchPad(
side,
A.tde(options.buffers[side].scratchPad, options.buffers.scratchPad),
options.buffers[side].bo.filetype
Expand Down Expand Up @@ -421,7 +421,7 @@ end
---@param mappings table A key value map of the mapping name and its command.
---
---@private
local function registerMappings(options, mappings)
local function register_mappings(options, mappings)
-- all of the mappings are disabled
if not options.enabled then
return
Expand Down Expand Up @@ -461,9 +461,9 @@ function NoNeckPain.setup(options)

NoNeckPain.options.hasNvim9 = vim.fn.has("nvim-0.9") == 1

D.warnDeprecation(NoNeckPain.options)
D.warn_deprecation(NoNeckPain.options)

registerMappings(NoNeckPain.options.mappings, {
register_mappings(NoNeckPain.options.mappings, {
toggle = ":NoNeckPain<CR>",
toggleLeftSide = ":NoNeckPainToggleLeftSide<CR>",
toggleRightSide = ":NoNeckPainToggleRightSide<CR>",
Expand Down
20 changes: 10 additions & 10 deletions lua/no-neck-pain/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function NoNeckPain.toggle()
_G.NoNeckPain.config = C.options
end

A.debounce("publicAPI_toggle", M.toggle)
A.debounce("public_api_toggle", M.toggle)
end

--- Toggles the scratchPad feature of the plugin.
Expand All @@ -24,7 +24,7 @@ function NoNeckPain.toggleScratchPad()
_G.NoNeckPain.config = C.options
end

M.toggleScratchPad()
M.toggle_scratchPad()
end

--- Sets the config `width` to the given `width` value and resizes the NoNeckPain windows.
Expand All @@ -45,7 +45,7 @@ function NoNeckPain.resize(width)
_G.NoNeckPain.config = vim.tbl_deep_extend("keep", { width = width }, _G.NoNeckPain.config)
end

M.init("publicAPI_resize", false)
M.init("public_api_resize", false)
end

--- Toggles the config `${side}.enabled` and re-inits the plugin.
Expand All @@ -56,8 +56,8 @@ function NoNeckPain.toggleSide(side)
error("no-neck-pain.nvim must be enabled, run `NoNeckPain` first.")
end

A.debounce("publicAPI_toggleSide", function(scope)
M.toggleSide(scope, side)
A.debounce("public_api_toggle_side", function(scope)
M.toggle_side(scope, side)
end)
end

Expand All @@ -67,12 +67,12 @@ function NoNeckPain.enable()
_G.NoNeckPain.config = C.options
end

A.debounce("publicAPI_enable", M.enable, 10)
A.debounce("public_api_enable", M.enable, 10)
end

--- Disables the plugin, clear highlight groups and autocmds, closes side buffers and resets the internal state.
function NoNeckPain.disable()
A.debounce("publicAPI_disable", M.disable)
A.debounce("public_api_disable", M.disable)
end

-- setup NoNeckPain options and merge them with user provided ones.
Expand All @@ -91,14 +91,14 @@ function NoNeckPain.setup(opts)
if _G.NoNeckPain.config.autocmds.reloadOnColorSchemeChange then
vim.api.nvim_create_autocmd({ "ColorScheme" }, {
pattern = "*",
callback = function()
callback = function(p)
vim.schedule(function()
if _G.NoNeckPain.state == nil or not _G.NoNeckPain.state.enabled then
return
end

_G.NoNeckPain.config = C.defaults(opts)
M.init("ColorScheme")
M.init(p.event)
end)
end,
group = "NoNeckPainAutocmd",
Expand All @@ -117,7 +117,7 @@ function NoNeckPain.setup(opts)

NoNeckPain.enable()

A.debounce("enableOnVimEnter", function()
A.debounce("enable_on_vim_enter", function()
if _G.NoNeckPain.state ~= nil then
pcall(vim.api.nvim_del_augroup_by_name, "NoNeckPainVimEnterAutocmd")
end
Expand Down
Loading

0 comments on commit de23f9a

Please sign in to comment.