Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,16 @@ Available block directives include:
The global directives (@stylized, @order, and @library) are all detected from
inside a @section block (usually the Introduction section).

Long `@throws`, `@order`, and `@tagline` directives can continue on comment
lines with four spaces of indentation after the usual comment leader:

```viml
" @throws Ambiguous if {name} refers to multiple sources within one
" namespace.
```

An unindented comment starts a separate documentation line.

### Inline Directives

Inline directives occur in the body of comment blocks. Most take one argument
Expand Down
55 changes: 55 additions & 0 deletions tests/test_directive_continuations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import unittest

from vimdoc import paragraph
from vimdoc import parser
from vimdoc import docline


class TestDirectiveContinuations(unittest.TestCase):

def test_throws_description_continues_with_four_spaces(self):
source = ['""', '" @throws Ambiguous if {name} refers to multiple sources within one',
'" namespace.', 'function! example#Find(name)']
block, = parser.ParseBlocks(source, 'autoload/example.vim')
description, = [p.description for p in block.paragraphs
if isinstance(p, paragraph.ExceptionLine)]
self.assertEqual('if {name} refers to multiple sources within one namespace.',
description)

def test_order_continues_across_multiple_lines(self):
parsed = list(parser.EnumerateParsedLines([
'""', '" @order intro', '" commands', '" about']))
self.assertEqual(['intro', 'commands', 'about'], parsed[0][1].order)
self.assertEqual(1, parsed[0][0])

def test_unindented_description_is_a_separate_paragraph(self):
source = ['""', '" @throws Example first.', '" Separate paragraph.',
'function! example#Find()']
block, = parser.ParseBlocks(source, 'autoload/example.vim')
self.assertEqual('first.', block.paragraphs[0].description)
self.assertEqual('Separate paragraph.', block.paragraphs[1].text)

def test_tagline_joins_as_one_value(self):
parsed = list(parser.EnumerateParsedLines(
['""', '" @tagline First part', '" second part']))
self.assertEqual('First part second part', parsed[0][1].tagline)

def test_usage_descriptions_are_not_joined_to_the_header(self):
parsed = list(parser.EnumerateParsedLines(
['""', '" @usage {name}', '" Indented description.']))
self.assertEqual(2, len(parsed))
self.assertEqual(' Indented description.', parsed[1][1].line)

def test_indented_block_directives_remain_separate(self):
cases = [
('@tagline First', '@section Introduction', docline.Section),
('@throws Example first', '@default value=0', docline.Default),
('@order intro', '@section Introduction', docline.Section),
]
for first, second, expected_type in cases:
with self.subTest(first=first, second=second):
parsed = list(parser.EnumerateParsedLines(
['""', '" ' + first, '" ' + second]))
self.assertEqual(2, len(parsed))
self.assertIsInstance(parsed[1][1], expected_type)
self.assertEqual(2, parsed[1][0])
20 changes: 19 additions & 1 deletion vimdoc/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,27 @@ def EnumerateStripNewlinesAndJoinContinuations(lines):
yield lineno, cached


def EnumerateJoinedDirectiveLines(lines):
"""Joins four-space continuations of free-form block directives."""
pending = None
for lineno, line in EnumerateStripNewlinesAndJoinContinuations(lines):
directive = regex.block_directive.match(pending[1]) if pending else None
body = regex.comment_leader.sub('', line) if IsComment(line) else None
if (directive and directive.group(1) in ('throws', 'tagline', 'order')
and body and body.startswith(' ') and body.strip()
and not regex.block_directive.match(line)):
pending = (pending[0], pending[1] + ' ' + body.lstrip())
else:
if pending is not None:
yield pending
pending = (lineno, line)
if pending is not None:
yield pending


def EnumerateParsedLines(lines):
vimdoc_mode = False
for i, line in EnumerateStripNewlinesAndJoinContinuations(lines):
for i, line in EnumerateJoinedDirectiveLines(lines):
if not vimdoc_mode:
if regex.vimdoc_leader.match(line):
vimdoc_mode = True
Expand Down
Loading