Skip to content

Commit 121155c

Browse files
committed
update zig-lsp-codegen
1 parent d128eaa commit 121155c

File tree

2 files changed

+17
-25
lines changed

2 files changed

+17
-25
lines changed

build.zig.zon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
.minimum_zig_version = "0.14.0",
55
.dependencies = .{
66
.lsp_codegen = .{
7-
.url = "git+https://github.com/zigtools/zig-lsp-codegen.git#063a98c13a2293d8654086140813bdd1de6501bc",
8-
.hash = "lsp_codegen-0.1.0-CMjjo0ZXCQB-rAhPYrlfzzpU0u0u2MeGvUucZ-_g32eg",
7+
.url = "git+https://github.com/zigtools/zig-lsp-codegen.git#30ccbd10fec9c4eaad5196f2d560b80d6cc2c078",
8+
.hash = "lsp_codegen-0.1.0-CMjjo9nOCQA7rKs_uLFYA6A2FQ7hRqwXeID0CRnql0AD",
99
},
1010
},
1111
.paths = .{

src/utils.zig

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -139,34 +139,26 @@ pub fn randomize(
139139

140140
pub 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

163147
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);
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

172164
pub fn waitForResponseToRequest(

0 commit comments

Comments
 (0)