mirror of
https://github.com/LazyVim/LazyVim.git
synced 2025-06-27 11:19:02 +02:00
fix(config): make lazyvim.json idempotent, pretty-printed and remove full paths
This commit is contained in:
parent
7d9a395565
commit
2a0b7a88ba
4 changed files with 59 additions and 4 deletions
48
lua/lazyvim/util/json.lua
Normal file
48
lua/lazyvim/util/json.lua
Normal file
|
@ -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
|
Loading…
Add table
Add a link
Reference in a new issue