From c5071f9f49c79c649f5f7bb004046fd6188605a0 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Fri, 3 Feb 2017 17:23:15 +0100 Subject: [PATCH] Change get_previous_leaf to return None if there is no leaf anymore (at the start of the file). --- jedi/api/helpers.py | 19 ++++++++++--------- jedi/parser/__init__.py | 12 +++++------- jedi/parser/tree.py | 8 ++++---- 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/jedi/api/helpers.py b/jedi/api/helpers.py index 742d4634..294a5871 100644 --- a/jedi/api/helpers.py +++ b/jedi/api/helpers.py @@ -74,16 +74,14 @@ def _get_code_for_stack(code_lines, module_node, position): return u('') # If we're not on a comment simply get the previous leaf and proceed. - try: - leaf = leaf.get_previous_leaf() - except IndexError: + leaf = leaf.get_previous_leaf() + if leaf is None: return u('') # At the beginning of the file. is_after_newline = leaf.type == 'newline' while leaf.type == 'newline': - try: - leaf = leaf.get_previous_leaf() - except IndexError: + leaf = leaf.get_previous_leaf() + if leaf is None: return u('') if leaf.type == 'error_leaf' or leaf.type == 'string': @@ -244,6 +242,8 @@ def _get_call_signature_details_from_error_node(node, position): # until the parentheses is enough. children = node.children[index:] name = element.get_previous_leaf() + if name is None: + continue if name.type == 'name' or name.parent.type in ('trailer', 'atom'): return CallSignatureDetails( element, @@ -255,9 +255,8 @@ def get_call_signature_details(module, position): leaf = module.get_leaf_for_position(position, include_prefixes=True) if leaf.start_pos >= position: # Whitespace / comments after the leaf count towards the previous leaf. - try: - leaf = leaf.get_previous_leaf() - except IndexError: + leaf = leaf.get_previous_leaf() + if leaf is None: return None if leaf == ')': @@ -281,6 +280,8 @@ def get_call_signature_details(module, position): if node.type == 'trailer' and node.children[0] == '(': leaf = node.get_previous_leaf() + if leaf is None: + return None return CallSignatureDetails( node.children[0], *_get_index_and_key(node.children, position)) diff --git a/jedi/parser/__init__.py b/jedi/parser/__init__.py index d7a61f22..48196b81 100644 --- a/jedi/parser/__init__.py +++ b/jedi/parser/__init__.py @@ -212,16 +212,14 @@ class Parser(object): if '\n' not in prefix: # Basically if the last line doesn't end with a newline. we # have to add the previous line's end_position. - try: - last_end = endmarker.get_previous_leaf().end_pos[1] - except IndexError: - pass + previous_leaf = endmarker.get_previous_leaf() + if previous_leaf is not None: + last_end = previous_leaf.end_pos[1] last_line = re.sub('.*\n', '', prefix) endmarker.start_pos = endmarker.line - 1, last_end + len(last_line) else: - try: - newline = endmarker.get_previous_leaf() - except IndexError: + newline = endmarker.get_previous_leaf() + if newline is None: return # This means that the parser is empty. assert newline.value.endswith('\n') diff --git a/jedi/parser/tree.py b/jedi/parser/tree.py index f0b481fd..540db1cb 100644 --- a/jedi/parser/tree.py +++ b/jedi/parser/tree.py @@ -232,7 +232,7 @@ class Base(object): if i == 0: node = node.parent if node.parent is None: - raise IndexError('Cannot access the previous element of the first one.') + return None else: node = c[i - 1] break @@ -286,10 +286,10 @@ class Leaf(Base): self.indent = value[1] def get_start_pos_of_prefix(self): - try: - return self.get_previous_leaf().end_pos - except IndexError: + previous_leaf = self.get_previous_leaf() + if previous_leaf is None: return self.line - self.prefix.count('\n'), 0 # It's the first leaf. + return previous_leaf.end_pos @property def end_pos(self):