Skip to content

Commit

Permalink
feat: simple method to generate a json schema based on a lua table
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Sep 8, 2022
1 parent d71e54e commit 89f459f
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions lua/lsp-settings/schema.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
local M = {}

-- try to create a simple schema from a given value
function M.to_schema(value)
if value == nil then
return { type = "null" }
elseif type(value) == "boolean" then
return { type = "boolean", default = value, description = "boolean" }
elseif type(value) == "string" then
return { type = "string", default = value, description = "string" }
elseif type(value) == "number" then
return { type = "number", default = value, description = "number" }
end

if vim.tbl_islist(value) then
return { type = "array", default = value, description = "array" }
end

if type(value) == "table" then
local obj = { type = "object", properties = {} }
for k, v in pairs(value) do
obj.properties[k] = M.to_schema(v)
end
return obj
end
return { type = "null" }
end

function M.plugin_schema(name, obj, description)
local schema = M.to_schema(obj)
schema.description = description
return {
type = "object",
properties = {
[name] = schema,
},
}
end

return M

0 comments on commit 89f459f

Please sign in to comment.