Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Use function name for decorators #335

Closed
wants to merge 4 commits into from
Closed
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
*.egg-info/
__pycache__
.pytest_cache/
neovim/ui/screen.c
neovim/ui/screen.so
build/
43 changes: 33 additions & 10 deletions neovim/plugin/decorators.py
Original file line number Diff line number Diff line change
@@ -42,11 +42,16 @@ def dec(f):
return dec


def command(name, nargs=0, complete=None, range=None, count=None, bang=False,
register=False, sync=False, allow_nested=False, eval=None):
def command(name=None, nargs=0, complete=None, range=None,
count=None, bang=False, register=False, sync=False,
allow_nested=False, eval=None):
"""Tag a function or plugin method as a Nvim command handler."""
def dec(f):
f._nvim_rpc_method_name = 'command:{}'.format(name)
if not name:
command_name = capitalize_name(f.__name__)
else:
command_name = name
f._nvim_rpc_method_name = 'command:{}'.format(command_name)
f._nvim_rpc_sync = sync
f._nvim_bind = True
f._nvim_prefix_plugin_path = True
@@ -80,18 +85,22 @@ def dec(f):

f._nvim_rpc_spec = {
'type': 'command',
'name': name,
'name': command_name,
'sync': rpc_sync,
'opts': opts
}
return f
return dec


def autocmd(name, pattern='*', sync=False, allow_nested=False, eval=None):
def autocmd(name=None, pattern='*', sync=False, allow_nested=False, eval=None):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be a concern that a plugin potentially can have multiple implementations of the autocmd with disjoint patterns. At least there should be a warning in the docs. (python doesn't give a warning on method shadowing, though we could hack it with a metaclass).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure about the autocmd, it doesn't have much sense here I guess.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the parameter should be named event, that's why I was confused here.

"""Tag a function or plugin method as a Nvim autocommand handler."""
def dec(f):
f._nvim_rpc_method_name = 'autocmd:{}:{}'.format(name, pattern)
if not name:
autocmd_name = capitalize_name(f.__name__)
else:
autocmd_name = name
f._nvim_rpc_method_name = 'autocmd:{}:{}'.format(autocmd_name, pattern)
f._nvim_rpc_sync = sync
f._nvim_bind = True
f._nvim_prefix_plugin_path = True
@@ -110,18 +119,23 @@ def dec(f):

f._nvim_rpc_spec = {
'type': 'autocmd',
'name': name,
'name': autocmd_name,
'sync': rpc_sync,
'opts': opts
}
return f
return dec


def function(name, range=False, sync=False, allow_nested=False, eval=None):
def function(name=None, range=False, sync=False,
allow_nested=False, eval=None):
"""Tag a function or plugin method as a Nvim function handler."""
def dec(f):
f._nvim_rpc_method_name = 'function:{}'.format(name)
if not name:
function_name = capitalize_name(f.__name__)
else:
function_name = name
f._nvim_rpc_method_name = 'function:{}'.format(function_name)
f._nvim_rpc_sync = sync
f._nvim_bind = True
f._nvim_prefix_plugin_path = True
@@ -141,14 +155,23 @@ def dec(f):

f._nvim_rpc_spec = {
'type': 'function',
'name': name,
'name': function_name,
'sync': rpc_sync,
'opts': opts
}
return f
return dec


def capitalize_name(name):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not like this kind of magic. For plugins that use this, it would be better to break python conventions and name the implementation function literally what the vimL side name is. Also, _ is not forbidden in vimL.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, a matter of style I think. I would no like to have a function/command named My_function_something, and you can still pass a name if people want that name.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Referential transparency is more important than style. If an rplugin defines MySpecialCommand to users, I would expect to find that name literally in the source, much more often than not.

words = [
word[0].upper() + word[1:]
for word in name.split('_')
if word
]
return ''.join(words)


def shutdown_hook(f):
"""Tag a function or method as a shutdown hook."""
f._nvim_shutdown_hook = True
80 changes: 79 additions & 1 deletion test/test_decorators.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from neovim.plugin.decorators import command
from neovim.plugin.decorators import autocmd, command, function


def test_command_count():
@@ -26,3 +26,81 @@ def function():
decorated = command('test', count=count_value)(function)
assert 'count' in decorated._nvim_rpc_spec['opts']
assert decorated._nvim_rpc_spec['opts']['count'] == count_value


def test_name_command():
def newcommand():
return

def newCommand():
return

def new_command():
return

def _new_command():
return

decorated = command()(newcommand)
assert decorated._nvim_rpc_spec['name'] == 'Newcommand'

decorated = command()(newCommand)
assert decorated._nvim_rpc_spec['name'] == 'NewCommand'

decorated = command()(new_command)
assert decorated._nvim_rpc_spec['name'] == 'NewCommand'

decorated = command()(_new_command)
assert decorated._nvim_rpc_spec['name'] == 'NewCommand'


def test_name_function():
def newfunction():
return

def newFunction():
return

def new_function():
return

def _new_function():
return

decorated = function()(newfunction)
assert decorated._nvim_rpc_spec['name'] == 'Newfunction'

decorated = function()(newFunction)
assert decorated._nvim_rpc_spec['name'] == 'NewFunction'

decorated = function()(new_function)
assert decorated._nvim_rpc_spec['name'] == 'NewFunction'

decorated = function()(_new_function)
assert decorated._nvim_rpc_spec['name'] == 'NewFunction'


def test_name_autocmd():
def newautocmd():
return

def newAutocmd():
return

def new_autocmd():
return

def _new_autocmd():
return

decorated = autocmd()(newautocmd)
assert decorated._nvim_rpc_spec['name'] == 'Newautocmd'

decorated = autocmd()(newAutocmd)
assert decorated._nvim_rpc_spec['name'] == 'NewAutocmd'

decorated = autocmd()(new_autocmd)
assert decorated._nvim_rpc_spec['name'] == 'NewAutocmd'

decorated = autocmd()(_new_autocmd)
assert decorated._nvim_rpc_spec['name'] == 'NewAutocmd'
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ deps=
pytest
pytest-xdist
pyuv: pyuv
commands=python -m pytest -s
commands=python -m pytest -s {posargs}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm adding this so I can do tox -e py36 test/test_decorators.py


[testenv:checkqa]
deps =