From e5f09e1c7df318b42695d44e0ebda7d70bfbb3e5 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Fri, 8 Jul 2016 00:03:52 +0200 Subject: [PATCH] Fix an issue with end_pos of a module. --- jedi/parser/__init__.py | 18 ++++++++++++++---- test/test_api/test_call_signatures.py | 8 ++++++++ test/test_parser/test_get_code.py | 2 ++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/jedi/parser/__init__.py b/jedi/parser/__init__.py index 0d842411..8fb915a5 100644 --- a/jedi/parser/__init__.py +++ b/jedi/parser/__init__.py @@ -232,10 +232,19 @@ class Parser(object): endmarker = self._parsed.children[-1] # The newline is either in the endmarker as a prefix or the previous # leaf as a newline token. - if endmarker.prefix.endswith('\n'): - endmarker.prefix = endmarker.prefix[:-1] - last_line = re.sub('.*\n', '', endmarker.prefix) - endmarker._start_pos = endmarker._start_pos[0] - 1, len(last_line) + prefix = endmarker.prefix + if prefix.endswith('\n'): + endmarker.prefix = prefix = prefix[:-1] + last_end = 0 + 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 + last_line = re.sub('.*\n', '', prefix) + endmarker._start_pos = endmarker._start_pos[0] - 1, last_end + len(last_line) else: try: newline = endmarker.get_previous_leaf() @@ -251,6 +260,7 @@ class Parser(object): # will be no previous leaf. So just ignore it. break elif newline.value != '\n': + # TODO REMOVE, error recovery was simplified. # This may happen if error correction strikes and removes # a whole statement including '\n'. break diff --git a/test/test_api/test_call_signatures.py b/test/test_api/test_call_signatures.py index 81d3c026..c238ca56 100644 --- a/test/test_api/test_call_signatures.py +++ b/test/test_api/test_call_signatures.py @@ -369,3 +369,11 @@ def test_different_caller(): assert_signature('(str)(', 'str', 0) assert_signature('(str)()', 'str', 0, column=len('(str)(')) + + +def test_in_function(): + code = dedent('''\ + class X(): + @property + def func(''') + assert not Script(code).call_signatures() diff --git a/test/test_parser/test_get_code.py b/test/test_parser/test_get_code.py index be825208..43202fb0 100644 --- a/test/test_parser/test_get_code.py +++ b/test/test_parser/test_get_code.py @@ -103,3 +103,5 @@ def test_end_newlines(): test('a\n#comment', (2, 8)) test('a#comment', (1, 9)) test('def a():\n pass', (2, 5)) + + test('def a(', (1, 6))