1
0
forked from VimPlug/jedi

Replace get_all_import_names with a leaf search method in Simple.

This commit is contained in:
Dave Halter
2014-11-19 01:31:08 +01:00
parent bab6788b42
commit 1c240e75d3
2 changed files with 19 additions and 27 deletions

View File

@@ -25,22 +25,9 @@ def get_on_import_stmt(evaluator, user_context, user_stmt, is_like_search=False)
Resolve the user statement, if it is an import. Only resolve the
parts until the user position.
"""
import_names = user_stmt.get_all_import_names()
kill_count = -1
cur_name_part = None
for name in import_names:
if user_stmt.alias == name:
continue
name = user_stmt.leaf_for_position(user_context.position)
if name is None:
raise NotImplementedError
if name.end_pos >= user_context.position:
if not cur_name_part:
cur_name_part = name
kill_count += 1
context = user_context.get_context()
just_from = next(context) == 'from'
i = imports.ImportWrapper(evaluator, user_stmt, is_like_search,
kill_count=kill_count, nested_resolve=True,
is_just_from=just_from)
return i, cur_name_part
i = imports.ImportWrapper(evaluator, name)
return i, name

View File

@@ -414,6 +414,17 @@ class Simple(Base):
def get_code(self):
return "".join(c.get_code() for c in self.children)
def leaf_for_position(self, position):
for c in self.children:
if isinstance(c, Leaf):
if c.start_pos <= position < c.end_pos:
return c
else:
result = c.leaf_for_position(position)
if result is not None:
return result
return None
def __repr__(self):
code = self.get_code().replace('\n', ' ')
if not is_py3:
@@ -1062,20 +1073,14 @@ class ForFlow(Flow):
class Import(Simple):
def get_all_import_names(self):
n = []
if self.from_names:
n += self.from_names
if self.namespace_names:
n += self.namespace_names
if self.alias is not None:
n.append(self.alias)
return n
# TODO remove. do we even need this?
raise NotImplementedError
def path_for_name(self, name):
for path in self._paths():
if name in path:
return path[:path.index(name) + 1]
raise NotImplementedError
raise ValueError('Name should be defined in the import itself')
@property
def level(self):