diff --git a/parso/python/diff.py b/parso/python/diff.py index 65a7317..ac11f31 100644 --- a/parso/python/diff.py +++ b/parso/python/diff.py @@ -608,13 +608,6 @@ class _NodesTree(object): break if node.type == 'endmarker': - # We basically removed the endmarker, but we are not allowed to - # remove the newline at the end of the line, otherwise it's - # going to be missing. - newline_index = max(node.prefix.rfind('\n'), node.prefix.rfind('\r')) - if newline_index > -1: - new_prefix = node.prefix[:newline_index + 1] - # Endmarkers just distort all the checks below. Remove them. break if node.type == 'error_leaf' and node.token_type in ('DEDENT', 'ERROR_DEDENT'): @@ -677,6 +670,16 @@ class _NodesTree(object): new_nodes.pop() if new_nodes: + if not _ends_with_newline(new_nodes[-1].get_last_leaf()) and not had_valid_suite_last: + p = new_nodes[-1].get_next_leaf().prefix + # We are not allowed to remove the newline at the end of the + # line, otherwise it's going to be missing. This happens e.g. + # if a bracket is around before that moves newlines to + # prefixes. + newline_index = max(p.rfind('\n'), p.rfind('\r')) + if newline_index > -1: + new_prefix = p[:newline_index + 1] + if had_valid_suite_last: last = new_nodes[-1] if last.type == 'decorated': diff --git a/test/test_diff_parser.py b/test/test_diff_parser.py index fb72398..91ccedd 100644 --- a/test/test_diff_parser.py +++ b/test/test_diff_parser.py @@ -1176,9 +1176,9 @@ def test_error_dedent_in_between(differ): differ.parse(code1, copies=1, parsers=2) -def test_x(differ): +def test_some_other_indentation_issues(differ): code1 = dedent('''\ - class SocketIO: + class C: x def f(): "" @@ -1198,5 +1198,19 @@ def test_x(differ): a ''') differ.initialize(code1) - differ.parse(code2, copies=ANY, parsers=ANY, expect_error_leaves=True) - differ.parse(code1, copies=ANY, parsers=ANY) + differ.parse(code2, copies=2, parsers=1, expect_error_leaves=True) + differ.parse(code1, copies=2, parsers=2) + + +def test_open_bracket(differ): + code1 = dedent('''\ + class C: + 1 + 2 # ha + ''') + code2 = insert_line_into_code(code1, 2, ' [str\n') + code3 = insert_line_into_code(code2, 4, ' str\n') + differ.initialize(code1) + differ.parse(code2, copies=1, parsers=1, expect_error_leaves=True) + differ.parse(code3, copies=1, parsers=1, expect_error_leaves=True) + differ.parse(code1, copies=1, parsers=1)