From 8c5a4c0519b4c230e5abc93ef23a9b4fa4139b7b Mon Sep 17 00:00:00 2001 From: Cameron Date: Mon, 17 Jul 2023 19:56:51 +0200 Subject: [PATCH] Debug --- lua/neogit/lib/hl.lua | 483 ++++++++++++------------------------------ 1 file changed, 132 insertions(+), 351 deletions(-) diff --git a/lua/neogit/lib/hl.lua b/lua/neogit/lib/hl.lua index 7d3052a3e..6b5ce7ab9 100644 --- a/lua/neogit/lib/hl.lua +++ b/lua/neogit/lib/hl.lua @@ -18,150 +18,86 @@ local Color = require("neogit.lib.color").Color local hl_store local M = {} ----@param group string Syntax group name. ----@param opt HiSpec -function M.hi(group, opt) - vim.cmd( - string.format( - "hi %s %s guifg=%s guibg=%s gui=%s guisp=%s blend=%s", - opt.default and "default" or "", - group, - opt.fg or "NONE", - opt.bg or "NONE", - opt.gui or "NONE", - opt.sp or "NONE", - opt.blend or "NONE" - ) - ) -end - ----@param name string Syntax group name. ----@return table|nil -function M.make_hl_link_attrs(name) - local fg = M.get_fg(name, true) - local bg = M.get_bg(name, true) - local gui = M.get_gui(name, true) - - if fg or bg or gui then - return { fg = fg, bg = bg, gui = gui } +local function to_hex(dec) + local hex = string.format("%x", dec) + if #hex < 6 then + return string.rep("0", 6 - #hex) .. hex else - return + return hex end end ---@param name string Syntax group name. ----@param attr string Attribute name. ----@param trans boolean Translate the syntax group (follows links). -function M.get_hl_attr(name, attr, trans) - local id = vim.fn.hlID(name) - if id == 0 then - return - end - if id and trans then - id = vim.fn.synIDtrans(id) +local function get_fg(name) + local color = vim.api.nvim_get_hl(0, { name = name })["fg"] + if color then + return "#" .. to_hex(color) end - if not id then - return - end - - local value = vim.fn.synIDattr(id, attr) - if not value or value == "" then - return - end - - return value end ----@param group_name string Syntax group name. ----@param trans boolean|nil Translate the syntax group (follows links). True by default. -function M.get_fg(group_name, trans) - if type(trans) ~= "boolean" then - trans = true - end - return M.get_hl_attr(group_name, "fg", trans) -end - ----@param group_name string Syntax group name. ----@param trans boolean|nil Translate the syntax group (follows links). True by default. -function M.get_bg(group_name, trans) - if type(trans) ~= "boolean" then - trans = true +---@param name string Syntax group name. +local function get_bg(name) + local color = vim.api.nvim_get_hl(0, { name = name })["bg"] + if color then + return "#" .. to_hex(color) end - return M.get_hl_attr(group_name, "bg", trans) end ----@param group_name string Syntax group name. ----@param trans boolean|nil Translate the syntax group (follows links). True by default. -function M.get_gui(group_name, trans) - if type(trans) ~= "boolean" then - trans = true - end - local hls = {} - local attributes = { - "bold", - "italic", - "reverse", - "standout", - "underline", - "undercurl", - "strikethrough", - } - - for _, attr in ipairs(attributes) do - if M.get_hl_attr(group_name, attr, trans) == "1" then - table.insert(hls, attr) - end - end - - if #hls > 0 then - return table.concat(hls, ",") - end -end -function M.make_palette() - local bg = vim.o.bg - local hl_bg_normal = M.get_bg("Normal") or (bg == "dark" and "#22252A" or "#eeeeee") - local bg_normal = Color.from_hex(hl_bg_normal) +-- stylua: ignore start +local function make_palette() + local bg = Color.from_hex(get_bg("Normal") or (vim.o.bg == "dark" and "#22252A" or "#eeeeee")) + local red = Color.from_hex(get_fg("Error") or "#E06C75") + local orange = Color.from_hex(get_fg("SpecialChar") or "#ffcb6b") + local yellow = Color.from_hex(get_fg("PreProc") or "#FFE082") + local green = Color.from_hex(get_fg("String") or "#C3E88D") + local cyan = Color.from_hex(get_fg("Operator") or "#89ddff") + local blue = Color.from_hex(get_fg("Macro") or "#82AAFF") + local purple = Color.from_hex(get_fg("Include") or "#C792EA") return { - bg0 = M.get_bg("Normal") or "#22252A", - bg1 = bg_normal:shade(0.019):to_css(), - bg2 = bg_normal:shade(0.065):to_css(), - bg3 = bg_normal:shade(0.11):to_css(), - - grey = bg_normal:shade(0.4):to_css(), - - red = M.get_fg("Error") or "#E06C75", - bg_red = Color.from_hex(M.get_fg("Error") or "#E06C75"):shade(-0.18):to_css(), - line_red = M.get_bg("DiffDelete") - or Color.from_hex(M.get_fg("Error") or "#E06C75"):shade(-0.6):set_saturation(0.4):to_css(), - - orange = M.get_fg("SpecialChar") or "#ffcb6b", - bg_orange = Color.from_hex(M.get_fg("SpecialChar") or "#ffcb6b"):shade(-0.17):to_css(), - - yellow = M.get_fg("PreProc") or "#FFE082", - bg_yellow = Color.from_hex(M.get_fg("PreProc") or "#FFE082"):shade(-0.17):to_css(), - - green = M.get_fg("String") or "#C3E88D", - bg_green = Color.from_hex(M.get_fg("String") or "#C3E88D"):shade(-0.18):to_css(), - line_green = M.get_bg("DiffAdd") - or Color.from_hex(M.get_fg("String") or "#C3E88D"):shade(-0.72):set_saturation(0.2):to_css(), - - cyan = M.get_fg("Operator") or "#89ddff", - bg_cyan = Color.from_hex(M.get_fg("Operator") or "#89ddff"):shade(-0.18):to_css(), - - blue = M.get_fg("Macro") or "#82AAFF", - bg_blue = Color.from_hex(M.get_fg("Macro") or "#82AAFF"):shade(-0.18):to_css(), - - purple = M.get_fg("Include") or "#C792EA", - bg_purple = Color.from_hex(M.get_fg("Include") or "#C792EA"):shade(-0.18):to_css(), - md_purple = "#c3a7e5", + bg0 = bg:to_css(), + bg1 = bg:shade(0.019):to_css(), + bg2 = bg:shade(0.065):to_css(), + bg3 = bg:shade(0.11):to_css(), + grey = bg:shade(0.4):to_css(), + red = red:to_css(), + bg_red = red:shade(-0.18):to_css(), + line_red = get_bg("DiffDelete") or red:shade(-0.6):set_saturation(0.4):to_css(), + orange = orange:to_css(), + bg_orange = orange:shade(-0.17):to_css(), + yellow = yellow:to_css(), + bg_yellow = yellow:shade(-0.17):to_css(), + green = green:to_css(), + bg_green = green:shade(-0.18):to_css(), + line_green = get_bg("DiffAdd") or green:shade(-0.72):set_saturation(0.2):to_css(), + cyan = cyan:to_css(), + bg_cyan = cyan:shade(-0.18):to_css(), + blue = blue:to_css(), + bg_blue = blue:shade(-0.18):to_css(), + purple = purple:to_css(), + bg_purple = purple:shade(-0.18):to_css(), + md_purple = purple:shade(0.18):to_css(), } end +-- stylua: ignore end + +-- https://github.com/lewis6991/gitsigns.nvim/blob/1e01b2958aebb79f1c33e7427a1bac131a678e0d/lua/gitsigns/highlight.lua#L250 +--- @param hl_name string +--- @return boolean +local function is_set(hl_name) + local exists, hl = pcall(vim.api.nvim_get_hl, 0, { name = hl_name }) + if not exists then + return false + end + + return not vim.tbl_isempty(hl) +end function M.setup() - local palette = M.make_palette() + local palette = make_palette() + -- stylua: ignore start hl_store = { NeogitGraphRed = { fg = palette.red }, NeogitGraphWhite = { fg = palette.white }, @@ -172,235 +108,80 @@ function M.setup() NeogitGraphBlue = { fg = palette.blue }, NeogitGraphPurple = { fg = palette.purple }, NeogitGraphGray = { fg = palette.grey }, - - NeogitGraphBoldRed = { fg = palette.red, gui = "bold" }, - NeogitGraphBoldWhite = { fg = palette.white, gui = "bold" }, - NeogitGraphBoldOrange = { fg = palette.orange, gui = "bold" }, - NeogitGraphBoldYellow = { fg = palette.yellow, gui = "bold" }, - NeogitGraphBoldGreen = { fg = palette.green, gui = "bold" }, - NeogitGraphBoldCyan = { fg = palette.cyan, gui = "bold" }, - NeogitGraphBoldBlue = { fg = palette.blue, gui = "bold" }, - NeogitGraphBoldPurple = { fg = palette.purple, gui = "bold" }, - NeogitGraphBoldGray = { fg = palette.grey, gui = "bold" }, - - NeogitHunkHeader = { - fg = palette.bg0, - bg = palette.grey, - gui = "bold", - }, - NeogitHunkHeaderHighlight = { - fg = palette.bg0, - bg = palette.md_purple, - gui = "bold", - }, - NeogitDiffContext = { - bg = palette.bg1, - }, - NeogitDiffContextHighlight = { - bg = palette.bg2, - }, - NeogitDiffAdd = { - bg = palette.line_green, - fg = palette.bg_green, - }, - NeogitDiffAddHighlight = { - bg = palette.line_green, - fg = palette.green, - }, - NeogitDiffDelete = { - bg = palette.line_red, - fg = palette.bg_red, - }, - NeogitDiffDeleteHighlight = { - bg = palette.line_red, - fg = palette.red, - }, - NeogitPopupSectionTitle = { - link = "Function", - }, - NeogitPopupBranchName = { - link = "String", - }, - NeogitPopupBold = { - gui = "bold", - }, - NeogitPopupSwitchKey = { - fg = palette.purple, - }, - NeogitPopupSwitchEnabled = { - link = "SpecialChar", - }, - NeogitPopupSwitchDisabled = { - link = "Comment", - }, - NeogitPopupOptionKey = { - fg = palette.purple, - }, - NeogitPopupOptionEnabled = { - link = "SpecialChar", - }, - NeogitPopupOptionDisabled = { - link = "Comment", - }, - NeogitPopupConfigKey = { - fg = palette.purple, - }, - NeogitPopupConfigEnabled = { - link = "SpecialChar", - }, - NeogitPopupConfigDisabled = { - link = "Comment", - }, - NeogitPopupActionKey = { - fg = palette.purple, - }, - NeogitPopupActionDisabled = { - link = "Comment", - }, - NeogitFilePath = { - fg = palette.blue, - gui = "italic", - }, - NeogitCommitViewHeader = { - bg = palette.bg_cyan, - fg = palette.bg0, - }, - NeogitDiffHeader = { - bg = palette.bg3, - fg = palette.blue, - gui = "bold", - }, - NeogitDiffHeaderHighlight = { - bg = palette.bg3, - fg = palette.orange, - gui = "bold", - }, - NeogitNotificationInfo = { - link = "DiagnosticInfo", - }, - NeogitNotificationWarning = { - link = "DiagnosticWarn", - }, - NeogitNotificationError = { - link = "DiagnosticError", - }, - NeogitCommandText = { - link = "Comment", - }, - NeogitCommandTime = { - link = "Comment", - }, - NeogitCommandCodeNormal = { - link = "String", - }, - NeogitCommandCodeError = { - link = "Error", - }, - NeogitBranch = { - fg = palette.orange, - gui = "bold", - }, - NeogitRemote = { - fg = palette.green, - gui = "bold", - }, - NeogitUnmergedInto = { - link = "Function", - }, - NeogitUnpulledFrom = { - link = "Function", - }, - NeogitObjectId = { - link = "Comment", - }, - NeogitStash = { - link = "Comment", - }, - NeogitRebaseDone = { - link = "Comment", - }, - NeogitCursorLine = { - bg = palette.bg1, - }, - NeogitFold = { - fg = "None", - bg = "None", - }, - NeogitChangeModified = { - fg = palette.bg_blue, - gui = "italic,bold", - }, - NeogitChangeAdded = { - fg = palette.bg_green, - gui = "italic,bold", - }, - NeogitChangeDeleted = { - fg = palette.bg_red, - gui = "italic,bold", - }, - NeogitChangeRenamed = { - fg = palette.bg_purple, - gui = "italic,bold", - }, - NeogitChangeUpdated = { - fg = palette.bg_orange, - gui = "italic,bold", - }, - NeogitChangeCopied = { - fg = palette.bg_cyan, - gui = "italic,bold", - }, - NeogitChangeBothModified = { - fg = palette.bg_yellow, - gui = "italic,bold", - }, - NeogitChangeNewFile = { - fg = palette.bg_green, - gui = "italic,bold", - }, - NeogitUntrackedfiles = { - fg = palette.bg_purple, - gui = "bold", - }, - NeogitUnstagedchanges = { - fg = palette.bg_purple, - gui = "bold", - }, - NeogitUnmergedchanges = { - fg = palette.bg_purple, - gui = "bold", - }, - NeogitUnpulledchanges = { - fg = palette.bg_purple, - gui = "bold", - }, - NeogitRecentcommits = { - fg = palette.bg_purple, - gui = "bold", - }, - NeogitStagedchanges = { - fg = palette.bg_purple, - gui = "bold", - }, - NeogitStashes = { - fg = palette.bg_purple, - gui = "bold", - }, - NeogitRebasing = { - fg = palette.bg_purple, - gui = "bold", - }, + NeogitGraphBoldRed = { fg = palette.red, bold = true }, + NeogitGraphBoldWhite = { fg = palette.white, bold = true }, + NeogitGraphBoldOrange = { fg = palette.orange, bold = true }, + NeogitGraphBoldYellow = { fg = palette.yellow, bold = true }, + NeogitGraphBoldGreen = { fg = palette.green, bold = true }, + NeogitGraphBoldCyan = { fg = palette.cyan, bold = true }, + NeogitGraphBoldBlue = { fg = palette.blue, bold = true }, + NeogitGraphBoldPurple = { fg = palette.purple, bold = true }, + NeogitGraphBoldGray = { fg = palette.grey, bold = true }, + NeogitHunkHeader = { fg = palette.bg0, bg = palette.grey, bold = true }, + NeogitHunkHeaderHighlight = { fg = palette.bg0, bg = palette.md_purple, bold = true }, + NeogitDiffContext = { bg = palette.bg1 }, + NeogitDiffContextHighlight = { bg = palette.bg2 }, + NeogitDiffAdd = { bg = palette.line_green, fg = palette.bg_green }, + NeogitDiffAddHighlight = { bg = palette.line_green, fg = palette.green }, + NeogitDiffDelete = { bg = palette.line_red, fg = palette.bg_red }, + NeogitDiffDeleteHighlight = { bg = palette.line_red, fg = palette.red }, + NeogitPopupSectionTitle = { link = "Function" }, + NeogitPopupBranchName = { link = "String" }, + NeogitPopupBold = { bold = true }, + NeogitPopupSwitchKey = { fg = palette.purple }, + NeogitPopupSwitchEnabled = { link = "SpecialChar" }, + NeogitPopupSwitchDisabled = { link = "Comment" }, + NeogitPopupOptionKey = { fg = palette.purple }, + NeogitPopupOptionEnabled = { link = "SpecialChar" }, + NeogitPopupOptionDisabled = { link = "Comment" }, + NeogitPopupConfigKey = { fg = palette.purple }, + NeogitPopupConfigEnabled = { link = "SpecialChar" }, + NeogitPopupConfigDisabled = { link = "Comment" }, + NeogitPopupActionKey = { fg = palette.purple }, + NeogitPopupActionDisabled = { link = "Comment" }, + NeogitFilePath = { fg = palette.blue, italic = true }, + NeogitCommitViewHeader = { bg = palette.bg_cyan, fg = palette.bg0 }, + NeogitDiffHeader = { bg = palette.bg3, fg = palette.blue, bold = true }, + NeogitDiffHeaderHighlight = { bg = palette.bg3, fg = palette.orange, bold = true }, + NeogitNotificationInfo = { link = "DiagnosticInfo" }, + NeogitNotificationWarning = { link = "DiagnosticWarn" }, + NeogitNotificationError = { link = "DiagnosticError" }, + NeogitCommandText = { link = "Comment" }, + NeogitCommandTime = { link = "Comment" }, + NeogitCommandCodeNormal = { link = "String" }, + NeogitCommandCodeError = { link = "Error" }, + NeogitBranch = { fg = palette.orange, bold = true }, + NeogitRemote = { fg = palette.green, bold = true }, + NeogitUnmergedInto = { link = "Function" }, + NeogitUnpulledFrom = { link = "Function" }, + NeogitObjectId = { link = "Comment" }, + NeogitStash = { link = "Comment" }, + NeogitRebaseDone = { link = "Comment" }, + NeogitCursorLine = { bg = palette.bg1 }, + NeogitFold = { fg = "None", bg = "None" }, + NeogitChangeModified = { fg = palette.bg_blue, bold = true, italic = true }, + NeogitChangeAdded = { fg = palette.bg_green, bold = true, italic = true }, + NeogitChangeDeleted = { fg = palette.bg_red, bold = true, italic = true }, + NeogitChangeRenamed = { fg = palette.bg_purple, bold = true, italic = true }, + NeogitChangeUpdated = { fg = palette.bg_orange, bold = true, italic = true }, + NeogitChangeCopied = { fg = palette.bg_cyan, bold = true, italic = true }, + NeogitChangeBothModified = { fg = palette.bg_yellow, bold = true, italic = true }, + NeogitChangeNewFile = { fg = palette.bg_green, bold = true, italic = true }, + NeogitUntrackedfiles = { fg = palette.bg_purple, bold = true }, + NeogitUnstagedchanges = { fg = palette.bg_purple, bold = true }, + NeogitUnmergedchanges = { fg = palette.bg_purple, bold = true }, + NeogitUnpulledchanges = { fg = palette.bg_purple, bold = true }, + NeogitRecentcommits = { fg = palette.bg_purple, bold = true }, + NeogitStagedchanges = { fg = palette.bg_purple, bold = true }, + NeogitStashes = { fg = palette.bg_purple, bold = true }, + NeogitRebasing = { fg = palette.bg_purple, bold = true }, } + -- stylua: ignore end for group, hl in pairs(hl_store) do - if vim.fn.hlID(group) == 0 then - if hl.link then - local attrs = M.make_hl_link_attrs(hl.link) or {} - attrs.default = true - M.hi(group, vim.tbl_extend("keep", hl, attrs)) - else - M.hi(group, hl) - end + if not is_set(group) then + hl.default = true + vim.api.nvim_set_hl(0, group, hl) end end end