Skip to content

Commit

Permalink
Fix completion with one arg and add optional class objects to complet…
Browse files Browse the repository at this point in the history
…ion list (#811)
  • Loading branch information
mpanarin authored May 25, 2020
1 parent f7a523f commit 4217971
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
7 changes: 7 additions & 0 deletions pyls/lsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ class CompletionItemKind(object):
Color = 16
File = 17
Reference = 18
Folder = 19
EnumMember = 20
Constant = 21
Struct = 22
Event = 23
Operator = 24
TypeParameter = 25


class DocumentHighlightKind(object):
Expand Down
20 changes: 19 additions & 1 deletion pyls/plugins/jedi_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,25 @@ def pyls_completions(config, document, position):
snippet_support = completion_capabilities.get('completionItem', {}).get('snippetSupport')

should_include_params = settings.get('include_params')
should_include_class_objects = settings.get('include_class_objects', True)

include_params = snippet_support and should_include_params and use_snippets(document, position)
return [_format_completion(c, include_params) for c in completions] or None
include_class_objects = snippet_support and should_include_class_objects and use_snippets(document, position)

ready_completions = [
_format_completion(c, include_params)
for c in completions
]

if include_class_objects:
for c in completions:
if c.type == 'class':
completion_dict = _format_completion(c, False)
completion_dict['kind'] = lsp.CompletionItemKind.TypeParameter
completion_dict['label'] += ' object'
ready_completions.append(completion_dict)

return ready_completions or None


def is_exception_class(name):
Expand Down Expand Up @@ -153,6 +170,7 @@ def _format_completion(d, include_params=True):
snippet += ')$0'
completion['insertText'] = snippet
elif len(positional_args) == 1:
completion['insertTextFormat'] = lsp.InsertTextFormat.Snippet
completion['insertText'] = d.name + '($0)'
else:
completion['insertText'] = d.name + '()'
Expand Down
21 changes: 21 additions & 0 deletions test/plugins/test_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,27 @@ def test_snippets_completion(config, workspace):
com_position = {'line': 1, 'character': len(doc_snippets)}
completions = pyls_jedi_completions(config, doc, com_position)
assert completions[0]['insertText'] == 'defaultdict($0)'
assert completions[0]['insertTextFormat'] == lsp.InsertTextFormat.Snippet


def test_completion_with_class_objects(config, workspace):
doc_text = 'class FOOBAR(Object): pass\nFOOB'
com_position = {'line': 1, 'character': 4}
doc = Document(DOC_URI, workspace, doc_text)
config.capabilities['textDocument'] = {
'completion': {'completionItem': {'snippetSupport': True}}}
config.update({'plugins': {'jedi_completion': {
'include_params': True,
'include_class_objects': True,
}}})
completions = pyls_jedi_completions(config, doc, com_position)
assert len(completions) == 2

assert completions[0]['label'] == 'FOOBAR'
assert completions[0]['kind'] == lsp.CompletionItemKind.Class

assert completions[1]['label'] == 'FOOBAR object'
assert completions[1]['kind'] == lsp.CompletionItemKind.TypeParameter


def test_snippet_parsing(config, workspace):
Expand Down
5 changes: 5 additions & 0 deletions vscode-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@
"default": true,
"description": "Auto-completes methods and classes with tabstops for each parameter."
},
"pyls.plugins.jedi_completion.include_class_objects": {
"type": "boolean",
"default": true,
"description": "Adds class objects as a separate completion item."
},
"pyls.plugins.jedi_completion.fuzzy": {
"type": "boolean",
"default": false,
Expand Down

0 comments on commit 4217971

Please sign in to comment.