@@ -139,34 +139,26 @@ pub fn randomize(
139
139
140
140
pub fn randomPosition (random : std.Random , data : []const u8 ) lsp.types.Position {
141
141
// TODO: Consider offsets
142
-
143
- const line_count = std .mem .count (u8 , data , "\n " );
144
- const line = if (line_count == 0 ) 0 else random .intRangeLessThan (usize , 0 , line_count );
145
- var lines = std .mem .splitScalar (u8 , data , '\n ' );
146
-
147
- var character : usize = 0 ;
148
-
149
- var index : usize = 0 ;
150
- while (lines .next ()) | line_content | : (index += 1 ) {
151
- if (index == line ) {
152
- character = if (line_content .len == 0 ) 0 else random .intRangeLessThan (usize , 0 , line_content .len );
153
- break ;
154
- }
155
- }
156
-
157
- return .{
158
- .line = @intCast (line ),
159
- .character = @intCast (character ),
160
- };
142
+ var index = random .uintAtMost (usize , data .len );
143
+ while (index != 0 and index < data .len and ! isFirstUtf8Byte (data [index ])) index -= 1 ;
144
+ return lsp .offsets .indexToPosition (data , index , .@"utf-16" );
161
145
}
162
146
163
147
pub fn randomRange (random : std.Random , data : []const u8 ) lsp.types.Range {
164
- const a = randomPosition (random , data );
165
- const b = randomPosition (random , data );
148
+ // TODO: Consider offsets
149
+ var loc : lsp.offsets.Loc = .{
150
+ .start = random .uintAtMost (usize , data .len ),
151
+ .end = random .uintAtMost (usize , data .len ),
152
+ };
153
+ while (loc .start != 0 and loc .start < data .len and ! isFirstUtf8Byte (data [loc .start ])) loc .start -= 1 ;
154
+ while (loc .end != 0 and loc .end < data .len and ! isFirstUtf8Byte (data [loc .end ])) loc .end -= 1 ;
155
+ if (loc .start > loc .end ) std .mem .swap (usize , & loc .start , & loc .end );
166
156
167
- const is_a_first = a .line < b .line or (a .line == b .line and a .character < b .character );
157
+ return lsp .offsets .locToRange (data , loc , .@"utf-16" );
158
+ }
168
159
169
- return if (is_a_first ) .{ .start = a , .end = b } else .{ .start = b , .end = a };
160
+ fn isFirstUtf8Byte (byte : u8 ) bool {
161
+ return byte & 0b11000000 != 0b10000000 ;
170
162
}
171
163
172
164
pub fn waitForResponseToRequest (
0 commit comments