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 @@ -314,6 +314,9 @@ will generate the following usage line:

:[range]SpawnZerglings[!] {hatchery} [larva]

Vimdoc warns if documented arguments are declared for a command
that accepts none (`-nargs=0`, including the default when omitted).

You can override the usage line with the @usage command, which takes a list of
arguments. Any arguments that look like vim variable names (\I\i+) will be
assumed to be parameters, and their required-ness will be inferred from the
Expand Down
33 changes: 33 additions & 0 deletions tests/test_command_argument_warnings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import unittest
import warnings

from vimdoc import error
from vimdoc import parser


class TestCommandArgumentWarnings(unittest.TestCase):

def parse(self, definition, documentation):
source = ['""'] + ['" ' + line for line in documentation] + [definition]
with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter('always', error.ArgumentMismatch)
list(parser.ParseBlocks(source, 'plugin/example.vim'))
return [item for item in caught if issubclass(item.category, error.ArgumentMismatch)]

def test_required_argument_for_zero_argument_command_warns(self):
caught = self.parse('command! -nargs=0 Example echo 1', ['Takes {value}.'])
self.assertEqual(1, len(caught))

def test_optional_argument_for_implicit_zero_argument_command_warns(self):
caught = self.parse('command! Example echo 1', ['@usage [value]'])
self.assertEqual(1, len(caught))

def test_argument_accepting_commands_do_not_warn(self):
for nargs in ['1', '?', '*', '+']:
with self.subTest(nargs=nargs):
self.assertEqual([], self.parse('command! -nargs=' + nargs + ' Example echo 1',
['Takes {value}.']))

def test_builtin_command_modifiers_are_not_arguments(self):
self.assertEqual([], self.parse('command! -range -bang -count Example echo 1',
['Runs the command.']))
7 changes: 7 additions & 0 deletions vimdoc/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,13 @@ def Close(self):
from vimdoc.docline import Usage
self.header = Usage('{]')
self.locals['usage'] = self.header.GenerateUsage(self)
if (self.locals.get('type') == vimdoc.COMMAND
and self.locals.get('nargs') == '0'
and (regex.required_arg.search(self.locals['usage'])
or regex.optional_arg.search(self.locals['usage']))):
warnings.warn('Documentation declares arguments for command {} '
'which accepts no arguments.'.format(self.FullName()),
error.ArgumentMismatch)
if 'private' in self.locals and self.locals.get('type') != vimdoc.FUNCTION:
raise error.InvalidBlock('Only functions may be marked as private.')
return self
Expand Down
2 changes: 1 addition & 1 deletion vimdoc/codeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def Update(self, block):
head += '<>'
if self.flags.get('bang'):
head += '[!]'
block.Local(head=head)
block.Local(head=head, nargs=self.flags.get('nargs', '0'))


class Setting(Definition):
Expand Down
2 changes: 2 additions & 0 deletions vimdoc/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ def ParseCodeLine(line):
'buffer': '-buffer' in args,
'bar': '-bar' in args,
}
flags['nargs'] = next((flag.split('=', 1)[1] for flag in args.split()
if flag.startswith('-nargs=')), '0')
return codeline.Command(name, **flags)
smatch = regex.setting_line.match(line)
if smatch:
Expand Down
Loading