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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ Available block directives include:
- `@setting name` declares a setting (global or per-buffer configuration
variable).
- `@default arg=value` describes the default value of an optional arg.
- `@tag name` renders an arbitrary help tag at that position in the block.
The name is used literally, without a plugin-name prefix.
- `@throws exception` describes the type of exceptions that a function or
command may throw.
- `@deprecated reason` marks a command or function as deprecated and excludes it
Expand All @@ -173,6 +175,7 @@ enclosed in parenthesis.
- `@flag(name)` generates a link to a flag defined in the plugin.
- `@setting(name)` generates a link to a setting defined in the plugin.
- `@section(id)` generates a link to a section defined in the plugin.
- `@tag(name)` renders an arbitrary help tag inline in prose.
- `@dict(name)` generates a link to a dictionary defined in the plugin.
- `@plugin(attr)` Outputs some plugin data, such as the name or author. `attr`
must be one of `stylized`, `name`, or `author`. If the attr (and parenthesis)
Expand Down
39 changes: 39 additions & 0 deletions tests/test_arbitrary_tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import io
import unittest

from vimdoc import module
from vimdoc import output
from vimdoc import parser


class TestArbitraryTags(unittest.TestCase):

def writer(self):
writer = output.Helpfile(module.Module('example', module.VimPlugin('example')), '.')
writer.file = io.StringIO()
return writer

def test_tags_are_rendered_at_their_documented_position(self):
source = ['""', '" @section Introduction', '" Before the tags.',
'" @tag plugin[]', '" @tag plugin[commands]', '" After the tags.']
block, = parser.ParseBlocks(source, 'plugin/example.vim')
block.Local(namespace=None)
writer = self.writer()
writer.WriteParagraphs(block)
text = writer.file.getvalue()
self.assertLess(text.index('Before the tags.'), text.index('*plugin[]*'))
self.assertLess(text.index('*plugin[]*'), text.index('*plugin[commands]*'))
self.assertLess(text.index('*plugin[commands]*'), text.index('After the tags.'))

def test_inline_tag_is_not_prefixed_with_plugin_name(self):
self.assertEqual('Before *plugin[]* after',
self.writer().Expand('Before @tag(plugin[]) after', None))

def test_long_tag_is_not_wrapped_or_rejected(self):
tag = 'long-tag-' * 15
block, = parser.ParseBlocks(['""', '" @section Introduction', '" @tag ' + tag],
'plugin/example.vim')
block.Local(namespace=None)
writer = self.writer()
writer.WriteParagraphs(block)
self.assertIn('*' + tag + '*\n', writer.file.getvalue())
4 changes: 4 additions & 0 deletions vimdoc/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ def Default(self, arg, value):
self._optional_args.append(arg)
self.paragraphs.SetType(paragraph.DefaultLine, arg, value)

def AddTag(self, tag):
"""Adds an arbitrary help tag at the current position in the block."""
self.paragraphs.SetType(paragraph.TagLine, tag)

def Except(self, typ, description):
"""Adds a line specifying that the code can throw a specific exception."""
description = description or ''
Expand Down
11 changes: 11 additions & 0 deletions vimdoc/docline.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,16 @@ def Update(self, block):
block.Except(self.error, self.description)


class Tag(BlockDirective):
REGEX = regex.stylizing_args

def Assign(self, tag):
self.tag = tag

def Update(self, block):
block.AddTag(self.tag)


class Header(BlockDirective):
"""A header directive, like @usage @function or @command."""

Expand Down Expand Up @@ -407,6 +417,7 @@ def GenerateUsage(self, block):
'stylized': Stylized,
'subsection': SubSection,
'tagline': Tagline,
'tag': Tag,
'throws': Throws,
'usage': Usage,
}
6 changes: 5 additions & 1 deletion vimdoc/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ def WriteParagraph(self, p, namespace, indent=0):
elif isinstance(p, paragraph.DefaultLine):
self.WriteLine(self.Default(
p.arg, p.value, namespace), indent=indent)
elif isinstance(p, paragraph.TagLine):
self.Print(self.Tag(p.tag).rjust(self.WIDTH), wide=True)
elif isinstance(p, paragraph.ExceptionLine):
self.WriteLine(self.Throws(
p.exception, p.description, namespace), indent=indent)
Expand Down Expand Up @@ -256,7 +258,9 @@ def Expander(match):

def ExpandInline(self, inline, element, namespace):
"""Expands inline directives, like @function()."""
if inline == 'section':
if inline == 'tag':
return None if element is None else self.Tag(element)
elif inline == 'section':
return self.Link(self.Slug(element))
elif inline == 'function':
# If a user says @function(#Foo) then that points to in#this#file#Foo.
Expand Down
7 changes: 7 additions & 0 deletions vimdoc/paragraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ def __init__(self, arg, value):
self.value = value


class TagLine(Paragraph):
def __init__(self, tag):
super(TagLine, self).__init__()
self.open = False
self.tag = tag


class ListItem(TextParagraph):
def __init__(self, leader='*', level=0):
super(ListItem, self).__init__()
Expand Down
Loading