Skip to content

Commit

Permalink
Completion detail using module name, function parameters (#170)
Browse files Browse the repository at this point in the history
* Use "builtin" or parent path for completion detail

* Append parameters to completion item label if function/method

* Update tests to expect labels with parentheses and args

* Update test_completion.py

* Check completion label specifically for python 2 or 3

* Use list comprehension instead of map for parameter list
  • Loading branch information
tomv564 authored and gatesn committed Oct 30, 2017
1 parent 9a3545d commit 106e760
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
19 changes: 16 additions & 3 deletions pyls/plugins/completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,27 @@
def pyls_completions(document, position):
definitions = document.jedi_script(position).completions()
return [{
'label': d.name,
'label': _label(d),
'kind': _kind(d),
'detail': d.description or "",
'detail': _detail(d),
'documentation': d.docstring(),
'sortText': _sort_text(d)
'sortText': _sort_text(d),
'insertText': d.name
} for d in definitions]


def _label(definition):
if definition.type in ('function', 'method'):
params = ", ".join(param.name for param in definition.params)
return "{}({})".format(definition.name, params)

return definition.name


def _detail(definition):
return "builtin" if definition.in_builtin_module() else definition.parent().full_name or ""


def _sort_text(definition):
""" Ensure builtins appear at the bottom.
Description is of format <type>: <module>.<item>
Expand Down
10 changes: 7 additions & 3 deletions test/plugins/test_completion.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright 2017 Palantir Technologies, Inc.
import sys
from pyls import uris
from pyls.plugins.completion import pyls_completions
from pyls.workspace import Document
Expand All @@ -23,7 +24,10 @@ def test_completion():
items = pyls_completions(doc, com_position)

assert len(items) > 0
assert items[0]['label'] == 'read'
if (sys.version_info > (3, 0)):
assert items[0]['label'] == 'read(args)'
else:
assert items[0]['label'] == 'read()'


def test_completion_ordering():
Expand All @@ -35,6 +39,6 @@ def test_completion_ordering():
items = {c['label']: c['sortText'] for c in completions}

# Assert that builtins come after our own functions even if alphabetically they're before
assert items['hello'] < items['dict']
assert items['hello()'] < items['dict']
# And that 'hidden' functions come after unhidden ones
assert items['hello'] < items['_a_hello']
assert items['hello()'] < items['_a_hello()']

0 comments on commit 106e760

Please sign in to comment.