Import completion on syntactically correct imports.

This commit is contained in:
Dave Halter
2014-11-26 01:15:40 +01:00
parent 499c62df43
commit 149b4d8ad5
2 changed files with 68 additions and 58 deletions
+8 -2
View File
@@ -136,13 +136,19 @@ class Script(object):
) )
print(importer.completion_names(self._evaluator, True)) print(importer.completion_names(self._evaluator, True))
return [(name, module) for name in importer.completion_names(self._evaluator, True)] return [(name, module) for name in importer.completion_names(self._evaluator, True)]
# TODO DELETE still needed? elif isinstance(user_stmt, pr.Import):
if isinstance(user_stmt, pr.Import): # TODO this paragraph is necessary, but not sure it works.
context = self._user_context.get_context() context = self._user_context.get_context()
next(context) # skip the path next(context) # 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 ..
return ((k, bs) for k in keywords.keyword_names('import')) return ((k, bs) for k in keywords.keyword_names('import'))
module = self._parser.module()
name = user_stmt.name_for_position(self._pos)
imp = imports.ImportWrapper(self._evaluator, name)
return [(n, module) for n in imp.get_defined_names()]
return self._simple_complete(path, like) return self._simple_complete(path, like)
def completion_possible(path): def completion_possible(path):
+25 -21
View File
@@ -48,9 +48,13 @@ class ImportWrapper(pr.Base):
self.import_path = self._import.path_for_name(name) self.import_path = self._import.path_for_name(name)
self.is_like_search = False # TODO REMOVE self.is_like_search = False # TODO REMOVE
def get_defined_names(self, on_import_stmt=False): def get_defined_names(self):
# TODO not sure if this method is actually necessary. # The import path needs to be reduced by one, because we're completing.
return [] import_path = self.import_path[:-1]
module = self._import.get_parent_until()
importer = get_importer(self._evaluator, tuple(import_path),
module, self._import.level)
return importer.completion_names(self._evaluator)
@memoize_default() @memoize_default()
def follow(self, is_goto=False): def follow(self, is_goto=False):
@@ -622,24 +626,7 @@ class _Importer(object):
definition that is not defined in a module. definition that is not defined in a module.
""" """
names = [] names = []
if not self.import_path: # Empty import path=completion after import if self.import_path:
if not self._is_relative_import():
names += self._get_module_names()
if self._importer.file_path is not None:
path = os.path.abspath(self._importer.file_path)
for i in range(self.import_stmt.relative_count - 1):
path = os.path.dirname(path)
names += self._get_module_names([path])
if self._is_relative_import():
rel_path = os.path.join(self._importer.get_relative_path(),
'__init__.py')
if os.path.exists(rel_path):
m = _load_module(self._evaluator, rel_path)
names += m.get_defined_names()
# Import Path exists.
for scope in self.follow(evaluator): for scope in self.follow(evaluator):
# flask # flask
if self.import_path == ('flask', 'ext'): if self.import_path == ('flask', 'ext'):
@@ -684,6 +671,23 @@ class _Importer(object):
scope, include_builtin=False): scope, include_builtin=False):
for n in scope_names: for n in scope_names:
names.append(n) names.append(n)
else:
# Empty import path=completion after import
if not self.level:
names += self._get_module_names()
if self.file_path is not None:
path = os.path.abspath(self.file_path)
for i in range(self.level - 1):
path = os.path.dirname(path)
names += self._get_module_names([path])
if self.level:
rel_path = os.path.join(self.get_relative_path(),
'__init__.py')
if os.path.exists(rel_path):
m = _load_module(self._evaluator, rel_path)
names += m.get_defined_names()
return names return names