Fix import completion issues.

This commit is contained in:
Dave Halter
2014-12-18 02:55:03 +01:00
parent f8cd3c661a
commit 1fb13837c4
4 changed files with 18 additions and 10 deletions

View File

@@ -133,7 +133,8 @@ class Script(object):
# TODO this closure is ugly. it also doesn't work with
# simple_complete (used for Interpreter), somehow redo.
module = self._parser.module()
names, level, only_modules = helpers.check_error_statements(module, self._pos)
names, level, only_modules, unfinished_dotted = \
helpers.check_error_statements(module, self._pos)
completions = []
#print(importer.completion_names(self._evaluator, True))
if names is not None:
@@ -145,12 +146,13 @@ class Script(object):
# TODO this paragraph is necessary, but not sure it works.
context = self._user_context.get_context()
# print(next(self._user_context.get_context()), 'x')
if not next(context).startswith('.'): # skip the path
if next(context) == 'from':
# completion is just "import" if before stands from ..
completions += ((k, bs) for k in keywords.keyword_names('import'))
if unfinished_dotted:
return completions
else:
return [(k, bs) for k in keywords.keyword_names('import')]
if isinstance(user_stmt, pr.Import):
module = self._parser.module()
@@ -286,7 +288,7 @@ class Script(object):
return []
module = self._parser.module()
names, level, _ = helpers.check_error_statements(module, self._pos)
names, level, _, _ = helpers.check_error_statements(module, self._pos)
if names:
i = imports.get_importer(self._evaluator, names, module, level)
return i.follow(self._evaluator)

View File

@@ -39,7 +39,7 @@ def check_error_statements(module, pos):
if error_statement.first_type in ('import_from', 'import_name') \
and error_statement.first_pos < pos <= error_statement.next_start_pos:
return importer_from_error_statement(error_statement, pos)
return None, 0, False
return None, 0, False, False
def importer_from_error_statement(error_statement, pos):
@@ -51,12 +51,17 @@ def importer_from_error_statement(error_statement, pos):
names = []
level = 0
only_modules = True
unfinished_dotted = False
for typ, nodes in error_statement.stack:
if typ == 'dotted_name':
names += check_dotted(nodes)
if nodes[-1] == '.':
# An unfinished dotted_name
unfinished_dotted = True
elif typ == 'import_name':
if nodes[0].start_pos <= pos <= nodes[0].end_pos:
return None, 0, False
# We are on the import.
return None, 0, False, False
elif typ == 'import_from':
for node in nodes:
if node.start_pos >= pos:
@@ -70,4 +75,4 @@ def importer_from_error_statement(error_statement, pos):
elif node == 'import':
only_modules = False
return names, level, only_modules
return names, level, only_modules, unfinished_dotted

View File

@@ -63,7 +63,7 @@ import datetime.date
#? 21 ['import']
from import_tree.pkg import pkg
#? 22 ['import', 'mod1']
#? 22 ['mod1']
from import_tree.pkg. import mod1
#? 17 ['mod1', 'mod2', 'random', 'pkg', 'rename1', 'rename2', 'recurse_class1', 'recurse_class2']
from import_tree. import pkg

View File

@@ -74,6 +74,7 @@ def test_after_from():
completions = Script(source, column=column).completions()
assert [c.name for c in completions] == result
check('\nfrom os. ', ['path'])
check('\nfrom os ', ['import'])
check('from os ', ['import'])
check('\nfrom os import whatever', ['import'], len('from os im'))