Fix get_line_code().

Fixes #948.
This commit is contained in:
Dave Halter
2017-09-09 18:28:05 +02:00
parent ef6a1ca10f
commit 381fedddb4
2 changed files with 11 additions and 10 deletions

View File

@@ -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):

View File

@@ -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():