diff --git a/lua/lazyvim/config/init.lua b/lua/lazyvim/config/init.lua index 07bb1ed6..4a9a0f22 100644 --- a/lua/lazyvim/config/init.lua +++ b/lua/lazyvim/config/init.lua @@ -130,7 +130,7 @@ local defaults = { M.json = { data = { version = nil, ---@type string? - hashes = {}, ---@type table + news = {}, ---@type table extras = {}, ---@type string[] }, } @@ -144,6 +144,11 @@ function M.json.load() local ok, json = pcall(vim.json.decode, data, { luanil = { object = true, array = true } }) if ok then M.json.data = vim.tbl_deep_extend("force", M.json.data, json or {}) + if M.json.data.hashes then + ---@diagnostic disable-next-line: no-unknown + M.json.data.hashes = nil + M.json.save() + end end end end @@ -152,7 +157,7 @@ function M.json.save() local path = vim.fn.stdpath("config") .. "/lazyvim.json" local f = io.open(path, "w") if f then - f:write(vim.json.encode(M.json.data)) + f:write(Util.json.encode(M.json.data)) f:close() end end diff --git a/lua/lazyvim/util/init.lua b/lua/lazyvim/util/init.lua index 2751e8e6..ac2b4055 100644 --- a/lua/lazyvim/util/init.lua +++ b/lua/lazyvim/util/init.lua @@ -12,6 +12,7 @@ local LazyUtil = require("lazy.core.util") ---@field extras lazyvim.util.extras ---@field inject lazyvim.util.inject ---@field news lazyvim.util.news +---@field json lazyvim.util.json local M = {} ---@type table diff --git a/lua/lazyvim/util/json.lua b/lua/lazyvim/util/json.lua new file mode 100644 index 00000000..73c4fb15 --- /dev/null +++ b/lua/lazyvim/util/json.lua @@ -0,0 +1,48 @@ +local Util = require("lazyvim.util") + +---@class lazyvim.util.json +local M = {} + +---@param value any +---@param indent string +local function encode(value, indent) + local t = type(value) + + if t == "string" then + return string.format("%q", value) + elseif t == "number" or t == "boolean" then + return tostring(value) + elseif t == "table" then + local is_list = Util.is_list(value) + local parts = {} + local next_indent = indent .. " " + + if is_list then + ---@diagnostic disable-next-line: no-unknown + for _, v in ipairs(value) do + local e = encode(v, next_indent) + if e then + table.insert(parts, next_indent .. e) + end + end + return "[\n" .. table.concat(parts, ",\n") .. "\n" .. indent .. "]" + else + local keys = vim.tbl_keys(value) + table.sort(keys) + ---@diagnostic disable-next-line: no-unknown + for _, k in ipairs(keys) do + local e = encode(value[k], next_indent) + if e then + table.insert(parts, next_indent .. string.format("%q", k) .. ": " .. e) + end + end + return "{\n" .. table.concat(parts, ",\n") .. "\n" .. indent .. "}" + end + end +end + +function M.encode(value) + return encode(value, "") +end + +return M diff --git a/lua/lazyvim/util/news.lua b/lua/lazyvim/util/news.lua index 4e28148b..a4517bf5 100644 --- a/lua/lazyvim/util/news.lua +++ b/lua/lazyvim/util/news.lua @@ -38,6 +38,7 @@ end ---@param file string ---@param opts? {plugin?:string, rtp?:boolean, when_changed?:boolean} function M.open(file, opts) + local ref = file opts = opts or {} if opts.plugin then local plugin = require("lazy.core.config").plugins[opts.plugin] --[[@as LazyPlugin?]] @@ -55,10 +56,10 @@ function M.open(file, opts) if opts.when_changed then local hash = M.hash(file) - if hash == Config.json.data.hashes[file] then + if hash == Config.json.data.news[ref] then return end - Config.json.data.hashes[file] = hash + Config.json.data.news[ref] = hash Config.json.save() end