aboutsummaryrefslogtreecommitdiff
path: root/src/common/position.odin
blob: e8c5ff25424c0b7b0a2ad503c113751ffe6b0a70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package common

import "core:strings"
import "core:unicode/utf8"
import "core:fmt"
import "core:odin/ast"

/*
	This file handles the conversion between utf-16 and utf-8 offsets in the text document
*/

//TODO(Optimize by calculating all the newlines at parse instead of calculating them)

Position :: struct {
	line:      int,
	character: int,
}

Range :: struct {
	start: Position,
	end:   Position,
}

Location :: struct {
	uri:   string,
	range: Range,
}

AbsoluteRange :: struct {
	start: int,
	end:   int,
}

AbsolutePosition :: int

get_absolute_position :: proc(position: Position, document_text: []u8) -> (AbsolutePosition, bool) {
	absolute: AbsolutePosition

	if len(document_text) == 0 {
		absolute = 0
		return absolute, true
	}

	line_count := 0
	index := 1
	last := document_text[0]

	if !get_index_at_line(&index, &line_count, &last, document_text, position.line) {
		return absolute, false
	}

	absolute = index + get_character_offset_u16_to_u8(position.character, document_text[index:])

	return absolute, true
}

get_relative_token_position :: proc(offset: int, document_text: []u8, current_start: int) -> Position {
	start_index := current_start

	data := document_text[start_index:]

	i: int

	position: Position

	for i + start_index < offset {
		r, w := utf8.decode_rune(data[i:])

		if r == '\n' { //\r?
			position.character = 0
			position.line += 1
			i             += 1
		} else if w == 0 {
			return position
		} else {
			if r < 0x10000 {
				position.character += 1
			} else {
				position.character += 2
			}

			i += w
		}
	}

	return position
}

/*
	Get the range of a token in utf16 space
*/
get_token_range :: proc(node: ast.Node, document_text: string) -> Range {
	range: Range

	go_backwards_to_endline :: proc(offset: int, document_text: []u8) -> int {
		index := offset

		for index > 0 && document_text[index] != '\n' && document_text[index] != '\r' {
			index -= 1
		}

		if index == 0 {
			return 0
		}

		return index + 1
	}

	pos_offset := min(len(document_text) - 1, node.pos.offset)
	end_offset := min(len(document_text) - 1, node.end.offset)

	offset := go_backwards_to_endline(pos_offset, transmute([]u8)document_text)

	range.start.line = node.pos.line - 1
	range.start.character = get_character_offset_u8_to_u16(node.pos.column - 1, transmute([]u8)document_text[offset:])

	offset = go_backwards_to_endline(end_offset - 1, transmute([]u8)document_text)

	range.end.line = node.end.line - 1
	range.end.character = get_character_offset_u8_to_u16(node.end.column - 1, transmute([]u8)document_text[offset:])

	return range
}

get_absolute_range :: proc(range: Range, document_text: []u8) -> (AbsoluteRange, bool) {
	absolute: AbsoluteRange

	if len(document_text) == 0 {
		absolute.start = 0
		absolute.end   = 0
		return absolute, true
	}

	line_count := 0
	index      := 1
	last       := document_text[0]

	if !get_index_at_line(&index, &line_count, &last, document_text, range.start.line) {
		return absolute, false
	}

	absolute.start = index + get_character_offset_u16_to_u8(range.start.character, document_text[index:])

	//if the last line was indexed at zero we have to move it back to index 1.
	//This happens when line = 0
	if index == 0 {
		index = 1
	}

	if !get_index_at_line(&index, &line_count, &last, document_text, range.end.line) {
		return absolute, false
	}

	absolute.end = index + get_character_offset_u16_to_u8(range.end.character, document_text[index:])

	return absolute, true
}

get_index_at_line :: proc(current_index: ^int, current_line: ^int, last: ^u8, document_text: []u8, end_line: int) -> bool {
	if end_line == 0 {
		current_index^ = 0
		return true
	}

	if current_line^ == end_line {
		return true
	}

	for ; current_index^ < len(document_text); current_index^ += 1 {
		current := document_text[current_index^]

		if last^ == '\r' {
			current_line^ += 1

			if current_line^ == end_line {
				last^ = current
				current_index^ += 1
				return true
			}
		} else if current == '\n' {
			current_line^ += 1

			if current_line^ == end_line {
				last^ = current
				current_index^ += 1
				return true
			}
		}

		last^ = document_text[current_index^]
	}

	return false
}

get_character_offset_u16_to_u8 :: proc(character_offset: int, document_text: []u8) -> int {
	utf8_idx  := 0
	utf16_idx := 0

	for utf16_idx < character_offset {
		r, w := utf8.decode_rune(document_text[utf8_idx:])

		if r == '\n' || r == '\r' {
			return utf8_idx
		} else if w == 0 {
			return utf8_idx
		} else if r < 0x10000 {
			utf16_idx += 1
		} else {
			utf16_idx += 2
		}

		utf8_idx += w
	}

	return utf8_idx
}

get_character_offset_u8_to_u16 :: proc(character_offset: int, document_text: []u8) -> int {
	utf8_idx  := 0
	utf16_idx := 0

	for utf8_idx < character_offset {
		r, w := utf8.decode_rune(document_text[utf8_idx:])

		if r == '\n' || r == '\r' {
			return utf16_idx
		} else if w == 0 {
			return utf16_idx
		} else if r < 0x10000 {
			utf16_idx += 1
		} else {
			utf16_idx += 2
		}

		utf8_idx += w
	}

	return utf16_idx
}