EdenEast / nightfox.nvim

🦊A highly customizable theme for vim and neovim with support for lsp, treesitter and a variety of plugins.
MIT License
3.1k stars 143 forks source link

Config with lua functions #244

Open EdenEast opened 1 year ago

EdenEast commented 1 year ago

Now that pr #241 has landed I would like to change now nightfox is configured. Instead of having a lua table that you fill in and extend with nightfox I would like to expose this as a lua function instead. Here is a mock up of what a config would look like.

require("nightfox").setup({
  options = {},
  on_palette = function(palette, name, color)
    if name == "nightfox" then
      local red = color.new("#ff0000")
      palette.red.base = red:to_css()
      palette.red.bright = red:brighten(10):to_css()
      palette.red.dim = red:lighten(-10):to_css()
    elseif name == "dayfox" then
      local shade = require("nightfox.lib.shade")
      palette.blue = shade.new("#0000ff", 0.15, -0, 15)
    elseif name == "terafox" then
      palette.green.base = "#00ff00"
    end
  end,
  on_spec = function(spec, name, color)
    local p = spec.palette
    -- Note: Only overrding a single value as setting syntax to a new table would remove other values
    spec.syntax.operator = p.orange.base
    spec.syntax.builtin0 = p.pink.bright
    spec.syntax.builtin1 = p.cyan:harsh() -- bright for dark themes and dim for light
    spec.syntax.builtin2 = p.yellow:subtle() -- dim for dark themes and bright for light
  end,
  on_highlight = function(spec, hl, name, color)
    local p = spec.palette
    hl.TelescopeBorder = { fg = spec.bg4 }
    hl.TelescopeTitle = { fg = spec.fg2, bg = spec.bg4 }

    hl.CmpItemKindFunction = { fg = p.magenta.base }
    hl.CmpItemKindMethod = { fg = p.magenta.base }
    hl.CmpWindowBorder = { fg = spec.bg0, bg = spec.bg0 }

    hl["@attribute"] = { link = "Constant" }
  end,
})

In the example above, name would be the name of the style, and color would be nightfox's Color manipulation library.

User's configuration is now hashed and cached these functions would only be executed when there is a change to nightfox or the file that the nightfox setup function is called.

nullchilly commented 1 year ago

I am not sure I like the hl. spec. prefixes

hl = {
    TelescopeBorder = { fg = spec.bg4 }
    TelescopeTitle = { fg = spec.fg2, bg = spec.bg4 }

    CmpItemKindFunction = { fg = p.magenta.base }
    CmpItemKindMethod = { fg = p.magenta.base }
    CmpWindowBorder = { fg = spec.bg0, bg = spec.bg0 }
}

Seems better to me as an user

EdenEast commented 1 year ago

The idea would be that the function would be passed the actual lua tables that nightfox uses. You can then do what you want and reference what you want with that lua table.

One of the issues with that is that users can override the entire highlight group table for example. Since this is also just a lua function the user can just deep extend a local table that they create.

on_highlight = function(spec, hl, name, color)
  local p = spec.palette

  local override = {
    TelescopeBorder = { fg = spec.bg4 },
    TelescopeTitle = { fg = spec.fg2, bg = spec.bg4 },

    CmpItemKindFunction = { fg = p.magenta.base },
    CmpItemKindMethod = { fg = p.magenta.base },
    CmpWindowBorder = { fg = spec.bg0, bg = spec.bg0 },
  }

  vim.tbl_deep_extend("force", hl, override)
end

Maybe instead of modding the actual table the function would expect to return a table that would would then be extended in nightfox.

nullchilly commented 1 year ago

Keep in mind that there isn't a vim.tbl_deep_extend inside vim's lua

As a result I added this https://github.com/catppuccin/nvim/blob/main/lua/catppuccin/lib/vim/init.lua

Maybe instead of modding the actual table the function would expect to return a table that would would then be extended in nightfox.

Yes, that's what I do in catppuccin