forked from VimPlug/jedi
Replace get_all_import_names with a leaf search method in Simple.
This commit is contained in:
@@ -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
|
Resolve the user statement, if it is an import. Only resolve the
|
||||||
parts until the user position.
|
parts until the user position.
|
||||||
"""
|
"""
|
||||||
import_names = user_stmt.get_all_import_names()
|
name = user_stmt.leaf_for_position(user_context.position)
|
||||||
kill_count = -1
|
if name is None:
|
||||||
cur_name_part = None
|
raise NotImplementedError
|
||||||
for name in import_names:
|
|
||||||
if user_stmt.alias == name:
|
|
||||||
continue
|
|
||||||
|
|
||||||
if name.end_pos >= user_context.position:
|
i = imports.ImportWrapper(evaluator, name)
|
||||||
if not cur_name_part:
|
return i, name
|
||||||
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
|
|
||||||
|
|||||||
@@ -414,6 +414,17 @@ class Simple(Base):
|
|||||||
def get_code(self):
|
def get_code(self):
|
||||||
return "".join(c.get_code() for c in self.children)
|
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):
|
def __repr__(self):
|
||||||
code = self.get_code().replace('\n', ' ')
|
code = self.get_code().replace('\n', ' ')
|
||||||
if not is_py3:
|
if not is_py3:
|
||||||
@@ -1062,20 +1073,14 @@ class ForFlow(Flow):
|
|||||||
|
|
||||||
class Import(Simple):
|
class Import(Simple):
|
||||||
def get_all_import_names(self):
|
def get_all_import_names(self):
|
||||||
n = []
|
# TODO remove. do we even need this?
|
||||||
if self.from_names:
|
raise NotImplementedError
|
||||||
n += self.from_names
|
|
||||||
if self.namespace_names:
|
|
||||||
n += self.namespace_names
|
|
||||||
if self.alias is not None:
|
|
||||||
n.append(self.alias)
|
|
||||||
return n
|
|
||||||
|
|
||||||
def path_for_name(self, name):
|
def path_for_name(self, name):
|
||||||
for path in self._paths():
|
for path in self._paths():
|
||||||
if name in path:
|
if name in path:
|
||||||
return path[:path.index(name) + 1]
|
return path[:path.index(name) + 1]
|
||||||
raise NotImplementedError
|
raise ValueError('Name should be defined in the import itself')
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def level(self):
|
def level(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user