aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/gen_annotations.lua
blob: 716ea1ec57f33e6ad1fbf4cfeb67668371802570 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
---Merge maps recursively and replace non-map values.
---@param ... any Values to merge in order.
---@return any
local function merge(...)
  ---Return whether a value can be merged as a map.
  ---@param value any Candidate value.
  ---@return boolean
  local function is_mergeable(value)
    return type(value) == 'table' and (vim.tbl_isempty(value) or not vim.islist(value))
  end

  local values = { ... }
  local merged = values[1]
  for i = 2, #values, 1 do
    local next_value = values[i]
    if is_mergeable(merged) and is_mergeable(next_value) then
      for key, item in pairs(next_value) do
        merged[key] = merge(merged[key], item)
      end
    else
      merged = next_value
    end
  end
  return merged
end

---@class Settings
---@field _settings table
---@field file string
local Settings = {}
Settings.__index = Settings

---Create a settings store from dotted keys.
---@param settings? table Initial dotted-key values.
---@return Settings
function Settings.new(settings)
  local self = setmetatable({ _settings = {} }, Settings)
  for key, value in pairs(settings or {}) do
    self:set(key, value)
  end
  return self
end

---Expand dotted keys in a table into nested tables.
---@param value any Source value.
---@return any
function Settings.expand_keys(value)
  if type(value) ~= 'table' then
    return value
  end
  local expanded = Settings.new()
  for key, item in pairs(value) do
    expanded:set(key, item)
  end
  return expanded:get()
end

---Split a dotted key into path segments.
---@param key any Dotted key or raw key value.
---@return any[]
function Settings.split_key(key)
  if not key or key == '' then
    return {}
  end
  if type(key) ~= 'string' then
    return { key }
  end
  local parts = {}
  for part in string.gmatch(key, '[^.]+') do
    table.insert(parts, part)
  end
  return parts
end

---Store a value at a dotted key path.
---@param key any Target dotted key.
---@param value any Value to store.
function Settings:set(key, value)
  local parts = Settings.split_key(key)

  if #parts == 0 then
    self._settings = value
    return
  end

  local node = self._settings
  for i = 1, #parts - 1, 1 do
    local part = parts[i]
    if type(node[part]) ~= 'table' then
      node[part] = {}
    end
    node = node[part]
  end
  node[parts[#parts]] = value
end

---Read a value from the settings store.
---@param key? any Dotted key to read.
---@param opts? {defaults?:table, expand?:boolean} Read options.
---@return any
function Settings:get(key, opts)
  ---@type table|nil
  local node = self._settings

  for _, part in ipairs(Settings.split_key(key)) do
    if type(node) ~= 'table' then
      node = nil
      break
    end
    node = node[part]
  end

  if opts and opts.expand and type(node) == 'table' then
    node = Settings.expand_keys(node)
  end

  if opts and opts.defaults then
    if node == nil then
      return vim.deepcopy(opts.defaults)
    end
    if type(node) ~= 'table' then
      return node
    end
    node = merge({}, opts.defaults, node)
  end

  return node
end

---Format schema text as Lua comments.
---@param desc? string Comment body.
---@param prefix? string Optional line prefix.
---@return string?
local function format_comment(desc, prefix)
  if desc then
    prefix = (prefix or '') .. '---'
    -- Lua also ends a comment at `\r`.
    return prefix .. desc:gsub('\r\n?', '\n'):gsub('\n', '\n' .. prefix)
  end
end

---Append a property's description comment.
---@param lines string[] Output buffer.
---@param prop table Schema property.
---@param prefix? string Optional line prefix.
local function append_description(lines, prop, prefix)
  local description = prop.markdownDescription or prop.description
  if type(description) == 'table' and description.message then
    description = description.message
  end
  if prop.default then
    if prop.default == vim.NIL then
      prop.default = nil
    end
    if type(prop.default) == 'table' and vim.tbl_isempty(prop.default) then
      prop.default = {}
    end
    description = (description and (description .. '\n\n') or '')
      .. '```lua\ndefault = '
      .. vim.inspect(prop.default)
      .. '\n```'
  end
  if description then
    table.insert(lines, format_comment(description, prefix))
  end
end

---Wrap nested schema nodes as object properties.
---@param node table Schema node tree.
---@return table
local function normalize_properties(node)
  return node.leaf and node
    or {
      type = 'object',
      properties = vim.tbl_map(function(child)
        return normalize_properties(child)
      end, node),
    }
end

---Normalize one schema path segment into a valid LuaLS class name segment.
---@param segment string
---@return string
local function segment_name_for_annotation(segment)
  local words = {}
  for word in string.gmatch(segment, '[%w]+') do
    if word:match('^%d') then
      word = '_' .. word
    end
    table.insert(words, word:sub(1, 1):upper() .. word:sub(2))
  end
  return #words > 0 and table.concat(words) or '_'
end

---Build the Lua class name for a nested schema object.
---`lspconfig.settings.x` + `foo` => `_.lspconfig.settings.x.Foo`, then + `bar` => `_.lspconfig.settings.x.Foo.Bar`.
---@param class_name string Parent class name.
---@param field string
---@return string
local function child_class_name(class_name, field)
  local parent = vim.startswith(class_name, '_.') and class_name or ('_.%s'):format(class_name)
  return ('%s.%s'):format(parent, segment_name_for_annotation(field))
end

---@type table<string, true>
local lua_keywords = {
  ['and'] = true,
  ['break'] = true,
  ['do'] = true,
  ['else'] = true,
  ['elseif'] = true,
  ['end'] = true,
  ['false'] = true,
  ['for'] = true,
  ['function'] = true,
  ['goto'] = true,
  ['if'] = true,
  ['in'] = true,
  ['local'] = true,
  ['nil'] = true,
  ['not'] = true,
  ['or'] = true,
  ['package'] = true,
  ['repeat'] = true,
  ['return'] = true,
  ['then'] = true,
  ['true'] = true,
  ['until'] = true,
  ['while'] = true,
}

---Format a schema field name for use in a LuaLS `---@field` annotation.
---@param field string
---@return string
local function field_name_for_annotation(field)
  if field:match('^[A-Za-z_][A-Za-z0-9_]*$') and not lua_keywords[field] then
    return field
  end
  return '[' .. vim.inspect(field) .. ']'
end

---LuaLS type of each JSON Schema `type`, except `array` (see lua_type_for). Others (`null`, invalid) are dropped.
---@type table<string, string>
local json_types = {
  boolean = 'boolean',
  integer = 'integer',
  number = 'number',
  object = 'table',
  string = 'string',
}

---Format a JSON value as a LuaLS literal type.
---@param value any
---@return string
local function literal_type_for(value)
  return type(value) == 'table' and 'table' or vim.inspect(value)
end

---Convert a schema property into a Lua type.
---@param prop any Schema property.
---@param refs table<string, string> Class name of each `$ref` target (see generate_file_annotations).
---@return string
local function lua_type_for(prop, refs)
  if type(prop) ~= 'table' then
    return 'any'
  end
  if prop['$ref'] then
    return refs[prop['$ref']] or 'any'
  end
  if prop.const ~= nil and type(prop.type) ~= 'table' then
    return literal_type_for(prop.const)
  end
  local types = {}
  if prop.enum then
    types = vim.tbl_map(literal_type_for, prop.enum)
  elseif prop.type then
    for _, t in ipairs(type(prop.type) == 'table' and prop.type or { prop.type }) do
      if t == 'array' then
        local item_type = lua_type_for(prop.items, refs)
        table.insert(types, (item_type:find('|', 1, true) and '(%s)[]' or '%s[]'):format(item_type))
      elseif json_types[t] then
        table.insert(types, json_types[t])
      end
    end
  else
    -- Convert `anyOf`/`oneOf` to a union, excluding null:
    -- `{ anyOf = { { type = 'string' }, { type = 'number' }, { type = 'null' } } }` => `string|number`.
    for _, alternative in ipairs(prop.anyOf or prop.oneOf or {}) do
      if type(alternative) ~= 'table' or alternative.type ~= 'null' then
        table.insert(types, lua_type_for(alternative, refs))
      end
    end
  end
  return #types > 0 and table.concat(vim.list.unique(types), '|') or 'any'
end

---Get the object schema (with `properties`) declared by `prop`, if any.
---`{ anyOf = { { type = 'object', properties = … }, { type = 'null' } } }` => the first alternative.
---@param prop any Schema property.
---@return table?
local function object_schema(prop)
  if type(prop) ~= 'table' then
    return nil
  end
  if prop.type == 'object' and prop.properties then
    return prop
  end
  local alternatives = vim.tbl_filter(function(alternative)
    return type(alternative) ~= 'table' or alternative.type ~= 'null'
  end, prop.anyOf or prop.oneOf or {})
  return #alternatives == 1 and object_schema(alternatives[1]) or nil
end

---Return whether a field is required by its parent schema.
---@param parent table
---@param field string
---@param child table
---@return boolean
local function is_required_field(parent, field, child)
  if child.required == true then
    return true
  end

  local required = parent.required
  if type(required) ~= 'table' then
    return false
  end

  for _, required_field in ipairs(required) do
    if required_field == field then
      return true
    end
  end

  return false
end

---Append annotations for an object node and its children.
---@param lines string[] Output buffer.
---@param class_name string
---@param prop table Object property schema.
---@param refs table<string, string> (see lua_type_for)
local function append_object(lines, class_name, prop, refs)
  local object_lines = {}
  append_description(object_lines, prop)
  table.insert(object_lines, ('---@class %s'):format(class_name))
  local fields = vim.tbl_keys(prop.properties or {})
  table.sort(fields)
  for _, field in ipairs(fields) do
    local child = type(prop.properties[field]) == 'table' and prop.properties[field] or {}
    local optional_marker = is_required_field(prop, field, child) and '' or '?'
    append_description(object_lines, child)

    local object = object_schema(child)
    local field_type = object and child_class_name(class_name, field) or lua_type_for(child, refs)
    table.insert(
      object_lines,
      ('---@field %s%s %s'):format(field_name_for_annotation(field), optional_marker, field_type)
    )
    if object then
      append_object(lines, field_type, object, refs)
    end
  end
  table.insert(lines, '')
  vim.list_extend(lines, object_lines)
end

---Append annotations for a schema definition.
---@param lines string[] Output buffer.
---@param class_name string
---@param def table Definition schema.
---@param refs table<string, string> (see lua_type_for)
local function append_definition(lines, class_name, def, refs)
  local object = object_schema(def)
  if object then
    append_object(lines, class_name, object, refs)
    return
  end
  table.insert(lines, '')
  append_description(lines, def)
  if not def.enum then
    table.insert(lines, ('---@alias %s %s'):format(class_name, lua_type_for(def, refs)))
    return
  end
  -- One member per line keeps diffs small for huge enums (e.g. ruff `RuleSelector`).
  table.insert(lines, ('---@alias %s'):format(class_name))
  for _, value in ipairs(def.enum) do
    table.insert(lines, ('---| %s'):format(literal_type_for(value)))
  end
end

---Generate annotation lines for one schema file.
---@param file string Schema file path.
---@return string[]
local function generate_file_annotations(file)
  local name = vim.fn.fnamemodify(file, ':t:r')
  local json = vim.json.decode(vim.fn.readblob(file), { luanil = { array = true, object = true } }) or {}
  local class_name = 'lspconfig.settings.' .. name
  local lines = { '---@meta' }

  -- `{ ['$ref'] = '#/$defs/Foo' }` => `_.lspconfig.settings.<name>.defs.Foo`, annotated once.
  local refs = {} ---@type table<string, string>
  local defs = {} ---@type table<string, table>
  for _, key in ipairs({ '$defs', 'definitions' }) do
    for def_name, def in pairs(json[key] or {}) do
      local def_class = ('_.%s.defs.%s'):format(class_name, segment_name_for_annotation(def_name))
      refs[('#/%s/%s'):format(key, def_name)] = def_class
      defs[def_class] = type(def) == 'table' and def or {}
    end
  end
  for def_class, def in vim.spairs(defs) do
    append_definition(lines, def_class, def, refs)
  end

  local schema = Settings.new()
  for key, prop in pairs(json.properties) do
    prop = type(prop) == 'table' and prop or {}
    prop.leaf = true
    schema:set(key, prop)
  end

  append_object(lines, class_name, normalize_properties(schema:get()), refs)
  return vim.tbl_filter(function(v)
    return v ~= nil
  end, lines)
end

---Generate Lua annotation files from the schemas directory.
---@return nil
local function generate_all_annotations()
  local schema_dir = vim.fs.joinpath(vim.uv.cwd(), 'schemas')
  local output_dir = vim.fs.joinpath(vim.uv.cwd(), 'lua', 'lspconfig', 'types', 'lsp')

  vim.fn.delete(output_dir, 'rf')
  vim.fn.mkdir(output_dir, 'p')

  for name, type in vim.fs.dir(schema_dir) do
    if type == 'file' and vim.endswith(name, '.json') then
      local file = vim.fs.joinpath(schema_dir, name)
      local lines = generate_file_annotations(file)
      local output_file = vim.fs.joinpath(output_dir, vim.fn.fnamemodify(name, ':r') .. '.lua')
      vim.fn.writefile(vim.split(table.concat(lines, '\n') .. '\n', '\n', { plain = true }), output_file, 'b')
    end
  end
end

generate_all_annotations()