From baa745a6ac0154fdeea67aee7d40da948af451b0 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Fri, 8 Jul 2016 08:39:36 +0200 Subject: [PATCH] A minor issue for getting the stack at a position, fixes #590. --- jedi/api/helpers.py | 18 +++++++++++++----- test/completion/parser.py | 9 ++++++++- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/jedi/api/helpers.py b/jedi/api/helpers.py index b5788b1f..a174eed6 100644 --- a/jedi/api/helpers.py +++ b/jedi/api/helpers.py @@ -3,6 +3,7 @@ Helpers for the API """ import re from collections import namedtuple +from textwrap import dedent from jedi._compatibility import u from jedi.evaluate.helpers import call_of_leaf @@ -28,9 +29,6 @@ def get_on_completion_name(lines, position): def _get_code(code_lines, start_pos, end_pos): - """ - :param code_start_pos: is where the code starts. - """ # Get relevant lines. lines = code_lines[start_pos[0] - 1:end_pos[0]] # Remove the parts at the end of the line. @@ -70,7 +68,15 @@ def get_stack_at_position(grammar, code_lines, module, pos): # impossible. raise OnErrorLeaf(user_stmt) - code = _get_code(code_lines, user_stmt.start_pos, pos) + start_pos = user_stmt.start_pos + if user_stmt.first_leaf() == '@': + # TODO this once again proves that just using user_stmt.get_code + # would probably be nicer than _get_code. + # Remove the indent to have a statement that is aligned (properties + # on the same line as function) + start_pos = start_pos[0], 0 + + code = _get_code(code_lines, start_pos, pos) if code == ';': # ; cannot be parsed. code = u('') @@ -78,7 +84,9 @@ def get_stack_at_position(grammar, code_lines, module, pos): # Remove whitespace at the end. Necessary, because the tokenizer will parse # an error token (there's no new line at the end in our case). This doesn't # alter any truth about the valid tokens at that position. - code = code.strip('\t ') + code = code.rstrip('\t ') + # Remove as many indents from **all** code lines as possible. + code = dedent(code) class EndMarkerReached(Exception): pass diff --git a/test/completion/parser.py b/test/completion/parser.py index 2cfdd06a..68793f4f 100644 --- a/test/completion/parser.py +++ b/test/completion/parser.py @@ -1,5 +1,5 @@ """ -Issues with the parser not the completion engine should be here. +Issues with the parser and not the type inference should be part of this file. """ class IndentIssues(): @@ -34,3 +34,10 @@ complete to definition. definition = 0 #? ['definition'] str(def + + +# It might be hard to determine the context +class Foo(object): + @property + #? ['str'] + def bar(str