gen_vimdoc.py 40 KB

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