|
| 1 | +-- https://github.com/neovim/nvim-lspconfig/blob/master/scripts/docgen.lua |
| 2 | +-- https://github.com/neovim/nvim-lspconfig/blob/master/LICENSE.md |
| 3 | +local preamble_parts = make_parts { |
| 4 | + function() |
| 5 | + if docs.description and #docs.description > 0 then |
| 6 | + return docs.description |
| 7 | + end |
| 8 | + end, |
| 9 | + function() |
| 10 | + local package_json_name = util.path.join(tempdir, template_name .. '.package.json') |
| 11 | + if docs.package_json then |
| 12 | + if not util.path.is_file(package_json_name) then |
| 13 | + os.execute(string.format('curl -v -L -o %q %q', package_json_name, docs.package_json)) |
| 14 | + end |
| 15 | + if not util.path.is_file(package_json_name) then |
| 16 | + print(string.format('Failed to download package.json for %q at %q', template_name, docs.package_json)) |
| 17 | + os.exit(1) |
| 18 | + return |
| 19 | + end |
| 20 | + local data = fn.json_decode(readfile(package_json_name)) |
| 21 | + -- The entire autogenerated section. |
| 22 | + return make_section(0, '\n', { |
| 23 | + -- The default settings section |
| 24 | + function() |
| 25 | + local default_settings = (data.contributes or {}).configuration |
| 26 | + if not default_settings.properties then |
| 27 | + return |
| 28 | + end |
| 29 | + -- The outer section. |
| 30 | + return make_section(0, '\n', { |
| 31 | + 'This server accepts configuration via the `settings` key.', |
| 32 | + '<details><summary>Available settings:</summary>', |
| 33 | + '', |
| 34 | + -- The list of properties. |
| 35 | + make_section( |
| 36 | + 0, |
| 37 | + '\n\n', |
| 38 | + sorted_map_table(default_settings.properties, function(k, v) |
| 39 | + local function tick(s) |
| 40 | + return string.format('`%s`', s) |
| 41 | + end |
| 42 | + local function bold(s) |
| 43 | + return string.format('**%s**', s) |
| 44 | + end |
| 45 | + |
| 46 | + -- https://github.github.com/gfm/#backslash-escapes |
| 47 | + local function excape_markdown_punctuations(str) |
| 48 | + local pattern = |
| 49 | + '\\(\\*\\|\\.\\|?\\|!\\|"\\|#\\|\\$\\|%\\|\'\\|(\\|)\\|,\\|-\\|\\/\\|:\\|;\\|<\\|=\\|>\\|@\\|\\[\\|\\\\\\|\\]\\|\\^\\|_\\|`\\|{\\|\\\\|\\|}\\)' |
| 50 | + return fn.substitute(str, pattern, '\\\\\\0', 'g') |
| 51 | + end |
| 52 | + |
| 53 | + -- local function pre(s) return string.format("<pre>%s</pre>", s) end |
| 54 | + -- local function code(s) return string.format("<code>%s</code>", s) end |
| 55 | + if not (type(v) == 'table') then |
| 56 | + return |
| 57 | + end |
| 58 | + return make_section(0, '\n', { |
| 59 | + '- ' .. make_section(0, ': ', { |
| 60 | + bold(tick(k)), |
| 61 | + function() |
| 62 | + if v.enum then |
| 63 | + return tick('enum ' .. inspect(v.enum)) |
| 64 | + end |
| 65 | + if v.type then |
| 66 | + return tick(table.concat(tbl_flatten { v.type }, '|')) |
| 67 | + end |
| 68 | + end, |
| 69 | + }), |
| 70 | + '', |
| 71 | + make_section(2, '\n\n', { |
| 72 | + { v.default and 'Default: ' .. tick(inspect(v.default, { newline = '', indent = '' })) }, |
| 73 | + { v.items and 'Array items: ' .. tick(inspect(v.items, { newline = '', indent = '' })) }, |
| 74 | + { excape_markdown_punctuations(v.description) }, |
| 75 | + }), |
| 76 | + }) |
| 77 | + end) |
| 78 | + ), |
| 79 | + '', |
| 80 | + '</details>', |
| 81 | + }) |
| 82 | + end, |
| 83 | + }) |
| 84 | + end |
| 85 | + end, |
| 86 | +} |
0 commit comments