@@ -139,34 +139,26 @@ pub fn randomize(
139139
140140pub fn randomPosition (random : std.Random , data : []const u8 ) lsp.types.Position {
141141 // 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" );
161145}
162146
163147pub 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 );
166156
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+ }
168159
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 ;
170162}
171163
172164pub fn waitForResponseToRequest (
0 commit comments