From 38c47157a933396f96c7813aa1723592bb5c492d Mon Sep 17 00:00:00 2001 From: nullchilly Date: Tue, 12 Jul 2022 13:15:50 +0700 Subject: [PATCH 1/4] fix(g.colors_name): Set colorscheme name --- lua/catppuccin/utils/util.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/lua/catppuccin/utils/util.lua b/lua/catppuccin/utils/util.lua index 51637db..38b3f3a 100644 --- a/lua/catppuccin/utils/util.lua +++ b/lua/catppuccin/utils/util.lua @@ -82,6 +82,7 @@ function util.load(theme) if vim.fn.exists("syntax_on") then vim.cmd("syntax reset") end + g.colors_name = "catppuccin" local custom_highlights = require("catppuccin.config").options.custom_highlights util.properties(theme.properties) From c1fb0012133c6bef03bd993b60e03f0944437076 Mon Sep 17 00:00:00 2001 From: nullchilly Date: Tue, 12 Jul 2022 14:00:32 +0700 Subject: [PATCH 2/4] refactor(performance): Improve nvim_set_hl --- lua/catppuccin/core/integrations/bufferline.lua | 2 +- lua/catppuccin/core/integrations/hop.lua | 4 ++-- lua/catppuccin/core/integrations/mini.lua | 6 +++--- lua/catppuccin/core/integrations/treesitter.lua | 2 +- lua/catppuccin/core/mapper.lua | 2 +- lua/catppuccin/utils/data.lua | 9 --------- lua/catppuccin/utils/util.lua | 14 +++++++++----- 7 files changed, 17 insertions(+), 22 deletions(-) diff --git a/lua/catppuccin/core/integrations/bufferline.lua b/lua/catppuccin/core/integrations/bufferline.lua index bf17205..c4f9128 100644 --- a/lua/catppuccin/core/integrations/bufferline.lua +++ b/lua/catppuccin/core/integrations/bufferline.lua @@ -14,7 +14,7 @@ function M.get(cp) BufferLineFill = { bg = bg_highlight }, BufferLineBackcrust = { fg = cp.text, bg = inactive_bg }, -- others BufferLineBufferVisible = { fg = cp.surface1, bg = inactive_bg }, - BufferLineBufferSelected = { fg = cp.text, bg = cp.base, style = "bold,italic" }, -- current + BufferLineBufferSelected = { fg = cp.text, bg = cp.base, style = { "bold", "italic" } }, -- current BufferLineTab = { fg = cp.surface1, bg = cp.base }, BufferLineTabSelected = { fg = cp.red, bg = cp.blue }, BufferLineTabClose = { fg = cp.red, bg = inactive_bg }, diff --git a/lua/catppuccin/core/integrations/hop.lua b/lua/catppuccin/core/integrations/hop.lua index 72cd3cf..f4ed799 100644 --- a/lua/catppuccin/core/integrations/hop.lua +++ b/lua/catppuccin/core/integrations/hop.lua @@ -2,9 +2,9 @@ local M = {} function M.get(cp) return { - HopNextKey = { bg = cp.base, fg = cp.peach, style = "bold,underline" }, + HopNextKey = { bg = cp.base, fg = cp.peach, style = { "bold", "underline" } }, HopNextKey1 = { bg = cp.base, fg = cp.blue, style = "bold" }, - HopNextKey2 = { bg = cp.base, fg = cp.teal, style = "bold,italic" }, + HopNextKey2 = { bg = cp.base, fg = cp.teal, style = { "bold", "italic" } }, HopUnmatched = { bg = cp.base, fg = cp.overlay0 }, } end diff --git a/lua/catppuccin/core/integrations/mini.lua b/lua/catppuccin/core/integrations/mini.lua index ad2c2c9..4280394 100644 --- a/lua/catppuccin/core/integrations/mini.lua +++ b/lua/catppuccin/core/integrations/mini.lua @@ -17,7 +17,7 @@ function M.get(cp) MiniJump = { fg = cp.overlay2, bg = cp.pink }, - MiniJump2dSpot = { bg = cp.base, fg = cp.peach, style = "bold,underline" }, + MiniJump2dSpot = { bg = cp.base, fg = cp.peach, style = { "bold", "underline" } }, MiniStarterCurrent = {}, MiniStarterFooter = { fg = cp.yellow, style = "italic" }, @@ -42,10 +42,10 @@ function M.get(cp) MiniSurround = { bg = cp.pink, fg = cp.surface1 }, - MiniTablineCurrent = { fg = cp.text, bg = cp.base, style = "bold,italic" }, + MiniTablineCurrent = { fg = cp.text, bg = cp.base, style = { "bold", "italic" } }, MiniTablineFill = { bg = bg_highlight }, MiniTablineHidden = { fg = cp.text, bg = inactive_bg }, - MiniTablineModifiedCurrent = { fg = cp.base, bg = cp.text, style = "bold,italic" }, + MiniTablineModifiedCurrent = { fg = cp.base, bg = cp.text, style = { "bold", "italic" } }, MiniTablineModifiedHidden = { fg = inactive_bg, bg = cp.text }, MiniTablineModifiedVisible = { fg = cp.surface1, bg = cp.subtext1 }, MiniTablineTabpagesection = { fg = cp.surface1, bg = cp.base }, diff --git a/lua/catppuccin/core/integrations/treesitter.lua b/lua/catppuccin/core/integrations/treesitter.lua index 2ed684c..3340774 100644 --- a/lua/catppuccin/core/integrations/treesitter.lua +++ b/lua/catppuccin/core/integrations/treesitter.lua @@ -80,7 +80,7 @@ function M.get(cp) -- TSURI = { }; -- Any URI like a link or email. -- -- Markdown tresitter parser support - TSURI = { fg = cp.rosewater, style = "italic,underline" }, -- urls, links and emails + TSURI = { fg = cp.rosewater, style = { "italic", "underline" } }, -- urls, links and emails TSLiteral = { fg = cp.teal, style = "italic" }, -- used for inline code in markdown and for doc in python (""") TSTextReference = { fg = cp.lavender, style = "bold" }, -- references TSTitle = { fg = cp.blue, style = "bold" }, -- titles like: # Example diff --git a/lua/catppuccin/core/mapper.lua b/lua/catppuccin/core/mapper.lua index 2f1f29c..3359662 100644 --- a/lua/catppuccin/core/mapper.lua +++ b/lua/catppuccin/core/mapper.lua @@ -34,7 +34,7 @@ local function get_base() }, -- Screen-line at the cursor, when 'cursorline' is secp. Low-priority if forecrust (ctermfg OR guifg) is not secp. Directory = { fg = cp.blue }, -- directory names (and other special names in listings) EndOfBuffer = { fg = cp.base }, -- filler lines (~) after the end of the buffer. By default, this is highlighted like |hl-NonText|. - ErrorMsg = { fg = cp.red, style = "bold,italic" }, -- error messages on the command line + ErrorMsg = { fg = cp.red, style = { "bold", "italic" } }, -- error messages on the command line VertSplit = { fg = cp.crust }, -- the column separating vertically split windows Folded = { fg = cp.blue, bg = cp.surface1 }, -- line used for closed folds FoldColumn = { bg = cp.base, fg = cp.overlay0 }, -- 'foldcolumn' diff --git a/lua/catppuccin/utils/data.lua b/lua/catppuccin/utils/data.lua index 3be43a8..241f04f 100644 --- a/lua/catppuccin/utils/data.lua +++ b/lua/catppuccin/utils/data.lua @@ -8,13 +8,4 @@ function M.set_of(list) return set end --- https://www.codegrepper.com/code-examples/lua/lua+split+string+on+comma -function M.split(s, delimiter) - local result = {} - for match in (s..delimiter):gmatch("(.-)"..delimiter) do - table.insert(result, match) - end - return result -end - return M diff --git a/lua/catppuccin/utils/util.lua b/lua/catppuccin/utils/util.lua index 51637db..ca75756 100644 --- a/lua/catppuccin/utils/util.lua +++ b/lua/catppuccin/utils/util.lua @@ -1,9 +1,6 @@ -local udata = require("catppuccin.utils.data") - local g = vim.g local util = {} - local has_nvim07 = vim.fn.has("nvim-0.7") function util.highlight(group, color) @@ -15,8 +12,12 @@ function util.highlight(group, color) else if color.style then if color.style ~= "NONE" then - for _, style in pairs(udata.split(color.style, ",")) do - color[style] = true + if type(color.style) == "table" then + for _, style in ipairs(color.style) do + color[style] = true + end + else + color[color.style] = true end end end @@ -26,6 +27,9 @@ function util.highlight(group, color) end else -- Doc: :h highlight-gui + if color.style and type(color.style) == "table" then + color.style = table.concat(color.style, ",") + end local style = color.style and "gui=" .. color.style or "gui=NONE" local fg = color.fg and "guifg=" .. color.fg or "guifg=NONE" local bg = color.bg and "guibg=" .. color.bg or "guibg=NONE" From 2a235b712bab33b1ee2463584577aeae1f0d5eca Mon Sep 17 00:00:00 2001 From: nullchilly Date: Wed, 13 Jul 2022 18:03:32 +0700 Subject: [PATCH 3/4] feat(compile): Add compile support --- lua/catppuccin/config.lua | 5 +++ lua/catppuccin/main.lua | 24 ++++++++---- lua/catppuccin/utils/util.lua | 69 +++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 7 deletions(-) diff --git a/lua/catppuccin/config.lua b/lua/catppuccin/config.lua index b29ae42..b37344c 100644 --- a/lua/catppuccin/config.lua +++ b/lua/catppuccin/config.lua @@ -4,6 +4,11 @@ local config = {} config.options = { transparent_background = false, term_colors = false, + compile = { + enable = false, + path = vim.fn.stdpath "cache" .. "/catppuccin", + suffix = "_compiled" + }, dim_inactive = { enable = false, shade = "dark", diff --git a/lua/catppuccin/main.lua b/lua/catppuccin/main.lua index 0f96ba7..2e2046e 100644 --- a/lua/catppuccin/main.lua +++ b/lua/catppuccin/main.lua @@ -1,6 +1,5 @@ local M = {} -local utils = require("catppuccin.utils.util") local flavours = {"latte", "frappe", "macchiato", "mocha"} function M.cli_flavour_completion() @@ -14,12 +13,23 @@ local function load() catppuccin.before_loading() end - -- colorscheme gets evaluated from mapper.lua - local theme = require("catppuccin.core.mapper").apply() - utils.load(theme) - - if catppuccin.after_loading ~= nil then - catppuccin.after_loading() + local config = require("catppuccin.config").options + if config.compile.enable == true then + local compiled_path = config.compile.path .. (vim.loop.os_uname().sysname == 'Windows' and "\\" or "/") .. vim.g.catppuccin_flavour .. config.compile.suffix .. ".lua" + local f = io.open(compiled_path, "r") + if f ~= nil then + io.close(f) + vim.cmd("luafile " .. compiled_path) + end + else + -- colorscheme gets evaluated from mapper.lua + local theme = require("catppuccin.core.mapper").apply() + local utils = require("catppuccin.utils.util") + utils.load(theme) + + if catppuccin.after_loading ~= nil then + catppuccin.after_loading() + end end vim.api.nvim_exec_autocmds("User", { pattern = "CatppuccinLoaded" }) diff --git a/lua/catppuccin/utils/util.lua b/lua/catppuccin/utils/util.lua index be3e05b..1cbb050 100644 --- a/lua/catppuccin/utils/util.lua +++ b/lua/catppuccin/utils/util.lua @@ -99,4 +99,73 @@ function util.load(theme) end end +-- Credit: https://github.com/EdenEast/nightfox.nvim +local fmt = string.format +local function inspect(t) + local list = {} + for k, v in pairs(t) do + local q = type(v) == "string" and [["]] or "" + table.insert(list, fmt([[%s = %s%s%s]], k, q, v, q)) + end + + table.sort(list) + return fmt([[{ %s }]], table.concat(list, ", ")) +end + +function util.compile(theme) + if vim.fn.has "nvim-0.7" == 0 then + print "Compiling requires neovim 0.7" + end + local lines = { [[ +-- This file is autogenerated by CATPPUCCIN. +-- Do not make changes directly to this file. + +vim.cmd("hi clear") +if vim.fn.exists("syntax_on") then + vim.cmd("syntax reset") +end +vim.g.colors_name = "catppuccin"]] } + local config = require("catppuccin.config").options + local custom_highlights = config.custom_highlights + for property, value in pairs(theme.properties) do + table.insert(lines, fmt("vim.o.%s = %s", property, value)) + end + local tbl = vim.tbl_deep_extend("keep", theme.integrations, theme.base) + tbl = vim.tbl_deep_extend("keep", custom_highlights, tbl) + + for group, color in pairs(tbl) do + if color.link then + table.insert(lines, fmt([[vim.api.nvim_set_hl(0, "%s", { link = "%s" })]], group, color.link)) + else + if color.style then + if color.style ~= "NONE" then + if type(color.style) == "table" then + for _, style in ipairs(color.style) do + color[style] = true + end + else + color[color.style] = true + end + end + end + + color.style = nil + vim.api.nvim_set_hl(0, group, color) + table.insert(lines, fmt([[vim.api.nvim_set_hl(0, "%s", %s)]], group, inspect(color))) + end + end + + if config.term_colors then + local colors = { "overlay0", "red", "green", "yellow", "blue", "pink", "sky", "text", "overlay1", "red", "green", "yellow", "blue", "pink", "sky", "text"} + for i = 0, 15 do + table.insert(lines, fmt('vim.g.terminal_color_%d = "%s"', i, theme.terminal[colors[i + 1]])) + end + end + os.execute(string.format("mkdir %s %s", vim.loop.os_uname().sysname == 'Windows' and "" or "-p", config.compile.path)) + print(string.format("mkdir %s %s", vim.loop.os_uname().sysname == 'Windows' and "" or "-p", config.compile.path)) + local file = io.open(config.compile.path .. (vim.loop.os_uname().sysname == 'Windows' and "\\" or "/") .. vim.g.catppuccin_flavour .. config.compile.suffix .. ".lua", "w") + file:write(table.concat(lines, "\n")) + file:close() +end + return util From 03535cfc7cf06291d26e5b5eb9a8122e8d709d81 Mon Sep 17 00:00:00 2001 From: nullchilly Date: Wed, 13 Jul 2022 18:49:13 +0700 Subject: [PATCH 4/4] feat(compile): Add compile commands --- lua/catppuccin/command.lua | 0 lua/catppuccin/main.lua | 17 +++++++++-------- lua/catppuccin/utils/util.lua | 10 ++++++++-- plugin/catppuccin.vim | 2 ++ 4 files changed, 19 insertions(+), 10 deletions(-) create mode 100644 lua/catppuccin/command.lua diff --git a/lua/catppuccin/command.lua b/lua/catppuccin/command.lua new file mode 100644 index 0000000..e69de29 diff --git a/lua/catppuccin/main.lua b/lua/catppuccin/main.lua index 2e2046e..bcac01a 100644 --- a/lua/catppuccin/main.lua +++ b/lua/catppuccin/main.lua @@ -20,16 +20,17 @@ local function load() if f ~= nil then io.close(f) vim.cmd("luafile " .. compiled_path) + vim.api.nvim_exec_autocmds("User", { pattern = "CatppuccinLoaded" }) + return end - else - -- colorscheme gets evaluated from mapper.lua - local theme = require("catppuccin.core.mapper").apply() - local utils = require("catppuccin.utils.util") - utils.load(theme) + end + -- colorscheme gets evaluated from mapper.lua + local theme = require("catppuccin.core.mapper").apply() + local utils = require("catppuccin.utils.util") + utils.load(theme) - if catppuccin.after_loading ~= nil then - catppuccin.after_loading() - end + if catppuccin.after_loading ~= nil then + catppuccin.after_loading() end vim.api.nvim_exec_autocmds("User", { pattern = "CatppuccinLoaded" }) diff --git a/lua/catppuccin/utils/util.lua b/lua/catppuccin/utils/util.lua index 1cbb050..52a41ee 100644 --- a/lua/catppuccin/utils/util.lua +++ b/lua/catppuccin/utils/util.lua @@ -112,10 +112,11 @@ local function inspect(t) return fmt([[{ %s }]], table.concat(list, ", ")) end -function util.compile(theme) +function util.compile() if vim.fn.has "nvim-0.7" == 0 then print "Compiling requires neovim 0.7" end + local theme = require("catppuccin.core.mapper").apply() local lines = { [[ -- This file is autogenerated by CATPPUCCIN. -- Do not make changes directly to this file. @@ -162,10 +163,15 @@ vim.g.colors_name = "catppuccin"]] } end end os.execute(string.format("mkdir %s %s", vim.loop.os_uname().sysname == 'Windows' and "" or "-p", config.compile.path)) - print(string.format("mkdir %s %s", vim.loop.os_uname().sysname == 'Windows' and "" or "-p", config.compile.path)) local file = io.open(config.compile.path .. (vim.loop.os_uname().sysname == 'Windows' and "\\" or "/") .. vim.g.catppuccin_flavour .. config.compile.suffix .. ".lua", "w") file:write(table.concat(lines, "\n")) file:close() end +function util.clean() + local config = require("catppuccin.config").options + local compiled_path = config.compile.path .. (vim.loop.os_uname().sysname == 'Windows' and "\\" or "/") .. vim.g.catppuccin_flavour .. config.compile.suffix .. ".lua" + os.remove(compiled_path) +end + return util diff --git a/plugin/catppuccin.vim b/plugin/catppuccin.vim index 141b212..7395b6b 100644 --- a/plugin/catppuccin.vim +++ b/plugin/catppuccin.vim @@ -10,5 +10,7 @@ function! s:ApplyFlavour(args) abort endfunction command! -nargs=1 -complete=custom,s:FlavourCompletion Catppuccin call s:ApplyFlavour([]) +command! CatppuccinCompile lua require("catppuccin.utils.util").compile() +command! CatppuccinClean lua require("catppuccin.utils.util").clean() let g:loaded_catppuccin = 1