gen_vimdoc.py 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  1. #!/usr/bin/env python3
  2. """Generates Nvim :help docs from C/Lua docstrings, using Doxygen.
  3. Also generates *.mpack files. To inspect the *.mpack structure:
  4. :new | put=v:lua.vim.inspect(msgpackparse(readfile('runtime/doc/api.mpack')))
  5. Flow:
  6. main
  7. extract_from_xml
  8. fmt_node_as_vimhelp \
  9. para_as_map } recursive
  10. update_params_map /
  11. render_node
  12. This would be easier using lxml and XSLT, but:
  13. 1. This should avoid needing Python dependencies, especially ones that are
  14. C modules that have library dependencies (lxml requires libxml and
  15. libxslt).
  16. 2. I wouldn't know how to deal with nested indentation in <para> tags using
  17. XSLT.
  18. Each function :help block is formatted as follows:
  19. - Max width of 78 columns (`text_width`).
  20. - Indent with spaces (not tabs).
  21. - Indent of 16 columns for body text.
  22. - Function signature and helptag (right-aligned) on the same line.
  23. - Signature and helptag must have a minimum of 8 spaces between them.
  24. - If the signature is too long, it is placed on the line after the helptag.
  25. Signature wraps at `text_width - 8` characters with subsequent
  26. lines indented to the open parenthesis.
  27. - Subsection bodies are indented an additional 4 spaces.
  28. - Body consists of function description, parameters, return description, and
  29. C declaration (`INCLUDE_C_DECL`).
  30. - Parameters are omitted for the `void` and `Error *` types, or if the
  31. parameter is marked as [out].
  32. - Each function documentation is separated by a single line.
  33. """
  34. import argparse
  35. import os
  36. import re
  37. import sys
  38. import shutil
  39. import textwrap
  40. import subprocess
  41. import collections
  42. import msgpack
  43. import logging
  44. from xml.dom import minidom
  45. MIN_PYTHON_VERSION = (3, 5)
  46. if sys.version_info < MIN_PYTHON_VERSION:
  47. print("requires Python {}.{}+".format(*MIN_PYTHON_VERSION))
  48. sys.exit(1)
  49. # DEBUG = ('DEBUG' in os.environ)
  50. INCLUDE_C_DECL = ('INCLUDE_C_DECL' in os.environ)
  51. INCLUDE_DEPRECATED = ('INCLUDE_DEPRECATED' in os.environ)
  52. log = logging.getLogger(__name__)
  53. LOG_LEVELS = {
  54. logging.getLevelName(level): level for level in [
  55. logging.DEBUG, logging.INFO, logging.ERROR
  56. ]
  57. }
  58. fmt_vimhelp = False # HACK
  59. text_width = 78
  60. script_path = os.path.abspath(__file__)
  61. base_dir = os.path.dirname(os.path.dirname(script_path))
  62. out_dir = os.path.join(base_dir, 'tmp-{target}-doc')
  63. filter_cmd = '%s %s' % (sys.executable, script_path)
  64. seen_funcs = set()
  65. msgs = [] # Messages to show on exit.
  66. lua2dox_filter = os.path.join(base_dir, 'scripts', 'lua2dox_filter')
  67. CONFIG = {
  68. 'api': {
  69. 'mode': 'c',
  70. 'filename': 'api.txt',
  71. # String used to find the start of the generated part of the doc.
  72. 'section_start_token': '*api-global*',
  73. # Section ordering.
  74. 'section_order': [
  75. 'vim.c',
  76. 'vimscript.c',
  77. 'buffer.c',
  78. 'extmark.c',
  79. 'window.c',
  80. 'win_config.c',
  81. 'tabpage.c',
  82. 'ui.c',
  83. 'extmark.c',
  84. ],
  85. # List of files/directories for doxygen to read, separated by blanks
  86. 'files': os.path.join(base_dir, 'src/nvim/api'),
  87. # file patterns used by doxygen
  88. 'file_patterns': '*.h *.c',
  89. # Only function with this prefix are considered
  90. 'fn_name_prefix': 'nvim_',
  91. # Section name overrides.
  92. 'section_name': {
  93. 'vim.c': 'Global',
  94. },
  95. # For generated section names.
  96. 'section_fmt': lambda name: f'{name} Functions',
  97. # Section helptag.
  98. 'helptag_fmt': lambda name: f'*api-{name.lower()}*',
  99. # Per-function helptag.
  100. 'fn_helptag_fmt': lambda fstem, name: f'*{name}()*',
  101. # Module name overrides (for Lua).
  102. 'module_override': {},
  103. # Append the docs for these modules, do not start a new section.
  104. 'append_only': [],
  105. },
  106. 'lua': {
  107. 'mode': 'lua',
  108. 'filename': 'lua.txt',
  109. 'section_start_token': '*lua-vim*',
  110. 'section_order': [
  111. 'vim.lua',
  112. 'shared.lua',
  113. 'uri.lua',
  114. 'ui.lua',
  115. ],
  116. 'files': ' '.join([
  117. os.path.join(base_dir, 'src/nvim/lua/vim.lua'),
  118. os.path.join(base_dir, 'runtime/lua/vim/shared.lua'),
  119. os.path.join(base_dir, 'runtime/lua/vim/uri.lua'),
  120. os.path.join(base_dir, 'runtime/lua/vim/ui.lua'),
  121. ]),
  122. 'file_patterns': '*.lua',
  123. 'fn_name_prefix': '',
  124. 'section_name': {
  125. 'lsp.lua': 'core',
  126. },
  127. 'section_fmt': lambda name: f'Lua module: {name.lower()}',
  128. 'helptag_fmt': lambda name: f'*lua-{name.lower()}*',
  129. 'fn_helptag_fmt': lambda fstem, name: f'*{fstem}.{name}()*',
  130. 'module_override': {
  131. # `shared` functions are exposed on the `vim` module.
  132. 'shared': 'vim',
  133. 'uri': 'vim',
  134. 'ui': 'vim.ui',
  135. },
  136. 'append_only': [
  137. 'shared.lua',
  138. ],
  139. },
  140. 'lsp': {
  141. 'mode': 'lua',
  142. 'filename': 'lsp.txt',
  143. 'section_start_token': '*lsp-core*',
  144. 'section_order': [
  145. 'lsp.lua',
  146. 'buf.lua',
  147. 'diagnostic.lua',
  148. 'codelens.lua',
  149. 'tagfunc.lua',
  150. 'handlers.lua',
  151. 'util.lua',
  152. 'log.lua',
  153. 'rpc.lua',
  154. 'sync.lua',
  155. 'protocol.lua',
  156. ],
  157. 'files': ' '.join([
  158. os.path.join(base_dir, 'runtime/lua/vim/lsp'),
  159. os.path.join(base_dir, 'runtime/lua/vim/lsp.lua'),
  160. ]),
  161. 'file_patterns': '*.lua',
  162. 'fn_name_prefix': '',
  163. 'section_name': {'lsp.lua': 'lsp'},
  164. 'section_fmt': lambda name: (
  165. 'Lua module: vim.lsp'
  166. if name.lower() == 'lsp'
  167. else f'Lua module: vim.lsp.{name.lower()}'),
  168. 'helptag_fmt': lambda name: (
  169. '*lsp-core*'
  170. if name.lower() == 'lsp'
  171. else f'*lsp-{name.lower()}*'),
  172. 'fn_helptag_fmt': lambda fstem, name: (
  173. f'*vim.lsp.{name}()*'
  174. if fstem == 'lsp' and name != 'client'
  175. else (
  176. '*vim.lsp.client*'
  177. # HACK. TODO(justinmk): class/structure support in lua2dox
  178. if 'lsp.client' == f'{fstem}.{name}'
  179. else f'*vim.lsp.{fstem}.{name}()*')),
  180. 'module_override': {},
  181. 'append_only': [],
  182. },
  183. 'diagnostic': {
  184. 'mode': 'lua',
  185. 'filename': 'diagnostic.txt',
  186. 'section_start_token': '*diagnostic-api*',
  187. 'section_order': [
  188. 'diagnostic.lua',
  189. ],
  190. 'files': os.path.join(base_dir, 'runtime/lua/vim/diagnostic.lua'),
  191. 'file_patterns': '*.lua',
  192. 'fn_name_prefix': '',
  193. 'section_name': {'diagnostic.lua': 'diagnostic'},
  194. 'section_fmt': lambda _: 'Lua module: vim.diagnostic',
  195. 'helptag_fmt': lambda _: '*diagnostic-api*',
  196. 'fn_helptag_fmt': lambda fstem, name: f'*vim.{fstem}.{name}()*',
  197. 'module_override': {},
  198. 'append_only': [],
  199. },
  200. 'treesitter': {
  201. 'mode': 'lua',
  202. 'filename': 'treesitter.txt',
  203. 'section_start_token': '*lua-treesitter-core*',
  204. 'section_order': [
  205. 'treesitter.lua',
  206. 'language.lua',
  207. 'query.lua',
  208. 'highlighter.lua',
  209. 'languagetree.lua',
  210. ],
  211. 'files': ' '.join([
  212. os.path.join(base_dir, 'runtime/lua/vim/treesitter.lua'),
  213. os.path.join(base_dir, 'runtime/lua/vim/treesitter/'),
  214. ]),
  215. 'file_patterns': '*.lua',
  216. 'fn_name_prefix': '',
  217. 'section_name': {},
  218. 'section_fmt': lambda name: (
  219. 'Lua module: vim.treesitter'
  220. if name.lower() == 'treesitter'
  221. else f'Lua module: vim.treesitter.{name.lower()}'),
  222. 'helptag_fmt': lambda name: (
  223. '*lua-treesitter-core*'
  224. if name.lower() == 'treesitter'
  225. else f'*treesitter-{name.lower()}*'),
  226. 'fn_helptag_fmt': lambda fstem, name: (
  227. f'*{name}()*'
  228. if name != 'new'
  229. else f'*{fstem}.{name}()*'),
  230. # 'fn_helptag_fmt': lambda fstem, name: (
  231. # f'*vim.treesitter.{name}()*'
  232. # if fstem == 'treesitter'
  233. # else (
  234. # '*vim.lsp.client*'
  235. # # HACK. TODO(justinmk): class/structure support in lua2dox
  236. # if 'lsp.client' == f'{fstem}.{name}'
  237. # else f'*vim.lsp.{fstem}.{name}()*')),
  238. 'module_override': {},
  239. 'append_only': [],
  240. }
  241. }
  242. param_exclude = (
  243. 'channel_id',
  244. )
  245. # Annotations are displayed as line items after API function descriptions.
  246. annotation_map = {
  247. 'FUNC_API_FAST': '{fast}',
  248. 'FUNC_API_CHECK_TEXTLOCK': 'not allowed when |textlock| is active',
  249. }
  250. # Tracks `xrefsect` titles. As of this writing, used only for separating
  251. # deprecated functions.
  252. xrefs = set()
  253. # Raises an error with details about `o`, if `cond` is in object `o`,
  254. # or if `cond()` is callable and returns True.
  255. def debug_this(o, cond=True):
  256. name = ''
  257. if not isinstance(o, str):
  258. try:
  259. name = o.nodeName
  260. o = o.toprettyxml(indent=' ', newl='\n')
  261. except Exception:
  262. pass
  263. if ((callable(cond) and cond())
  264. or (not callable(cond) and cond)
  265. or (not callable(cond) and cond in o)):
  266. raise RuntimeError('xxx: {}\n{}'.format(name, o))
  267. # Appends a message to a list which will be printed on exit.
  268. def msg(s):
  269. msgs.append(s)
  270. # Print all collected messages.
  271. def msg_report():
  272. for m in msgs:
  273. print(f' {m}')
  274. # Print collected messages, then throw an exception.
  275. def fail(s):
  276. msg_report()
  277. raise RuntimeError(s)
  278. def find_first(parent, name):
  279. """Finds the first matching node within parent."""
  280. sub = parent.getElementsByTagName(name)
  281. if not sub:
  282. return None
  283. return sub[0]
  284. def iter_children(parent, name):
  285. """Yields matching child nodes within parent."""
  286. for child in parent.childNodes:
  287. if child.nodeType == child.ELEMENT_NODE and child.nodeName == name:
  288. yield child
  289. def get_child(parent, name):
  290. """Gets the first matching child node."""
  291. for child in iter_children(parent, name):
  292. return child
  293. return None
  294. def self_or_child(n):
  295. """Gets the first child node, or self."""
  296. if len(n.childNodes) == 0:
  297. return n
  298. return n.childNodes[0]
  299. def clean_text(text):
  300. """Cleans text.
  301. Only cleans superfluous whitespace at the moment.
  302. """
  303. return ' '.join(text.split()).strip()
  304. def clean_lines(text):
  305. """Removes superfluous lines.
  306. The beginning and end of the string is trimmed. Empty lines are collapsed.
  307. """
  308. return re.sub(r'\A\n\s*\n*|\n\s*\n*\Z', '', re.sub(r'(\n\s*\n+)+', '\n\n', text))
  309. def is_blank(text):
  310. return '' == clean_lines(text)
  311. def get_text(n, preformatted=False):
  312. """Recursively concatenates all text in a node tree."""
  313. text = ''
  314. if n.nodeType == n.TEXT_NODE:
  315. return n.data
  316. if n.nodeName == 'computeroutput':
  317. for node in n.childNodes:
  318. text += get_text(node)
  319. return '`{}` '.format(text)
  320. for node in n.childNodes:
  321. if node.nodeType == node.TEXT_NODE:
  322. text += node.data if preformatted else clean_text(node.data)
  323. elif node.nodeType == node.ELEMENT_NODE:
  324. text += ' ' + get_text(node, preformatted)
  325. return text
  326. # Gets the length of the last line in `text`, excluding newline ("\n") char.
  327. def len_lastline(text):
  328. lastnl = text.rfind('\n')
  329. if -1 == lastnl:
  330. return len(text)
  331. if '\n' == text[-1]:
  332. return lastnl - (1 + text.rfind('\n', 0, lastnl))
  333. return len(text) - (1 + lastnl)
  334. def len_lastline_withoutindent(text, indent):
  335. n = len_lastline(text)
  336. return (n - len(indent)) if n > len(indent) else 0
  337. # Returns True if node `n` contains only inline (not block-level) elements.
  338. def is_inline(n):
  339. # if len(n.childNodes) == 0:
  340. # return n.nodeType == n.TEXT_NODE or n.nodeName == 'computeroutput'
  341. for c in n.childNodes:
  342. if c.nodeType != c.TEXT_NODE and c.nodeName != 'computeroutput':
  343. return False
  344. if not is_inline(c):
  345. return False
  346. return True
  347. def doc_wrap(text, prefix='', width=70, func=False, indent=None):
  348. """Wraps text to `width`.
  349. First line is prefixed with `prefix`, subsequent lines are aligned.
  350. If `func` is True, only wrap at commas.
  351. """
  352. if not width:
  353. # return prefix + text
  354. return text
  355. # Whitespace used to indent all lines except the first line.
  356. indent = ' ' * len(prefix) if indent is None else indent
  357. indent_only = (prefix == '' and indent is not None)
  358. if func:
  359. lines = [prefix]
  360. for part in text.split(', '):
  361. if part[-1] not in ');':
  362. part += ', '
  363. if len(lines[-1]) + len(part) > width:
  364. lines.append(indent)
  365. lines[-1] += part
  366. return '\n'.join(x.rstrip() for x in lines).rstrip()
  367. # XXX: Dummy prefix to force TextWrapper() to wrap the first line.
  368. if indent_only:
  369. prefix = indent
  370. tw = textwrap.TextWrapper(break_long_words=False,
  371. break_on_hyphens=False,
  372. width=width,
  373. initial_indent=prefix,
  374. subsequent_indent=indent)
  375. result = '\n'.join(tw.wrap(text.strip()))
  376. # XXX: Remove the dummy prefix.
  377. if indent_only:
  378. result = result[len(indent):]
  379. return result
  380. def max_name(names):
  381. if len(names) == 0:
  382. return 0
  383. return max(len(name) for name in names)
  384. def update_params_map(parent, ret_map, width=62):
  385. """Updates `ret_map` with name:desc key-value pairs extracted
  386. from Doxygen XML node `parent`.
  387. """
  388. params = collections.OrderedDict()
  389. for node in parent.childNodes:
  390. if node.nodeType == node.TEXT_NODE:
  391. continue
  392. name_node = find_first(node, 'parametername')
  393. if name_node.getAttribute('direction') == 'out':
  394. continue
  395. name = get_text(name_node)
  396. if name in param_exclude:
  397. continue
  398. params[name.strip()] = node
  399. max_name_len = max_name(params.keys()) + 8
  400. # `ret_map` is a name:desc map.
  401. for name, node in params.items():
  402. desc = ''
  403. desc_node = get_child(node, 'parameterdescription')
  404. if desc_node:
  405. desc = fmt_node_as_vimhelp(
  406. desc_node, width=width, indent=(' ' * max_name_len))
  407. ret_map[name] = desc
  408. return ret_map
  409. def render_node(n, text, prefix='', indent='', width=62):
  410. """Renders a node as Vim help text, recursively traversing all descendants."""
  411. global fmt_vimhelp
  412. global has_seen_preformatted
  413. def ind(s):
  414. return s if fmt_vimhelp else ''
  415. text = ''
  416. # space_preceding = (len(text) > 0 and ' ' == text[-1][-1])
  417. # text += (int(not space_preceding) * ' ')
  418. if n.nodeName == 'preformatted':
  419. o = get_text(n, preformatted=True)
  420. ensure_nl = '' if o[-1] == '\n' else '\n'
  421. text += '>{}{}\n<'.format(ensure_nl, o)
  422. elif is_inline(n):
  423. text = doc_wrap(get_text(n), indent=indent, width=width)
  424. elif n.nodeName == 'verbatim':
  425. # TODO: currently we don't use this. The "[verbatim]" hint is there as
  426. # a reminder that we must decide how to format this if we do use it.
  427. text += ' [verbatim] {}'.format(get_text(n))
  428. elif n.nodeName == 'listitem':
  429. for c in n.childNodes:
  430. result = render_node(
  431. c,
  432. text,
  433. indent=indent + (' ' * len(prefix)),
  434. width=width
  435. )
  436. if is_blank(result):
  437. continue
  438. text += indent + prefix + result
  439. elif n.nodeName in ('para', 'heading'):
  440. for c in n.childNodes:
  441. if (is_inline(c)
  442. and '' != get_text(c).strip()
  443. and text
  444. and ' ' != text[-1]):
  445. text += ' '
  446. text += render_node(c, text, indent=indent, width=width)
  447. elif n.nodeName == 'itemizedlist':
  448. for c in n.childNodes:
  449. text += '{}\n'.format(render_node(c, text, prefix='• ',
  450. indent=indent, width=width))
  451. elif n.nodeName == 'orderedlist':
  452. i = 1
  453. for c in n.childNodes:
  454. if is_blank(get_text(c)):
  455. text += '\n'
  456. continue
  457. text += '{}\n'.format(render_node(c, text, prefix='{}. '.format(i),
  458. indent=indent, width=width))
  459. i = i + 1
  460. elif n.nodeName == 'simplesect' and 'note' == n.getAttribute('kind'):
  461. text += '\nNote:\n '
  462. for c in n.childNodes:
  463. text += render_node(c, text, indent=' ', width=width)
  464. text += '\n'
  465. elif n.nodeName == 'simplesect' and 'warning' == n.getAttribute('kind'):
  466. text += 'Warning:\n '
  467. for c in n.childNodes:
  468. text += render_node(c, text, indent=' ', width=width)
  469. text += '\n'
  470. elif (n.nodeName == 'simplesect'
  471. and n.getAttribute('kind') in ('return', 'see')):
  472. text += ind(' ')
  473. for c in n.childNodes:
  474. text += render_node(c, text, indent=' ', width=width)
  475. elif n.nodeName == 'computeroutput':
  476. return get_text(n)
  477. else:
  478. raise RuntimeError('unhandled node type: {}\n{}'.format(
  479. n.nodeName, n.toprettyxml(indent=' ', newl='\n')))
  480. return text
  481. def para_as_map(parent, indent='', width=62):
  482. """Extracts a Doxygen XML <para> node to a map.
  483. Keys:
  484. 'text': Text from this <para> element
  485. 'params': <parameterlist> map
  486. 'return': List of @return strings
  487. 'seealso': List of @see strings
  488. 'xrefs': ?
  489. """
  490. chunks = {
  491. 'text': '',
  492. 'params': collections.OrderedDict(),
  493. 'return': [],
  494. 'seealso': [],
  495. 'xrefs': []
  496. }
  497. # Ordered dict of ordered lists.
  498. groups = collections.OrderedDict([
  499. ('params', []),
  500. ('return', []),
  501. ('seealso', []),
  502. ('xrefs', []),
  503. ])
  504. # Gather nodes into groups. Mostly this is because we want "parameterlist"
  505. # nodes to appear together.
  506. text = ''
  507. kind = ''
  508. last = ''
  509. if is_inline(parent):
  510. # Flatten inline text from a tree of non-block nodes.
  511. text = doc_wrap(render_node(parent, ""), indent=indent, width=width)
  512. else:
  513. prev = None # Previous node
  514. for child in parent.childNodes:
  515. if child.nodeName == 'parameterlist':
  516. groups['params'].append(child)
  517. elif child.nodeName == 'xrefsect':
  518. groups['xrefs'].append(child)
  519. elif child.nodeName == 'simplesect':
  520. last = kind
  521. kind = child.getAttribute('kind')
  522. if kind == 'return' or (kind == 'note' and last == 'return'):
  523. groups['return'].append(child)
  524. elif kind == 'see':
  525. groups['seealso'].append(child)
  526. elif kind in ('note', 'warning'):
  527. text += render_node(child, text, indent=indent, width=width)
  528. else:
  529. raise RuntimeError('unhandled simplesect: {}\n{}'.format(
  530. child.nodeName, child.toprettyxml(indent=' ', newl='\n')))
  531. else:
  532. if (prev is not None
  533. and is_inline(self_or_child(prev))
  534. and is_inline(self_or_child(child))
  535. and '' != get_text(self_or_child(child)).strip()
  536. and text
  537. and ' ' != text[-1]):
  538. text += ' '
  539. text += render_node(child, text, indent=indent, width=width)
  540. prev = child
  541. chunks['text'] += text
  542. # Generate map from the gathered items.
  543. if len(groups['params']) > 0:
  544. for child in groups['params']:
  545. update_params_map(child, ret_map=chunks['params'], width=width)
  546. for child in groups['return']:
  547. chunks['return'].append(render_node(
  548. child, '', indent=indent, width=width))
  549. for child in groups['seealso']:
  550. chunks['seealso'].append(render_node(
  551. child, '', indent=indent, width=width))
  552. for child in groups['xrefs']:
  553. # XXX: Add a space (or any char) to `title` here, otherwise xrefs
  554. # ("Deprecated" section) acts very weird...
  555. title = get_text(get_child(child, 'xreftitle')) + ' '
  556. xrefs.add(title)
  557. xrefdesc = get_text(get_child(child, 'xrefdescription'))
  558. chunks['xrefs'].append(doc_wrap(xrefdesc, prefix='{}: '.format(title),
  559. width=width) + '\n')
  560. return chunks
  561. def fmt_node_as_vimhelp(parent, width=62, indent=''):
  562. """Renders (nested) Doxygen <para> nodes as Vim :help text.
  563. NB: Blank lines in a docstring manifest as <para> tags.
  564. """
  565. rendered_blocks = []
  566. def fmt_param_doc(m):
  567. """Renders a params map as Vim :help text."""
  568. max_name_len = max_name(m.keys()) + 4
  569. out = ''
  570. for name, desc in m.items():
  571. name = ' {}'.format('{{{}}}'.format(name).ljust(max_name_len))
  572. out += '{}{}\n'.format(name, desc)
  573. return out.rstrip()
  574. def has_nonexcluded_params(m):
  575. """Returns true if any of the given params has at least
  576. one non-excluded item."""
  577. if fmt_param_doc(m) != '':
  578. return True
  579. for child in parent.childNodes:
  580. para = para_as_map(child, indent, width)
  581. # Generate text from the gathered items.
  582. chunks = [para['text']]
  583. if len(para['params']) > 0 and has_nonexcluded_params(para['params']):
  584. chunks.append('\nParameters: ~')
  585. chunks.append(fmt_param_doc(para['params']))
  586. if len(para['return']) > 0:
  587. chunks.append('\nReturn: ~')
  588. for s in para['return']:
  589. chunks.append(s)
  590. if len(para['seealso']) > 0:
  591. chunks.append('\nSee also: ~')
  592. for s in para['seealso']:
  593. chunks.append(s)
  594. for s in para['xrefs']:
  595. chunks.append(s)
  596. rendered_blocks.append(clean_lines('\n'.join(chunks).strip()))
  597. rendered_blocks.append('')
  598. return clean_lines('\n'.join(rendered_blocks).strip())
  599. def extract_from_xml(filename, target, width):
  600. """Extracts Doxygen info as maps without formatting the text.
  601. Returns two maps:
  602. 1. Functions
  603. 2. Deprecated functions
  604. The `fmt_vimhelp` global controls some special cases for use by
  605. fmt_doxygen_xml_as_vimhelp(). (TODO: ugly :)
  606. """
  607. global xrefs
  608. global fmt_vimhelp
  609. xrefs.clear()
  610. fns = {} # Map of func_name:docstring.
  611. deprecated_fns = {} # Map of func_name:docstring.
  612. dom = minidom.parse(filename)
  613. compoundname = get_text(dom.getElementsByTagName('compoundname')[0])
  614. for member in dom.getElementsByTagName('memberdef'):
  615. if member.getAttribute('static') == 'yes' or \
  616. member.getAttribute('kind') != 'function' or \
  617. member.getAttribute('prot') == 'private' or \
  618. get_text(get_child(member, 'name')).startswith('_'):
  619. continue
  620. loc = find_first(member, 'location')
  621. if 'private' in loc.getAttribute('file'):
  622. continue
  623. return_type = get_text(get_child(member, 'type'))
  624. if return_type == '':
  625. continue
  626. if return_type.startswith(('ArrayOf', 'DictionaryOf')):
  627. parts = return_type.strip('_').split('_')
  628. return_type = '{}({})'.format(parts[0], ', '.join(parts[1:]))
  629. name = get_text(get_child(member, 'name'))
  630. annotations = get_text(get_child(member, 'argsstring'))
  631. if annotations and ')' in annotations:
  632. annotations = annotations.rsplit(')', 1)[-1].strip()
  633. # XXX: (doxygen 1.8.11) 'argsstring' only includes attributes of
  634. # non-void functions. Special-case void functions here.
  635. if name == 'nvim_get_mode' and len(annotations) == 0:
  636. annotations += 'FUNC_API_FAST'
  637. annotations = filter(None, map(lambda x: annotation_map.get(x),
  638. annotations.split()))
  639. params = []
  640. type_length = 0
  641. for param in iter_children(member, 'param'):
  642. param_type = get_text(get_child(param, 'type')).strip()
  643. param_name = ''
  644. declname = get_child(param, 'declname')
  645. if declname:
  646. param_name = get_text(declname).strip()
  647. elif CONFIG[target]['mode'] == 'lua':
  648. # XXX: this is what lua2dox gives us...
  649. param_name = param_type
  650. param_type = ''
  651. if param_name in param_exclude:
  652. continue
  653. if fmt_vimhelp and param_type.endswith('*'):
  654. param_type = param_type.strip('* ')
  655. param_name = '*' + param_name
  656. type_length = max(type_length, len(param_type))
  657. params.append((param_type, param_name))
  658. # Handle Object Oriented style functions here.
  659. # We make sure they have "self" in the parameters,
  660. # and a parent function
  661. if return_type.startswith('function') \
  662. and len(return_type.split(' ')) >= 2 \
  663. and any(x[1] == 'self' for x in params):
  664. split_return = return_type.split(' ')
  665. name = f'{split_return[1]}:{name}'
  666. c_args = []
  667. for param_type, param_name in params:
  668. c_args.append((' ' if fmt_vimhelp else '') + (
  669. '%s %s' % (param_type.ljust(type_length), param_name)).strip())
  670. if not fmt_vimhelp:
  671. pass
  672. else:
  673. fstem = '?'
  674. if '.' in compoundname:
  675. fstem = compoundname.split('.')[0]
  676. fstem = CONFIG[target]['module_override'].get(fstem, fstem)
  677. vimtag = CONFIG[target]['fn_helptag_fmt'](fstem, name)
  678. prefix = '%s(' % name
  679. suffix = '%s)' % ', '.join('{%s}' % a[1] for a in params
  680. if a[0] not in ('void', 'Error'))
  681. if not fmt_vimhelp:
  682. c_decl = '%s %s(%s);' % (return_type, name, ', '.join(c_args))
  683. signature = prefix + suffix
  684. else:
  685. c_decl = textwrap.indent('%s %s(\n%s\n);' % (return_type, name,
  686. ',\n'.join(c_args)),
  687. ' ')
  688. # Minimum 8 chars between signature and vimtag
  689. lhs = (width - 8) - len(vimtag)
  690. if len(prefix) + len(suffix) > lhs:
  691. signature = vimtag.rjust(width) + '\n'
  692. signature += doc_wrap(suffix, width=width, prefix=prefix,
  693. func=True)
  694. else:
  695. signature = prefix + suffix
  696. signature += vimtag.rjust(width - len(signature))
  697. paras = []
  698. brief_desc = find_first(member, 'briefdescription')
  699. if brief_desc:
  700. for child in brief_desc.childNodes:
  701. paras.append(para_as_map(child))
  702. desc = find_first(member, 'detaileddescription')
  703. if desc:
  704. for child in desc.childNodes:
  705. paras.append(para_as_map(child))
  706. log.debug(
  707. textwrap.indent(
  708. re.sub(r'\n\s*\n+', '\n',
  709. desc.toprettyxml(indent=' ', newl='\n')), ' ' * 16))
  710. fn = {
  711. 'annotations': list(annotations),
  712. 'signature': signature,
  713. 'parameters': params,
  714. 'parameters_doc': collections.OrderedDict(),
  715. 'doc': [],
  716. 'return': [],
  717. 'seealso': [],
  718. }
  719. if fmt_vimhelp:
  720. fn['desc_node'] = desc # HACK :(
  721. for m in paras:
  722. if 'text' in m:
  723. if not m['text'] == '':
  724. fn['doc'].append(m['text'])
  725. if 'params' in m:
  726. # Merge OrderedDicts.
  727. fn['parameters_doc'].update(m['params'])
  728. if 'return' in m and len(m['return']) > 0:
  729. fn['return'] += m['return']
  730. if 'seealso' in m and len(m['seealso']) > 0:
  731. fn['seealso'] += m['seealso']
  732. if INCLUDE_C_DECL:
  733. fn['c_decl'] = c_decl
  734. if 'Deprecated' in str(xrefs):
  735. deprecated_fns[name] = fn
  736. elif name.startswith(CONFIG[target]['fn_name_prefix']):
  737. fns[name] = fn
  738. xrefs.clear()
  739. fns = collections.OrderedDict(sorted(
  740. fns.items(),
  741. key=lambda key_item_tuple: key_item_tuple[0].lower()))
  742. deprecated_fns = collections.OrderedDict(sorted(deprecated_fns.items()))
  743. return (fns, deprecated_fns)
  744. def fmt_doxygen_xml_as_vimhelp(filename, target):
  745. """Entrypoint for generating Vim :help from from Doxygen XML.
  746. Returns 3 items:
  747. 1. Vim help text for functions found in `filename`.
  748. 2. Vim help text for deprecated functions.
  749. """
  750. global fmt_vimhelp
  751. fmt_vimhelp = True
  752. fns_txt = {} # Map of func_name:vim-help-text.
  753. deprecated_fns_txt = {} # Map of func_name:vim-help-text.
  754. fns, _ = extract_from_xml(filename, target, width=text_width)
  755. for name, fn in fns.items():
  756. # Generate Vim :help for parameters.
  757. if fn['desc_node']:
  758. doc = fmt_node_as_vimhelp(fn['desc_node'])
  759. if not doc:
  760. doc = 'TODO: Documentation'
  761. annotations = '\n'.join(fn['annotations'])
  762. if annotations:
  763. annotations = ('\n\nAttributes: ~\n' +
  764. textwrap.indent(annotations, ' '))
  765. i = doc.rfind('Parameters: ~')
  766. if i == -1:
  767. doc += annotations
  768. else:
  769. doc = doc[:i] + annotations + '\n\n' + doc[i:]
  770. if INCLUDE_C_DECL:
  771. doc += '\n\nC Declaration: ~\n>\n'
  772. doc += fn['c_decl']
  773. doc += '\n<'
  774. func_doc = fn['signature'] + '\n'
  775. func_doc += textwrap.indent(clean_lines(doc), ' ' * 16)
  776. # Verbatim handling.
  777. func_doc = re.sub(r'^\s+([<>])$', r'\1', func_doc, flags=re.M)
  778. split_lines = func_doc.split('\n')
  779. start = 0
  780. while True:
  781. try:
  782. start = split_lines.index('>', start)
  783. except ValueError:
  784. break
  785. try:
  786. end = split_lines.index('<', start)
  787. except ValueError:
  788. break
  789. split_lines[start + 1:end] = [
  790. (' ' + x).rstrip()
  791. for x in textwrap.dedent(
  792. "\n".join(
  793. split_lines[start+1:end]
  794. )
  795. ).split("\n")
  796. ]
  797. start = end
  798. func_doc = "\n".join(split_lines)
  799. if 'Deprecated' in xrefs:
  800. deprecated_fns_txt[name] = func_doc
  801. elif name.startswith(CONFIG[target]['fn_name_prefix']):
  802. fns_txt[name] = func_doc
  803. xrefs.clear()
  804. fmt_vimhelp = False
  805. return ('\n\n'.join(list(fns_txt.values())),
  806. '\n\n'.join(list(deprecated_fns_txt.values())))
  807. def delete_lines_below(filename, tokenstr):
  808. """Deletes all lines below the line containing `tokenstr`, the line itself,
  809. and one line above it.
  810. """
  811. lines = open(filename).readlines()
  812. i = 0
  813. found = False
  814. for i, line in enumerate(lines, 1):
  815. if tokenstr in line:
  816. found = True
  817. break
  818. if not found:
  819. raise RuntimeError(f'not found: "{tokenstr}"')
  820. i = max(0, i - 2)
  821. with open(filename, 'wt') as fp:
  822. fp.writelines(lines[0:i])
  823. def main(config, args):
  824. """Generates:
  825. 1. Vim :help docs
  826. 2. *.mpack files for use by API clients
  827. Doxygen is called and configured through stdin.
  828. """
  829. for target in CONFIG:
  830. if args.target is not None and target != args.target:
  831. continue
  832. mpack_file = os.path.join(
  833. base_dir, 'runtime', 'doc',
  834. CONFIG[target]['filename'].replace('.txt', '.mpack'))
  835. if os.path.exists(mpack_file):
  836. os.remove(mpack_file)
  837. output_dir = out_dir.format(target=target)
  838. log.info("Generating documentation for %s in folder %s",
  839. target, output_dir)
  840. debug = args.log_level >= logging.DEBUG
  841. p = subprocess.Popen(
  842. ['doxygen', '-'],
  843. stdin=subprocess.PIPE,
  844. # silence warnings
  845. # runtime/lua/vim/lsp.lua:209: warning: argument 'foo' not found
  846. stderr=(subprocess.STDOUT if debug else subprocess.DEVNULL))
  847. p.communicate(
  848. config.format(
  849. input=CONFIG[target]['files'],
  850. output=output_dir,
  851. filter=filter_cmd,
  852. file_patterns=CONFIG[target]['file_patterns'])
  853. .encode('utf8')
  854. )
  855. if p.returncode:
  856. sys.exit(p.returncode)
  857. fn_map_full = {} # Collects all functions as each module is processed.
  858. sections = {}
  859. intros = {}
  860. sep = '=' * text_width
  861. base = os.path.join(output_dir, 'xml')
  862. dom = minidom.parse(os.path.join(base, 'index.xml'))
  863. # generate docs for section intros
  864. for compound in dom.getElementsByTagName('compound'):
  865. if compound.getAttribute('kind') != 'group':
  866. continue
  867. groupname = get_text(find_first(compound, 'name'))
  868. groupxml = os.path.join(base, '%s.xml' %
  869. compound.getAttribute('refid'))
  870. group_parsed = minidom.parse(groupxml)
  871. doc_list = []
  872. brief_desc = find_first(group_parsed, 'briefdescription')
  873. if brief_desc:
  874. for child in brief_desc.childNodes:
  875. doc_list.append(fmt_node_as_vimhelp(child))
  876. desc = find_first(group_parsed, 'detaileddescription')
  877. if desc:
  878. doc = fmt_node_as_vimhelp(desc)
  879. if doc:
  880. doc_list.append(doc)
  881. intros[groupname] = "\n".join(doc_list)
  882. for compound in dom.getElementsByTagName('compound'):
  883. if compound.getAttribute('kind') != 'file':
  884. continue
  885. filename = get_text(find_first(compound, 'name'))
  886. if filename.endswith('.c') or filename.endswith('.lua'):
  887. xmlfile = os.path.join(base,
  888. '{}.xml'.format(compound.getAttribute('refid')))
  889. # Extract unformatted (*.mpack).
  890. fn_map, _ = extract_from_xml(xmlfile, target, width=9999)
  891. # Extract formatted (:help).
  892. functions_text, deprecated_text = fmt_doxygen_xml_as_vimhelp(
  893. os.path.join(base, '{}.xml'.format(
  894. compound.getAttribute('refid'))), target)
  895. if not functions_text and not deprecated_text:
  896. continue
  897. else:
  898. name = os.path.splitext(
  899. os.path.basename(filename))[0].lower()
  900. sectname = name.upper() if name == 'ui' else name.title()
  901. doc = ''
  902. intro = intros.get(f'api-{name}')
  903. if intro:
  904. doc += '\n\n' + intro
  905. if functions_text:
  906. doc += '\n\n' + functions_text
  907. if INCLUDE_DEPRECATED and deprecated_text:
  908. doc += f'\n\n\nDeprecated {sectname} Functions: ~\n\n'
  909. doc += deprecated_text
  910. if doc:
  911. filename = os.path.basename(filename)
  912. sectname = CONFIG[target]['section_name'].get(
  913. filename, sectname)
  914. title = CONFIG[target]['section_fmt'](sectname)
  915. helptag = CONFIG[target]['helptag_fmt'](sectname)
  916. sections[filename] = (title, helptag, doc)
  917. fn_map_full.update(fn_map)
  918. if len(sections) == 0:
  919. fail(f'no sections for target: {target}')
  920. if len(sections) > len(CONFIG[target]['section_order']):
  921. raise RuntimeError(
  922. 'found new modules "{}"; update the "section_order" map'.format(
  923. set(sections).difference(CONFIG[target]['section_order'])))
  924. docs = ''
  925. i = 0
  926. for filename in CONFIG[target]['section_order']:
  927. try:
  928. title, helptag, section_doc = sections.pop(filename)
  929. except KeyError:
  930. msg(f'warning: empty docs, skipping (target={target}): {filename}')
  931. msg(f' existing docs: {sections.keys()}')
  932. continue
  933. i += 1
  934. if filename not in CONFIG[target]['append_only']:
  935. docs += sep
  936. docs += '\n%s%s' % (title,
  937. helptag.rjust(text_width - len(title)))
  938. docs += section_doc
  939. docs += '\n\n\n'
  940. docs = docs.rstrip() + '\n\n'
  941. docs += ' vim:tw=78:ts=8:ft=help:norl:\n'
  942. doc_file = os.path.join(base_dir, 'runtime', 'doc',
  943. CONFIG[target]['filename'])
  944. delete_lines_below(doc_file, CONFIG[target]['section_start_token'])
  945. with open(doc_file, 'ab') as fp:
  946. fp.write(docs.encode('utf8'))
  947. fn_map_full = collections.OrderedDict(sorted(fn_map_full.items()))
  948. with open(mpack_file, 'wb') as fp:
  949. fp.write(msgpack.packb(fn_map_full, use_bin_type=True))
  950. if not args.keep_tmpfiles:
  951. shutil.rmtree(output_dir)
  952. msg_report()
  953. def filter_source(filename):
  954. name, extension = os.path.splitext(filename)
  955. if extension == '.lua':
  956. p = subprocess.run([lua2dox_filter, filename], stdout=subprocess.PIPE)
  957. op = ('?' if 0 != p.returncode else p.stdout.decode('utf-8'))
  958. print(op)
  959. else:
  960. """Filters the source to fix macros that confuse Doxygen."""
  961. with open(filename, 'rt') as fp:
  962. print(re.sub(r'^(ArrayOf|DictionaryOf)(\(.*?\))',
  963. lambda m: m.group(1)+'_'.join(
  964. re.split(r'[^\w]+', m.group(2))),
  965. fp.read(), flags=re.M))
  966. def parse_args():
  967. targets = ', '.join(CONFIG.keys())
  968. ap = argparse.ArgumentParser(
  969. description="Generate helpdoc from source code")
  970. ap.add_argument(
  971. "--log-level", "-l", choices=LOG_LEVELS.keys(),
  972. default=logging.getLevelName(logging.ERROR), help="Set log verbosity"
  973. )
  974. ap.add_argument('source_filter', nargs='*',
  975. help="Filter source file(s)")
  976. ap.add_argument('-k', '--keep-tmpfiles', action='store_true',
  977. help="Keep temporary files")
  978. ap.add_argument('-t', '--target',
  979. help=f'One of ({targets}), defaults to "all"')
  980. return ap.parse_args()
  981. Doxyfile = textwrap.dedent('''
  982. OUTPUT_DIRECTORY = {output}
  983. INPUT = {input}
  984. INPUT_ENCODING = UTF-8
  985. FILE_PATTERNS = {file_patterns}
  986. RECURSIVE = YES
  987. INPUT_FILTER = "{filter}"
  988. EXCLUDE =
  989. EXCLUDE_SYMLINKS = NO
  990. EXCLUDE_PATTERNS = */private/* */health.lua */_*.lua
  991. EXCLUDE_SYMBOLS =
  992. EXTENSION_MAPPING = lua=C
  993. EXTRACT_PRIVATE = NO
  994. GENERATE_HTML = NO
  995. GENERATE_DOCSET = NO
  996. GENERATE_HTMLHELP = NO
  997. GENERATE_QHP = NO
  998. GENERATE_TREEVIEW = NO
  999. GENERATE_LATEX = NO
  1000. GENERATE_RTF = NO
  1001. GENERATE_MAN = NO
  1002. GENERATE_DOCBOOK = NO
  1003. GENERATE_AUTOGEN_DEF = NO
  1004. GENERATE_XML = YES
  1005. XML_OUTPUT = xml
  1006. XML_PROGRAMLISTING = NO
  1007. ENABLE_PREPROCESSING = YES
  1008. MACRO_EXPANSION = YES
  1009. EXPAND_ONLY_PREDEF = NO
  1010. MARKDOWN_SUPPORT = YES
  1011. ''')
  1012. if __name__ == "__main__":
  1013. args = parse_args()
  1014. print("Setting log level to %s" % args.log_level)
  1015. args.log_level = LOG_LEVELS[args.log_level]
  1016. log.setLevel(args.log_level)
  1017. log.addHandler(logging.StreamHandler())
  1018. if len(args.source_filter) > 0:
  1019. filter_source(args.source_filter[0])
  1020. else:
  1021. main(Doxyfile, args)
  1022. # vim: set ft=python ts=4 sw=4 tw=79 et :