def __init__(self, repo: SmartRepo, file_lines: List[bytes], ast_path: CursorPath): with file_from_text(file_lines, ast_path.file) as file: translation_unit = repo.get_cindex().parse(file.name) cursor = ast_path.locate(translation_unit, ast_path.file) parent_cursor = ast_path.drop(1).locate(translation_unit, ast_path.file) siblings = list(parent_cursor.get_children()) insertion_point = siblings.index(cursor) - 1 self.parent_path = ast_path.drop(1) if insertion_point == -1: from_location = cursor.extent.start self.predecessor_path = None if len(siblings) > 1: to_location = SourceLocation.from_offset( translation_unit, siblings[1].extent.start.file, siblings[1].extent.start.offset - 1) else: to_location = cursor.extent.end else: from_location = siblings[insertion_point].extent.end self.predecessor_path = ast_path.drop(1).appended( parent_cursor, siblings[insertion_point]) to_location = cursor.extent.end self.from_location = from_location.offset self.to_location = to_location.offset self.ast_path = ast_path self.file_lines = file_lines
def test_location(): tu = get_tu(baseInput) one = get_cursor(tu, 'one') two = get_cursor(tu, 'two') assert one is not None assert two is not None assert_location(one.location, line=1, column=5, offset=4) assert_location(two.location, line=2, column=5, offset=13) # adding a linebreak at top should keep columns same tu = get_tu('\n' + baseInput) one = get_cursor(tu, 'one') two = get_cursor(tu, 'two') assert one is not None assert two is not None assert_location(one.location, line=2, column=5, offset=5) assert_location(two.location, line=3, column=5, offset=14) # adding a space should affect column on first line only tu = get_tu(' ' + baseInput) one = get_cursor(tu, 'one') two = get_cursor(tu, 'two') assert_location(one.location, line=1, column=6, offset=5) assert_location(two.location, line=2, column=5, offset=14) # define the expected location ourselves and see if it matches # the returned location tu = get_tu(baseInput) file = File.from_name(tu, 't.c') location = SourceLocation.from_position(tu, file, 1, 5) cursor = Cursor.from_location(tu, location) one = get_cursor(tu, 'one') assert one is not None assert one == cursor # Ensure locations referring to the same entity are equivalent. location2 = SourceLocation.from_position(tu, file, 1, 5) assert location == location2 location3 = SourceLocation.from_position(tu, file, 1, 4) assert location2 != location3 offset_location = SourceLocation.from_offset(tu, file, 5) cursor = Cursor.from_location(tu, offset_location) verified = False for n in [n for n in tu.cursor.get_children() if n.spelling == 'one']: assert n == cursor verified = True assert verified
def test_location(): tu = get_tu(baseInput) one = get_cursor(tu, 'one') two = get_cursor(tu, 'two') assert one is not None assert two is not None assert_location(one.location, line=1, column=5, offset=4) assert_location(two.location, line=2, column=5, offset=13) # adding a linebreak at top should keep columns same tu = get_tu('\n' + baseInput) one = get_cursor(tu, 'one') two = get_cursor(tu, 'two') assert one is not None assert two is not None assert_location(one.location, line=2, column=5, offset=5) assert_location(two.location, line=3, column=5, offset=14) # adding a space should affect column on first line only tu = get_tu(' ' + baseInput) one = get_cursor(tu, 'one') two = get_cursor(tu, 'two') assert_location(one.location, line=1, column=6, offset=5) assert_location(two.location, line=2, column=5, offset=14) # define the expected location ourselves and see if it matches # the returned location tu = get_tu(baseInput) file = File.from_name(tu, 't.c') location = SourceLocation.from_position(tu, file, 1, 5) cursor = Cursor.from_location(tu, location) one = get_cursor(tu, 'one') assert one is not None assert one == cursor # Ensure locations referring to the same entity are equivalent. location2 = SourceLocation.from_position(tu, file, 1, 5) assert location == location2 location3 = SourceLocation.from_position(tu, file, 1, 4) assert location2 != location3 offset_location = SourceLocation.from_offset(tu, file, 5) cursor = Cursor.from_location(tu, offset_location) verified = False for n in [n for n in tu.cursor.get_children() if n.spelling == 'one']: assert n == cursor verified = True assert verified