diff --git a/jedi/api/classes.py b/jedi/api/classes.py index e67b64ac..15a40696 100644 --- a/jedi/api/classes.py +++ b/jedi/api/classes.py @@ -394,7 +394,7 @@ class BaseDefinition(object): lines = parser_cache[self._evaluator.grammar._hashed][path].lines line_nr = self._name.start_pos[0] - start_line_nr = line_nr - before + start_line_nr = max(line_nr - before, 0) return ''.join(lines[start_line_nr:line_nr + after + 1]) diff --git a/test/test_api/test_api.py b/test/test_api/test_api.py index dd5ed6fb..767d1818 100644 --- a/test/test_api/test_api.py +++ b/test/test_api/test_api.py @@ -171,6 +171,9 @@ def test_get_line_code(): 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 + # 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 def test_goto_assignments_follow_imports():