aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/docgen.lua
blob: a430e70bc3b52fd227071028e6037c5b374f6911 (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
require 'nvim_lsp'
local skeleton = require 'nvim_lsp/skeleton'
local inspect = vim.inspect

local function filter(...)
  local lines = {}
  for i = 1, select("#", ...) do
    local v = select(i, ...)
    if v then
      table.insert(lines, v)
    end
  end
  return lines
end

local function nilifempty(s)
  if #s == 0 then return end
  return s
end

local function dedent(s)
  local lines = vim.split(s, '\n', true)
  if #lines == 0 then
    return ""
  end
  local indent = #lines[1]:match("^%s*")
  for i = 1, #lines do
    lines[i] = lines[i]:sub(indent)
  end
  return table.concat(lines, '\n')
end

local function template(s, params)
  return (s:gsub("{{([^{}]+)}}", params))
end

local function map_list(t, fn)
  local res = {}
  for i, v in ipairs(t) do table.insert(res, fn(v, i)) end
  return res
end

local function indent(n, s)
  if n <= 0 then return s end
  local lines = vim.split(s, '\n', true)
  for i, line in ipairs(lines) do
    lines[i] = string.rep(" ", n)..line
  end
  return table.concat(lines, '\n')
end

local skeleton_keys = vim.tbl_keys(skeleton)
table.sort(skeleton_keys)

local function make_lsp_sections()
  local sections = map_list(skeleton_keys, function(k)
    local v = skeleton[k]
    local tconf = v.template_config

    local params = {}
    params.template_name = k
    if tconf.commands then
      local lines = {"Commands:"}
      local cnames = vim.tbl_keys(tconf.commands)
      table.sort(cnames)
      for _, cname in ipairs(cnames) do
        local def = tconf.commands[cname]
        if def.description then
          table.insert(lines, string.format("- %s: %s", cname, def.description))
        else
          table.insert(lines, string.format("- %s", cname))
        end
        lines[#lines] = indent(0, lines[#lines])
      end
      params.commands = indent(0, table.concat(lines, '\n'))
    end
    if tconf.default_config then
      local lines = {}
      lines = {"Default Values:"}
      local keys = vim.tbl_keys(tconf.default_config)
      table.sort(keys)
      for _, dk in ipairs(keys) do
        local dv = tconf.default_config[dk]
        local description = tconf.docs and tconf.docs.default_config and tconf.docs.default_config[dk]
        if description and type(description) ~= 'string' then
          description = inspect(description)
        end
        table.insert(lines, indent(2, string.format("%s = %s", dk, description or inspect(dv))))
      end
      params.default_config = indent(0, table.concat(lines, '\n'))
    end
    do
      local body_lines = filter(
      params.commands
      , params.default_config
      )
      params.body = indent(2, table.concat(body_lines, '\n\n'))
    end
    params.preamble = ""
    if tconf.docs then
      local installation_instructions
      if v.install then
        installation_instructions = string.format("Can be installed in neovim with `:LspInstall %s`", k)
      end
      local preamble_parts = filter(
        nilifempty(tconf.docs.description)
        , installation_instructions
      )
      -- Insert a newline after the preamble if it exists.
      if #preamble_parts > 0 then table.insert(preamble_parts, '') end
      params.preamble = table.concat(preamble_parts, '\n')
    end

    return template([[
## {{template_name}}

{{preamble}}
```lua
nvim_lsp.{{template_name}}.setup({config})
nvim_lsp#setup("{{template_name}}", {config})

{{body}}
```
]], params)
  end)
  return table.concat(sections, '\n')
end

local function make_implemented_servers_list()
  local parts = map_list(skeleton_keys, function(k)
    return template("- [{{server}}](https://github.com/neovim/nvim-lsp#{{server}})", {server=k})
  end)
  return table.concat(parts, '\n')
end

local function generate_readme(params)
  vim.validate {
    lsp_server_details = {params.lsp_server_details, 's'};
    implemented_servers_list = {params.implemented_servers_list, 's'};
  }
  local input_template = io.open("scripts/README_template.md"):read("*a")
  local readme_data = template(input_template, params)

  local writer = io.open("README.md", "w")
  writer:write(readme_data)
  writer:close()
end

generate_readme {
  implemented_servers_list = make_implemented_servers_list();
  lsp_server_details = make_lsp_sections();
}

-- vim:et ts=2 sw=2