AstroNvim / astrotheme

The default colorscheme used by AstroNvim
https://AstroNvim.com
GNU General Public License v3.0
97 stars 19 forks source link

Loading a palette which is not in the config table crashes #86

Closed Niverton closed 11 months ago

Niverton commented 1 year ago

Checklist

Neovim version (nvim -v)

n/a

Operating system/version

n/a

Terminal/GUI

n/a

Describe the bug

I'm trying to write a plugin to add a palette to astrotheme. I have a lua/astrotheme/paletes/astrobox.lua file in my plugin directory. Running require("astrotheme").load("astrobox") crashes during util.set_palettes when trying to extend my palette with astrotheme.config.palettes.astrobox is nil. Adding an empty table to the config fixes the issue, but requires a user to change their astrotheme's config when adding a palette plugin.

Steps to Reproduce

  1. Copy an existing palette in the "lua/astrotheme/palettes" directory with a new name (for exemple "astronew.lua")
  2. Run neovim, try to run :lua require("astrotheme").load("astronew")
  3. Observe the error

Expected behavior

The palette is loaded and the theme changes.

Screenshots

No response

Additional Context

The following changes to util.lua fix the issue (I can create a PR):

diff --git a/lua/astrotheme/lib/util.lua b/lua/astrotheme/lib/util.lua
index b8f848b..e4c1a25 100644
--- a/lua/astrotheme/lib/util.lua
+++ b/lua/astrotheme/lib/util.lua
@@ -50,13 +50,13 @@ function M.set_palettes(opts)
   local palette_name = "astrotheme.palettes." .. opts.palette
   if opts.dev then package.loaded[palette_name] = nil end
   local palette = require(palette_name)
-  palette = vim.tbl_deep_extend("force", palette, opts.palettes.global)
-  return vim.tbl_deep_extend("force", palette, opts.palettes[opts.palette])
+  palette = vim.tbl_deep_extend("force", palette, opts.palettes.global or {})
+  return vim.tbl_deep_extend("force", palette, opts.palettes[opts.palette] or {})
 end

 function M.set_highlights(opts, highlights, theme)
-  local global_hl = opts.highlights.global
-  local theme_hl = opts.highlights[theme]
+  local global_hl = opts.highlights.global or {}
+  local theme_hl = opts.highlights[theme] or {}

   pcall(global_hl.modify_hl_groups, highlights, C)
   pcall(theme_hl.modify_hl_groups, highlights, C)

Thanks a lot for your time on astronvim!

A-Lamia commented 1 year ago

You need to add your theme to the config file, if you look at config.lua you will see where you need to add it, astrotheme will fail to load any new theme that has not been added to the config.

Niverton commented 1 year ago

Hello, Yes I know I need to add it to the config, the bug report is more of a "why", or at least "why isn't the error message clearer".

A-Lamia commented 1 year ago

Well it's not clearer because i was not expecting people use astrotheme like that, the reason it's like that is for me so if i get that error i know i haven't added the config layout and maintaining the config layout is for consistency sake, I currently have a new branch that has changes to this area here ill look at making it modifiable i guess.

github-actions[bot] commented 11 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 5 days if no further activity occurs. Thank you for your contributions.