diff --git a/jedi/parser/fast.py b/jedi/parser/fast.py index a4b4cfec..acc224af 100644 --- a/jedi/parser/fast.py +++ b/jedi/parser/fast.py @@ -125,10 +125,27 @@ class ParserNode(object): self._content_scope = parser.module.subscopes[0] except IndexError: self._content_scope = parser.module + else: + self._rewrite_last_newline() # We need to be able to reset the original children of a parser. self._old_children = list(self._content_scope.children) + def _rewrite_last_newline(self): + """ + The ENDMARKER can contain a newline in the prefix. However this prefix + really belongs to the function - respectively to the next function or + parser node. If we don't rewrite that newline, we end up with a newline + in the wrong position, i.d. at the end of the file instead of in the + middle. + """ + c = self._content_scope.children + if pr.is_node(c[-1], 'suite'): # In a simple_stmt there's no DEDENT. + end_marker = self.parser.module.children[-1] + # Set the DEDENT prefix instead of the ENDMARKER. + c[-1].children[-1].prefix = end_marker.prefix + end_marker.prefix = '' + def __repr__(self): module = self.parser.module try: diff --git a/test/test_parser/test_fast_parser.py b/test/test_parser/test_fast_parser.py index 47342d1e..10e1deeb 100644 --- a/test/test_parser/test_fast_parser.py +++ b/test/test_parser/test_fast_parser.py @@ -301,6 +301,15 @@ def test_for_on_one_line(): """) check_fp(src, 2) + src = dedent("""\ + def hi(): + for x in foo: pass + + def nested(): + pass + """) + check_fp(src, 2) + def test_multi_line_for(): src = dedent("""\