summaryrefslogtreecommitdiff
path: root/tools/luals-gendoc/gendoc.lua
blob: 5365ddfd5419b6bef9507ec10ccd9fb30f0b106f (plain)
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
-- SPDX-FileCopyrightText: 2023 jacqueline <me@jacqueline.id.au>
--
-- SPDX-License-Identifier: GPL-3.0-only

#!/usr/bin/env lua

local json = require "json"

if #arg > 0 then
  print("usage:", arg[0])
  print([[
reads a lua-language-server json doc output from stdin, converts it into
markdown, and writes the result to stdout]])
  return
end

local raw_data = io.read("*all")
local parsed = json.decode(raw_data)

local definitions_per_module = {}
local fields_per_class = {}

for _, class in ipairs(parsed) do
  if not class.defines or not class.defines[1] then goto continue end

  -- Filter out any definitions that didn't come from us.
  local path = class.defines[1].file
  if not string.find(path, "/luals-stubs/", 1, true) then goto continue end

  local module_name = string.gsub(path, ".*/(%a*)%.lua", "%1")
  local module = definitions_per_module[module_name] or {}
  module[class.name] = class
  definitions_per_module[module_name] = module

  local fields = {}
  for _, field in ipairs(class.fields or {}) do
    fields[field.name] = true
  end
  fields_per_class[class.name] = fields

  ::continue::
end

local function sortedPairs(t)
  local keys = {}
  for key in pairs(t) do
    table.insert(keys, key)
  end
  table.sort(keys)
  local generator = coroutine.create(function()
    for _, key in ipairs(keys) do
      coroutine.yield(key, t[key])
    end
  end)
  return function()
    local _, key, val = coroutine.resume(generator)
    return key, val
  end
end

local function printHeading(level, text)
  local hashes = ""
  for _ = 1, level do hashes = hashes .. "#" end
  print(hashes .. " " .. text)
end

local function filterArgs(field)
  if not field.extends.args then return {} end
  local ret = {}
  for _, arg in ipairs(field.extends.args) do
    if arg.name ~= "self" then table.insert(ret, arg) end
  end
  return ret
end

local function filterReturns(field)
  if not field.extends.returns then return {} end
  local ret = {}
  for _, r in ipairs(field.extends.returns) do
    if r.desc then table.insert(ret, r) end
  end
  return ret
end

local function emitField(level, prefix, field)
  if not field.desc then return end

  printHeading(level, "`" .. prefix .. "." .. field.name .. "`")
  print()
  print("`" .. field.extends.view .. "`")
  print()

  if field.rawdesc then
    print(field.rawdesc)
    print()
  end

  local args = filterArgs(field)
  if #args > 0 then
    printHeading(level + 1, "Arguments")
    print()

    for _, arg in ipairs(args) do
      print(string.format(" - *%s*: %s", arg.name, arg.desc))
    end

    print()
  end

  local rets = filterReturns(field)
  if #rets > 0 then
    printHeading(level + 1, "Returns")
    print()

    for _, ret in ipairs(rets) do
      if #rets > 1 then
        print(" - " .. ret.desc)
      else
        print(ret.desc)
      end
    end

    print()
  end
end

local function baseClassName(class)
  for _, define in ipairs(class.defines or {}) do
    for _, extend in ipairs(define.extends or {}) do
      if extend.type == "doc.extends.name" then
        return extend.view
      end
    end
  end
end

local function isEnum(class)
  for _, define in pairs(class.defines) do
    if define.type == "doc.enum" then return true end
  end
  return false
end

local function isAlias(class)
  for _, define in pairs(class.defines) do
    if define.type == "doc.alias" then return true end
  end
  return false
end

local function emitClass(level, prefix, class)
  if not class.name then return end
  if not class.fields then return end
  if isAlias(class) then return end

  for _, define in ipairs(class.defines or {}) do
    if define.type == "tablefield" then
      print(" - " .. class.name)
      return
    end
  end

  printHeading(level, "`" .. prefix .. "." .. class.name .. "`")
  print()

  local base_class = baseClassName(class)
  local base_class_fields = {}
  if base_class then
    base_class_fields = fields_per_class[base_class] or {}
    print("`" .. class.name .. ":" .. base_class .. "`")
    print()
  end

  if class.desc then print(class.desc) end

  for _, field in ipairs(class.fields or {}) do
    if not base_class_fields[field.name] then
      emitField(level + 1, class.name, field)
    end
  end

  if isEnum(class) then
    printHeading(level + 1, "Values")
    print()
  end
end

local initial_level = 3

for name, module in sortedPairs(definitions_per_module) do
  printHeading(initial_level, "`" .. name .. "`")

  local top_level_class = module[name]
  if top_level_class then
    if top_level_class.desc then print(top_level_class.desc) end
    for _, field in ipairs(top_level_class.fields or {}) do
      emitField(initial_level + 1, name, field)
    end
  end

  for _, class in sortedPairs(module) do
    if class.name ~= name then
      emitClass(initial_level + 1, name, class)
    end
  end
end