diff --git a/jedi/api/classes.py b/jedi/api/classes.py index 15a40696..3be20172 100644 --- a/jedi/api/classes.py +++ b/jedi/api/classes.py @@ -393,9 +393,9 @@ class BaseDefinition(object): path = self._name.get_root_context().py__file__() lines = parser_cache[self._evaluator.grammar._hashed][path].lines - line_nr = self._name.start_pos[0] - start_line_nr = max(line_nr - before, 0) - return ''.join(lines[start_line_nr:line_nr + after + 1]) + index = self._name.start_pos[0] - 1 + start_index = max(index - before, 0) + return ''.join(lines[start_index:index + after + 1]) class Completion(BaseDefinition): diff --git a/test/test_api/test_api.py b/test/test_api/test_api.py index 767d1818..a096e649 100644 --- a/test/test_api/test_api.py +++ b/test/test_api/test_api.py @@ -162,18 +162,19 @@ def test_get_line_code(): assert get_line_code('') == '' # On custom code + first_line = 'def foo():\n' line = ' foo' - assert get_line_code('def foo():\n%s' % line) == line + code = '%s%s' % (first_line, line) + assert get_line_code(code) == first_line # With before/after - line = ' foo' - source = 'def foo():\n%s\nother_line' % line - assert get_line_code(source, line=2) == line + '\n' - assert get_line_code(source, line=2, after=1) == line + '\nother_line' - assert get_line_code(source, line=2, after=1, before=1) == source + code = code + '\nother_line' + assert get_line_code(code, line=2) == first_line + assert get_line_code(code, line=2, after=1) == first_line + line + '\n' + assert get_line_code(code, line=2, after=2, before=1) == code # Should just be the whole thing, since there are no more lines on both # sides. - assert get_line_code(source, line=2, after=3, before=3) == source + assert get_line_code(code, line=2, after=3, before=3) == code def test_goto_assignments_follow_imports():