From b94a09f36079126d9634e064eeaf6867780a12a8 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Sun, 7 Dec 2014 14:28:36 +0100 Subject: [PATCH] Fix end_pos of Literals and Whitespace leafs. --- jedi/parser/tree.py | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/jedi/parser/tree.py b/jedi/parser/tree.py index ff627d0b..95acd61d 100644 --- a/jedi/parser/tree.py +++ b/jedi/parser/tree.py @@ -207,11 +207,33 @@ class Leaf(Base): return "<%s: %s>" % (type(self).__name__, repr(self.value)) -class Whitespace(Leaf): +class LeafWithNewLines(Leaf): + @property + def end_pos(self): + """ + Literals and whitespace end_pos are more complicated than normal + end_pos, because the containing newlines may change the indexes. + """ + end_pos_line, end_pos_col = self.start_pos + lines = self.value.split('\n') + if self.value.endswith('\n'): + lines = lines[:-1] + lines[-1] += '\n' + end_pos_line += len(lines) - 1 + # Check for multiline token + if self.start_pos[0] == end_pos_line: + end_pos_col += len(lines[-1]) + else: + end_pos_col = len(lines[-1]) + return end_pos_line, end_pos_col + + +class Whitespace(LeafWithNewLines): type = 'whitespace' """Contains NEWLINE and ENDMARKER tokens.""" + class Name(Leaf): """ A string. Sometimes it is important to know if the string belongs to a name @@ -287,7 +309,7 @@ class Name(Leaf): return indexes -class Literal(Leaf): +class Literal(LeafWithNewLines): def eval(self): return literal_eval(self.value)