diff --git a/jedi/parser/__init__.py b/jedi/parser/__init__.py index 71899702..d6f45a80 100644 --- a/jedi/parser/__init__.py +++ b/jedi/parser/__init__.py @@ -312,7 +312,6 @@ class Parser(object): def remove_last_newline(self): endmarker = self.module.children[-1] - print('ENDNL', self.module.children, repr(endmarker.prefix)) # The newline is either in the endmarker as a prefix or the previous # leaf as a newline token. if endmarker.prefix.endswith('\n'): @@ -320,7 +319,10 @@ class Parser(object): last_line = re.sub('.*\n', '', endmarker.prefix) endmarker.start_pos = endmarker.start_pos[0] - 1, len(last_line) else: - newline = endmarker.get_previous() + try: + newline = endmarker.get_previous() + except IndexError: + return # This means that the parser is empty. while True: if newline.value == '': # Must be a DEDENT, just continue. diff --git a/jedi/parser/tree.py b/jedi/parser/tree.py index fa4b5810..9e76a6aa 100644 --- a/jedi/parser/tree.py +++ b/jedi/parser/tree.py @@ -233,9 +233,6 @@ class LeafWithNewLines(Leaf): """ 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: diff --git a/test/test_parser/test_parser.py b/test/test_parser/test_parser.py index ea5e76e6..b6115570 100644 --- a/test/test_parser/test_parser.py +++ b/test/test_parser/test_parser.py @@ -155,3 +155,11 @@ def test_error_correction_with(): assert len(comps) > 30 # `open` completions have a closed attribute. assert [1 for c in comps if c.name == 'closed'] + + +def test_newline_positions(): + endmarker = Parser(load_grammar(), 'a\n').module.children[-1] + assert endmarker.end_pos == (2, 0) + new_line = endmarker.get_previous() + assert new_line.start_pos == (1, 1) + assert new_line.end_pos == (2, 0)