From 1d64a5caa1c85504c671229c42db686beeff7cef Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Fri, 3 Feb 2017 17:35:53 +0100 Subject: [PATCH] Replace first_leaf and last_leaf with get_first_leaf and get_last_leaf. --- jedi/parser/diff.py | 16 ++++++++-------- jedi/parser/tree.py | 23 ++++++++++------------- 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/jedi/parser/diff.py b/jedi/parser/diff.py index cdc88e2f..4cf29421 100644 --- a/jedi/parser/diff.py +++ b/jedi/parser/diff.py @@ -44,7 +44,7 @@ def _merge_used_names(base_dict, other_dict): def _get_last_line(node_or_leaf): - last_leaf = node_or_leaf.last_leaf() + last_leaf = node_or_leaf.get_last_leaf() if _ends_with_newline(last_leaf): return last_leaf.start_pos[0] else: @@ -267,7 +267,7 @@ class DiffParser(object): return None line = self._nodes_stack.parsed_until_line + 1 - node = self._new_module.last_leaf() + node = self._new_module.get_last_leaf() while True: parent = node.parent if parent.type in ('suite', 'file_input'): @@ -426,7 +426,7 @@ class _NodesStackNode(object): assert not self.parent return 0 - last_leaf = self.children_groups[-1].children[-1].last_leaf() + last_leaf = self.children_groups[-1].children[-1].get_last_leaf() line = last_leaf.end_pos[0] # Calculate the line offsets @@ -500,7 +500,7 @@ class _NodesStack(object): """ Helps cleaning up the tree nodes that get inserted. """ - last_leaf = tree_nodes[-1].last_leaf() + last_leaf = tree_nodes[-1].get_last_leaf() is_endmarker = last_leaf.type == self.endmarker_type self._last_prefix = '' if is_endmarker: @@ -515,7 +515,7 @@ class _NodesStack(object): last_leaf.prefix, self._last_prefix = \ last_leaf.prefix[:separation + 1], last_leaf.prefix[separation + 1:] - first_leaf = tree_nodes[0].first_leaf() + first_leaf = tree_nodes[0].get_first_leaf() first_leaf.prefix = self.prefix + first_leaf.prefix self.prefix = '' @@ -592,13 +592,13 @@ class _NodesStack(object): new_nodes.pop() while new_nodes: last_node = new_nodes[-1] - if last_node.last_leaf().type == 'newline': + if last_node.get_last_leaf().type == 'newline': break new_nodes.pop() if new_nodes: try: - last_line_offset_leaf = new_nodes[line_offset_index].last_leaf() + last_line_offset_leaf = new_nodes[line_offset_index].get_last_leaf() except IndexError: line_offset = 0 # In this case we don't have to calculate an offset, because @@ -621,7 +621,7 @@ class _NodesStack(object): # Add an endmarker. try: - last_leaf = self._module.last_leaf() + last_leaf = self._module.get_last_leaf() end_pos = list(last_leaf.end_pos) except IndexError: end_pos = [1, 0] diff --git a/jedi/parser/tree.py b/jedi/parser/tree.py index 52b5a560..0618e4a9 100644 --- a/jedi/parser/tree.py +++ b/jedi/parser/tree.py @@ -298,10 +298,10 @@ class Leaf(Base): def move(self, line_offset): self.line += line_offset - def first_leaf(self): + def get_first_leaf(self): return self - def last_leaf(self): + def get_last_leaf(self): return self def get_code(self, normalized=False, include_prefix=True): @@ -560,14 +560,11 @@ class BaseNode(Base): pass # Must be a non-scope return None - def first_leaf(self): - try: - return self.children[0].first_leaf() - except AttributeError: - return self.children[0] + def get_first_leaf(self): + return self.children[0].get_first_leaf() - def last_leaf(self): - return self.children[-1].last_leaf() + def get_last_leaf(self): + return self.children[-1].get_last_leaf() def get_following_comment_same_line(self): """ @@ -576,11 +573,11 @@ class BaseNode(Base): """ try: if self.type == 'for_stmt': - whitespace = self.children[5].first_leaf().prefix + whitespace = self.children[5].get_first_leaf().prefix elif self.type == 'with_stmt': - whitespace = self.children[3].first_leaf().prefix + whitespace = self.children[3].get_first_leaf().prefix else: - whitespace = self.last_leaf().get_next_leaf().prefix + whitespace = self.get_last_leaf().get_next_leaf().prefix except AttributeError: return None except ValueError: @@ -1120,7 +1117,7 @@ class Flow(BaseNode): for i, child in enumerate(self.children): if start_pos < child.start_pos: return keyword - first_leaf = child.first_leaf() + first_leaf = child.get_first_leaf() if first_leaf in self.FLOW_KEYWORDS: keyword = first_leaf return 0