Skip to content

Commit

Permalink
Merge branch 'develop' into importmagic
Browse files Browse the repository at this point in the history
  • Loading branch information
youben11 authored Nov 14, 2019
2 parents 6812993 + 71c2fc5 commit 7be97a3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
20 changes: 19 additions & 1 deletion pyls/plugins/jedi_completion.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright 2017 Palantir Technologies, Inc.
import logging
import parso
from pyls import hookimpl, lsp, _utils

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -50,8 +51,25 @@ def pyls_completions(config, document, position):

settings = config.plugin_settings('jedi_completion', document_path=document.path)
should_include_params = settings.get('include_params')
include_params = (use_snippets(document, position) and
snippet_support and should_include_params)
return [_format_completion(d, include_params) for d in definitions] or None

return [_format_completion(d, snippet_support and should_include_params) for d in definitions] or None

def use_snippets(document, position):
"""
Determine if it's necessary to return snippets in code completions.
This returns `False` if a completion is being requested on an import
statement, `True` otherwise.
"""
lines = document.source.split('\n')
act_line = lines[position['line']]
tokens = parso.parse(act_line)
act_statement = tokens.children[0].get_code()
if act_statement.startswith('import') or act_statement.startswith('from'):
return False
return True


def _format_completion(d, include_params=True):
Expand Down
16 changes: 16 additions & 0 deletions test/plugins/test_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,19 @@ def test_matplotlib_completions(config):

assert items
assert any(['plot' in i['label'] for i in items])


def test_snippets_completion(config):
doc_snippets = 'from collections import defaultdict \na=defaultdict'
com_position = {'line': 0, 'character': 35}
doc = Document(DOC_URI, doc_snippets)
config.capabilities['textDocument'] = {
'completion': {'completionItem': {'snippetSupport': True}}}
config.update({'plugins': {'jedi_completion': {'include_params': True}}})
completions = pyls_jedi_completions(config, doc, com_position)
assert completions[0]['insertText'] == 'defaultdict'

com_position = {'line': 1, 'character': len(doc_snippets)}
completions = pyls_jedi_completions(config, doc, com_position)
out = 'defaultdict(${1:default_factory}, ${2:iterable}, ${3:kwargs})$0'
assert completions[0]['insertText'] == out

0 comments on commit 7be97a3

Please sign in to comment.