From 93f1cdebbcfbcbf3ae6cfbd4753c83be0554bb86 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Sun, 22 Mar 2020 21:15:22 +0100 Subject: [PATCH] Try to make parsed trees more similar for incomplete dedents, see also davidhalter/jedi#1499 --- parso/python/diff.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/parso/python/diff.py b/parso/python/diff.py index 9abe38c..ea0d16e 100644 --- a/parso/python/diff.py +++ b/parso/python/diff.py @@ -507,23 +507,32 @@ class _NodesTree(object): def _get_insertion_node(self, indentation_node): indentation = indentation_node.start_pos[1] + previous_node = None # find insertion node - while True: - node = self._working_stack[-1] + for node in reversed(self._working_stack): tree_node = node.tree_node if tree_node.type == 'suite': # A suite starts with NEWLINE, ... node_indentation = tree_node.children[1].start_pos[1] + if indentation > node_indentation and previous_node is not None: + # This means that it was not dedented enough. + node = previous_node + break if indentation >= node_indentation: # Not a Dedent # We might be at the most outer layer: modules. We # don't want to depend on the first statement # having the right indentation. - return node - + break elif tree_node.type == 'file_input': - return node + if indentation > 0 and previous_node is not None: + node = previous_node + break + previous_node = node + while True: + if node == self._working_stack[-1]: + return node self._working_stack.pop() def add_parsed_nodes(self, tree_nodes):