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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ This project would not exist without the hard work of some other open source pro
- [x] [denols](https://github.com/denoland/vscode_deno/tree/main/package.json)
- [x] [elixirls](https://github.com/elixir-lsp/vscode-elixir-ls/tree/master/package.json)
- [x] [elmls](https://github.com/elm-tooling/elm-language-client-vscode/tree/master/package.json)
- [x] [emmylua_ls](https://github.com/EmmyLuaLs/emmylua-analyzer-rust/86ae47efba57c2d70a5af18faa6e8418b0129b22/crates/emmylua_code_analysis/resources/schema.json)
- [x] [eslint](https://github.com/microsoft/vscode-eslint/tree/main/package.json)
- [x] [flow](https://github.com/flowtype/flow-for-vscode/tree/master/package.json)
- [x] [fsautocomplete](https://github.com/ionide/ionide-vscode-fsharp/tree/main/release/package.json)
Expand Down
56 changes: 56 additions & 0 deletions lua/codesettings/build/annotations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ function Build.flatten(t, ret)
end

function Build.get_type(prop)
-- Handle const values (literal types)
if prop.const ~= nil then
return vim.inspect(prop.const)
end

if prop.enum then
return table.concat(
vim.tbl_map(function(e)
Expand All @@ -84,6 +89,17 @@ function Build.get_type(prop)
' | '
)
end

-- Handle $ref references
if prop['$ref'] then
local ref = prop['$ref']
-- Extract definition name from refs like "#/definitions/MyType" or "#/$defs/MyType"
local def_name = ref:match('#/definitions/(.+)') or ref:match('#/%$defs/(.+)')
if def_name then
return Build.get_class(def_name)
end
end

local types = type(prop.type) == 'table' and prop.type or { prop.type }
if vim.tbl_isempty(types) and type(prop.anyOf) == 'table' then
return table.concat(
Expand All @@ -93,6 +109,14 @@ function Build.get_type(prop)
'|'
)
end
if vim.tbl_isempty(types) and type(prop.oneOf) == 'table' then
return table.concat(
vim.tbl_map(function(p)
return Build.get_type(p)
end, prop.oneOf),
'|'
)
end
types = vim.tbl_map(function(t)
if t == 'null' then
return
Expand All @@ -103,6 +127,10 @@ function Build.get_type(prop)
prop.items.type = 'any'
end
return prop.items.type .. '[]'
elseif prop.items and prop.items['$ref'] then
-- Handle array items with $ref
local item_type = Build.get_type(prop.items)
return item_type .. '[]'
end
return 'any[]'
end
Expand Down Expand Up @@ -140,11 +168,39 @@ function Build.process_object(name, prop)
vim.list_extend(Build.lines, lines)
end

-- Process definitions from the schema
function Build.process_definitions(definitions)
if not definitions or type(definitions) ~= 'table' then
return
end

local def_names = vim.tbl_keys(definitions)
table.sort(def_names)

for _, def_name in ipairs(def_names) do
local def = definitions[def_name]
-- Process objects with properties
if def.type == 'object' or def.properties then
Build.process_object(def_name, def)
-- Process enum-like definitions (oneOf with const values)
elseif def.oneOf or def.anyOf or def.enum then
local lines = {}
Build.add_desc(lines, def)
table.insert(lines, '---@alias ' .. Build.get_class(def_name) .. ' ' .. Build.get_type(def))
table.insert(Build.lines, '')
vim.list_extend(Build.lines, lines)
end
end
end

function Build.build_annotations(name)
local file = Util.path('schemas/' .. name .. '.json')
local json = Util.json_decode(Util.read_file(file)) or {}
Build.class_name = 'lsp.' .. name

-- Process definitions first (they may be referenced by properties)
Build.process_definitions(json.definitions or json['$defs'])

local schema = require('codesettings.settings').new()
for key, prop in pairs(json.properties) do
prop.leaf = true
Expand Down
16 changes: 15 additions & 1 deletion lua/codesettings/build/schemas.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ M.index = {
denols = 'https://raw.githubusercontent.com/denoland/vscode_deno/main/package.json',
elixirls = 'https://raw.githubusercontent.com/elixir-lsp/vscode-elixir-ls/master/package.json',
elmls = 'https://raw.githubusercontent.com/elm-tooling/elm-language-client-vscode/master/package.json',
emmylua_ls = 'https://raw.githubusercontent.com/EmmyLuaLs/emmylua-analyzer-rust/86ae47efba57c2d70a5af18faa6e8418b0129b22/crates/emmylua_code_analysis/resources/schema.json',
eslint = 'https://raw.githubusercontent.com/microsoft/vscode-eslint/main/package.json',
flow = 'https://raw.githubusercontent.com/flowtype/flow-for-vscode/master/package.json',
fsautocomplete = 'https://raw.githubusercontent.com/ionide/ionide-vscode-fsharp/main/release/package.json',
Expand Down Expand Up @@ -160,7 +161,12 @@ end
local SpecialCases = {
nixd = function(json)
-- nixd should be nested under "nixd"
return { nixd = { type = 'object', properties = json.properties } }
return {
nixd = {
type = 'object',
properties = json.properties,
},
}
end,
gopls = function(json)
local config = json.contributes.configuration.properties.gopls
Expand All @@ -176,6 +182,13 @@ local SpecialCases = {
end
return properties
end,
emmylua_ls = function(json)
-- emmylua_ls schema has a flat structure but expects all settings to be nested under "Lua" like lua_ls
-- when provided via lsp configuration.
return {
Lua = { type = 'object', properties = json.properties },
}
end,
}

---@param schema CodesettingsLspSchema
Expand Down Expand Up @@ -206,6 +219,7 @@ function M.fetch_schema(schema)
description = json.description,
properties = properties,
definitions = json.definitions,
['$defs'] = json['$defs'], -- seems to be an alias for definitions in some schemas
}

return ret
Expand Down
Loading