def _BuildTsFormatRange(request_data): filepath = request_data['filepath'] lines = GetFileLines(request_data, filepath) if 'range' not in request_data: return { 'file': filepath, 'line': 1, 'offset': 1, 'endLine': len(lines), 'endOffset': len(lines[-1]) + 1 } start = request_data['range']['start'] start_line_num = start['line_num'] start_line_value = lines[start_line_num - 1] start_codepoint = utils.ByteOffsetToCodepointOffset( start_line_value, start['column_num']) end = request_data['range']['end'] end_line_num = end['line_num'] end_line_value = lines[end_line_num - 1] end_codepoint = utils.ByteOffsetToCodepointOffset(end_line_value, end['column_num']) return { 'file': filepath, 'line': start_line_num, 'offset': start_codepoint, 'endLine': end_line_num, 'endOffset': end_codepoint }
def ByteOffsetToCodepointOffset_test(): # Tuples of ( ( unicode_line_value, byte_offset ), expected_result ). tests = [ # Simple ascii strings. (('test', 1), 1), (('test', 4), 4), (('test', 5), 5), # Unicode char at beginning. (('†est', 1), 1), (('†est', 4), 2), (('†est', 6), 4), (('†est', 7), 5), # Unicode char at end. (('tes†', 1), 1), (('tes†', 2), 2), (('tes†', 4), 4), (('tes†', 7), 5), # Unicode char in middle. (('tes†ing', 1), 1), (('tes†ing', 2), 2), (('tes†ing', 4), 4), (('tes†ing', 7), 5), (('tes†ing', 9), 7), (('tes†ing', 10), 8), ] for test in tests: yield lambda: eq_(utils.ByteOffsetToCodepointOffset(*test[0]), test[1])
def test_ByteOffsetToCodepointOffset(self): # Tuples of ( ( unicode_line_value, byte_offset ), expected_result ). test_cases = [ # Simple ascii strings. (('test', 1), 1), (('test', 4), 4), (('test', 5), 5), # Unicode char at beginning. (('†est', 1), 1), (('†est', 4), 2), (('†est', 6), 4), (('†est', 7), 5), # Unicode char at end. (('tes†', 1), 1), (('tes†', 2), 2), (('tes†', 4), 4), (('tes†', 7), 5), # Unicode char in middle. (('tes†ing', 1), 1), (('tes†ing', 2), 2), (('tes†ing', 4), 4), (('tes†ing', 7), 5), (('tes†ing', 9), 7), (('tes†ing', 10), 8), ] for test, expected in test_cases: with self.subTest(test=test, expected=expected): assert_that(utils.ByteOffsetToCodepointOffset(*test), equal_to(expected))
def GetByteOffsetDistanceFromTsDiagnosticRange(byte_offset, line_value, ts_diagnostic): ts_start_offset = ts_diagnostic['startLocation']['offset'] ts_end_offset = ts_diagnostic['endLocation']['offset'] codepoint_offset = utils.ByteOffsetToCodepointOffset( line_value, byte_offset) start_difference = codepoint_offset - ts_start_offset end_difference = codepoint_offset - (ts_end_offset - 1) if start_difference >= 0 and end_difference <= 0: return 0 return min(abs(start_difference), abs(end_difference))
def ByteOffsetToCodepointOffset_test(test, expected): assert_that(utils.ByteOffsetToCodepointOffset(*test), equal_to(expected))