get_code in Definition.description should not return first prefix.

This commit is contained in:
Dave Halter
2014-11-21 15:33:38 +01:00
parent f604066288
commit eb0bfb4381
4 changed files with 22 additions and 5 deletions

View File

@@ -471,11 +471,10 @@ class Script(object):
# The Evaluator.goto function checks for definitions, but since we # The Evaluator.goto function checks for definitions, but since we
# use a reverse tokenizer, we have new name_part objects, so we # use a reverse tokenizer, we have new name_part objects, so we
# have to check the user_stmt here for positions. # have to check the user_stmt here for positions.
if False and isinstance(user_stmt, pr.ExprStmt): if isinstance(user_stmt, pr.ExprStmt) \
and isinstance(last_name.parent, pr.ExprStmt):
for name in user_stmt.get_defined_names(): for name in user_stmt.get_defined_names():
if name.start_pos <= self._pos <= name.end_pos \ if name.start_pos <= self._pos <= name.end_pos:
and (not isinstance(name.parent, pr.Call)
or name.parent.next is None):
return [name] return [name]
defs = self._evaluator.goto(last_name) defs = self._evaluator.goto(last_name)

View File

@@ -591,7 +591,14 @@ class Definition(use_metaclass(CachedMetaClass, BaseDefinition)):
# only show module name # only show module name
d = 'module %s' % self.module_name d = 'module %s' % self.module_name
else: else:
d = d.get_code().replace('\n', '').replace('\r', '') first_leaf = d.first_leaf()
# Remove the prefix, because that's not what we want for get_code
# here.
old, first_leaf.prefix = first_leaf.prefix, ''
try:
d = d.get_code().replace('\n', '').replace('\r', '')
finally:
first_leaf.prefix = old
return d return d
@property @property

View File

@@ -443,6 +443,10 @@ class Evaluator(object):
return self.eval_element(call) return self.eval_element(call)
def goto(self, name): def goto(self, name):
stmt = name.parent
if isinstance(stmt, pr.ExprStmt) and name in stmt.get_defined_names():
return [name]
scope = name.get_parent_scope() scope = name.get_parent_scope()
if pr.is_node(name.parent, 'trailer'): if pr.is_node(name.parent, 'trailer'):
call = call_of_name(name, cut_own_trailer=True) call = call_of_name(name, cut_own_trailer=True)

View File

@@ -445,6 +445,13 @@ class Simple(Base):
return result return result
return None return None
def first_leaf(self):
try:
return self.children[0].first_leaf()
except AttributeError:
return self.children[0]
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: