mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-10 07:41:51 +08:00
Fix import completion issues.
This commit is contained in:
@@ -133,7 +133,8 @@ class Script(object):
|
|||||||
# TODO this closure is ugly. it also doesn't work with
|
# TODO this closure is ugly. it also doesn't work with
|
||||||
# simple_complete (used for Interpreter), somehow redo.
|
# simple_complete (used for Interpreter), somehow redo.
|
||||||
module = self._parser.module()
|
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 = []
|
completions = []
|
||||||
#print(importer.completion_names(self._evaluator, True))
|
#print(importer.completion_names(self._evaluator, True))
|
||||||
if names is not None:
|
if names is not None:
|
||||||
@@ -144,13 +145,14 @@ class Script(object):
|
|||||||
|
|
||||||
# TODO this paragraph is necessary, but not sure it works.
|
# TODO this paragraph is necessary, but not sure it works.
|
||||||
context = self._user_context.get_context()
|
context = self._user_context.get_context()
|
||||||
|
|
||||||
# print(next(self._user_context.get_context()), 'x')
|
|
||||||
if not next(context).startswith('.'): # skip the path
|
if not next(context).startswith('.'): # skip the path
|
||||||
if next(context) == 'from':
|
if next(context) == 'from':
|
||||||
# completion is just "import" if before stands from ..
|
# completion is just "import" if before stands from ..
|
||||||
completions += ((k, bs) for k in keywords.keyword_names('import'))
|
if unfinished_dotted:
|
||||||
return completions
|
return completions
|
||||||
|
else:
|
||||||
|
return [(k, bs) for k in keywords.keyword_names('import')]
|
||||||
|
|
||||||
if isinstance(user_stmt, pr.Import):
|
if isinstance(user_stmt, pr.Import):
|
||||||
module = self._parser.module()
|
module = self._parser.module()
|
||||||
@@ -286,7 +288,7 @@ class Script(object):
|
|||||||
return []
|
return []
|
||||||
|
|
||||||
module = self._parser.module()
|
module = self._parser.module()
|
||||||
names, level, _ = helpers.check_error_statements(module, self._pos)
|
names, level, _, _ = helpers.check_error_statements(module, self._pos)
|
||||||
if names:
|
if names:
|
||||||
i = imports.get_importer(self._evaluator, names, module, level)
|
i = imports.get_importer(self._evaluator, names, module, level)
|
||||||
return i.follow(self._evaluator)
|
return i.follow(self._evaluator)
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ def check_error_statements(module, pos):
|
|||||||
if error_statement.first_type in ('import_from', 'import_name') \
|
if error_statement.first_type in ('import_from', 'import_name') \
|
||||||
and error_statement.first_pos < pos <= error_statement.next_start_pos:
|
and error_statement.first_pos < pos <= error_statement.next_start_pos:
|
||||||
return importer_from_error_statement(error_statement, 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):
|
def importer_from_error_statement(error_statement, pos):
|
||||||
@@ -51,12 +51,17 @@ def importer_from_error_statement(error_statement, pos):
|
|||||||
names = []
|
names = []
|
||||||
level = 0
|
level = 0
|
||||||
only_modules = True
|
only_modules = True
|
||||||
|
unfinished_dotted = False
|
||||||
for typ, nodes in error_statement.stack:
|
for typ, nodes in error_statement.stack:
|
||||||
if typ == 'dotted_name':
|
if typ == 'dotted_name':
|
||||||
names += check_dotted(nodes)
|
names += check_dotted(nodes)
|
||||||
|
if nodes[-1] == '.':
|
||||||
|
# An unfinished dotted_name
|
||||||
|
unfinished_dotted = True
|
||||||
elif typ == 'import_name':
|
elif typ == 'import_name':
|
||||||
if nodes[0].start_pos <= pos <= nodes[0].end_pos:
|
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':
|
elif typ == 'import_from':
|
||||||
for node in nodes:
|
for node in nodes:
|
||||||
if node.start_pos >= pos:
|
if node.start_pos >= pos:
|
||||||
@@ -70,4 +75,4 @@ def importer_from_error_statement(error_statement, pos):
|
|||||||
elif node == 'import':
|
elif node == 'import':
|
||||||
only_modules = False
|
only_modules = False
|
||||||
|
|
||||||
return names, level, only_modules
|
return names, level, only_modules, unfinished_dotted
|
||||||
|
|||||||
@@ -63,7 +63,7 @@ import datetime.date
|
|||||||
|
|
||||||
#? 21 ['import']
|
#? 21 ['import']
|
||||||
from import_tree.pkg import pkg
|
from import_tree.pkg import pkg
|
||||||
#? 22 ['import', 'mod1']
|
#? 22 ['mod1']
|
||||||
from import_tree.pkg. import mod1
|
from import_tree.pkg. import mod1
|
||||||
#? 17 ['mod1', 'mod2', 'random', 'pkg', 'rename1', 'rename2', 'recurse_class1', 'recurse_class2']
|
#? 17 ['mod1', 'mod2', 'random', 'pkg', 'rename1', 'rename2', 'recurse_class1', 'recurse_class2']
|
||||||
from import_tree. import pkg
|
from import_tree. import pkg
|
||||||
|
|||||||
@@ -74,6 +74,7 @@ def test_after_from():
|
|||||||
completions = Script(source, column=column).completions()
|
completions = Script(source, column=column).completions()
|
||||||
assert [c.name for c in completions] == result
|
assert [c.name for c in completions] == result
|
||||||
|
|
||||||
|
check('\nfrom os. ', ['path'])
|
||||||
check('\nfrom os ', ['import'])
|
check('\nfrom os ', ['import'])
|
||||||
check('from os ', ['import'])
|
check('from os ', ['import'])
|
||||||
check('\nfrom os import whatever', ['import'], len('from os im'))
|
check('\nfrom os import whatever', ['import'], len('from os im'))
|
||||||
|
|||||||
Reference in New Issue
Block a user