Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .luarc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"diagnostics.globals": [
"vim"
]
}
23 changes: 17 additions & 6 deletions lua/raphael/core/autocmds.lua
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,23 @@ function M.picker_cursor_autocmd(picker_buf, cbs)
end
cbs = cbs or {}
local parse = cbs.parse
local preview = cbs.preview
local preview_fn = cbs.preview
local highlight = cbs.highlight
local update_preview = cbs.update_preview

local debounce_utils = require("raphael.utils.debounce")
local debounced_preview = debounce_utils.debounce(function(theme)
if theme and type(preview_fn) == "function" then
preview_fn(theme)
end
end, 100)

local debounced_update_preview = debounce_utils.debounce(function(opts)
if type(update_preview) == "function" then
update_preview(opts or { debounced = true })
end
end, 50)

vim.api.nvim_create_autocmd("CursorMoved", {
buffer = picker_buf,
callback = function()
Expand All @@ -254,15 +267,13 @@ function M.picker_cursor_autocmd(picker_buf, cbs)
if type(parse) == "function" then
theme = parse(line)
end
if theme and type(preview) == "function" then
preview(theme)
if theme then
debounced_preview(theme)
end
if type(highlight) == "function" then
highlight()
end
if type(update_preview) == "function" then
update_preview({ debounced = true })
end
debounced_update_preview({ debounced = true })
end,
})
end
Expand Down
14 changes: 14 additions & 0 deletions lua/raphael/core/cache.lua
Original file line number Diff line number Diff line change
Expand Up @@ -315,18 +315,21 @@ function M.toggle_bookmark(theme, scope)
state.bookmarks[scope] = list

M.write(state)
---@diagnostic disable-next-line: redundant-return-value
return false, state.bookmarks
else
if #list >= constants.MAX_BOOKMARKS then
vim.notify(
string.format("raphael.nvim: Max bookmarks (%d) reached in scope '%s'!", constants.MAX_BOOKMARKS, scope),
vim.log.levels.WARN
)
---@diagnostic disable-next-line: redundant-return-value
return false, nil
end
table.insert(list, theme)
state.bookmarks[scope] = list
M.write(state)
---@diagnostic disable-next-line: redundant-return-value
return true, state.bookmarks
end
end
Expand Down Expand Up @@ -409,6 +412,14 @@ function M.get_all_usage()
return state.usage or {}
end

--- Clear expired entries from the palette cache
function M.clear_expired_palette_cache()
local ok, palette_cache = pcall(require, "raphael.core.palette_cache")
if ok and palette_cache and palette_cache.clear_expired then
palette_cache.clear_expired()
end
end

--- Get or set collapsed state for a group key in persistent state.
---
--- If `collapsed` is provided, it sets the value and writes to disk.
Expand Down Expand Up @@ -511,6 +522,7 @@ end
--- @param theme string
--- @param scope string|nil
function M.set_quick_slot(slot, theme, scope)
---@diagnostic disable-next-line: cast-local-type
slot = normalize_slot(slot)
scope = scope or "__global"
if not slot then
Expand All @@ -537,6 +549,7 @@ end
--- @param slot string|number
--- @param scope string|nil
function M.clear_quick_slot(slot, scope)
---@diagnostic disable-next-line: cast-local-type
slot = normalize_slot(slot)
scope = scope or "__global"
if not slot then
Expand All @@ -557,6 +570,7 @@ end
--- @param scope string|nil
--- @return string|nil
function M.get_quick_slot(slot, scope)
---@diagnostic disable-next-line: cast-local-type
slot = normalize_slot(slot)
scope = scope or "__global"
if not slot then
Expand Down
58 changes: 54 additions & 4 deletions lua/raphael/core/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ local history = require("raphael.extras.history")
local themes = require("raphael.themes")
local picker = require("raphael.picker.ui")

local palette_cache = nil
local function get_palette_cache()
if not palette_cache then
palette_cache = require("raphael.core.palette_cache")
end
return palette_cache
end

local M = {}

--- Base configuration as provided by the user (after validation).
Expand Down Expand Up @@ -137,7 +145,6 @@ local function make_effective_config(base, profile_name)
end

local effective = vim.tbl_deep_extend("force", base_copy, prof_cfg)
-- Ensure profiles table itself isn't overridden by profile contents.
effective.profiles = profiles
effective.current_profile = profile_name
return effective
Expand Down Expand Up @@ -180,7 +187,6 @@ function M.setup(config)
M.config = make_effective_config(M.base_config, requested_profile)
M.state.current_profile = requested_profile

-- Refresh scoped state snapshots from disk
M.state.bookmarks = cache.get_bookmarks_table()
M.state.quick_slots = cache.get_quick_slots_table()

Expand Down Expand Up @@ -387,7 +393,33 @@ end

---@param opts table|nil
function M.open_picker(opts)
return picker.open(M, opts or {})
opts = opts or {}

local preload_themes = vim.schedule_wrap(function()
local themes_to_preload = {}
if opts.exclude_configured then
local all_installed = vim.tbl_keys(themes.installed)
local all_configured = themes.get_all_themes()
for _, theme in ipairs(all_installed) do
if not vim.tbl_contains(all_configured, theme) then
table.insert(themes_to_preload, theme)
end
end
else
themes_to_preload = themes.get_all_themes()
end

local palette_cache_mod = get_palette_cache()
if palette_cache_mod then
palette_cache_mod.preload_palettes(themes_to_preload)
end
end)

local result = picker.open(M, opts or {})

preload_themes()

return result
end

--- Set a quick favorite slot (0–9) to a theme (profile-aware if profile_scoped_state=true).
Expand Down Expand Up @@ -451,7 +483,6 @@ function M.set_profile(name, apply_default)
M.config = effective
M.state.current_profile = name

-- refresh bookmarks/quick_slots from disk (so scope view is correct)
M.state.bookmarks = cache.get_bookmarks_table()
M.state.quick_slots = cache.get_quick_slots_table()

Expand Down Expand Up @@ -573,4 +604,23 @@ function M.get_profile_config(name)
return make_effective_config(M.base_config, name)
end

-- Set up periodic cleanup of expired cache entries (lazy initialization)
local function setup_periodic_cleanup()
vim.schedule(function()
vim.defer_fn(function()
local ok, palette_cache_mod = pcall(get_palette_cache)
if ok and palette_cache_mod and palette_cache_mod.clear_expired then
palette_cache_mod.clear_expired()
end
local ok2, cache_mod = pcall(require, "raphael.core.cache")
if ok2 and cache_mod and cache_mod.clear_expired_palette_cache then
cache_mod.clear_expired_palette_cache()
end
setup_periodic_cleanup()
end, 300000) -- ~ 5 minutes
end)
end

vim.defer_fn(setup_periodic_cleanup, 10000)

return M
Loading