2022-11-26 01:14:33 +00:00
|
|
|
const std = @import("std");
|
|
|
|
const zls = @import("zls");
|
|
|
|
const builtin = @import("builtin");
|
|
|
|
|
|
|
|
const helper = @import("../helper.zig");
|
|
|
|
const Context = @import("../context.zig").Context;
|
|
|
|
const ErrorBuilder = @import("../ErrorBuilder.zig");
|
|
|
|
|
|
|
|
const types = zls.types;
|
|
|
|
const offsets = zls.offsets;
|
|
|
|
const requests = zls.requests;
|
|
|
|
|
|
|
|
const allocator: std.mem.Allocator = std.testing.allocator;
|
|
|
|
|
|
|
|
test "selectionRange - empty" {
|
|
|
|
try testSelectionRange("<>", &.{});
|
|
|
|
}
|
|
|
|
|
|
|
|
test "seletionRange - smoke" {
|
|
|
|
try testSelectionRange(
|
|
|
|
\\fn main() void {
|
|
|
|
\\ const x = 1 <>+ 1;
|
|
|
|
\\}
|
|
|
|
, &.{ "1 + 1", "const x = 1 + 1", "{\n const x = 1 + 1;\n}" });
|
|
|
|
}
|
|
|
|
|
|
|
|
fn testSelectionRange(source: []const u8, want: []const []const u8) !void {
|
|
|
|
var phr = try helper.collectClearPlaceholders(allocator, source);
|
|
|
|
defer phr.deinit(allocator);
|
|
|
|
|
|
|
|
var ctx = try Context.init();
|
|
|
|
defer ctx.deinit();
|
|
|
|
|
2023-02-11 19:19:37 +00:00
|
|
|
const test_uri = try ctx.addDocument(phr.new_source);
|
2022-11-26 01:14:33 +00:00
|
|
|
|
2022-12-27 06:47:57 +00:00
|
|
|
const position = offsets.locToRange(phr.new_source, phr.locations.items(.new)[0], .@"utf-16").start;
|
2022-11-26 01:14:33 +00:00
|
|
|
|
|
|
|
const SelectionRange = struct {
|
|
|
|
range: types.Range,
|
2022-12-27 06:47:57 +00:00
|
|
|
parent: ?*@This() = null,
|
2022-11-26 01:14:33 +00:00
|
|
|
};
|
|
|
|
|
2022-12-27 06:47:57 +00:00
|
|
|
const params = types.SelectionRangeParams{
|
2022-11-26 01:14:33 +00:00
|
|
|
.textDocument = .{ .uri = test_uri },
|
2022-12-07 16:39:46 +00:00
|
|
|
.positions = &[_]types.Position{position},
|
2022-12-27 06:47:57 +00:00
|
|
|
};
|
2022-11-26 01:14:33 +00:00
|
|
|
|
2022-12-27 06:47:57 +00:00
|
|
|
const response = try ctx.requestGetResponse(?[]SelectionRange, "textDocument/selectionRange", params);
|
2022-11-26 01:14:33 +00:00
|
|
|
|
|
|
|
const selectionRanges: []SelectionRange = response.result orelse {
|
|
|
|
std.debug.print("Server returned `null` as the result\n", .{});
|
|
|
|
return error.InvalidResponse;
|
|
|
|
};
|
|
|
|
|
|
|
|
var got = std.ArrayList([]const u8).init(allocator);
|
|
|
|
defer got.deinit();
|
|
|
|
|
|
|
|
var it: ?*SelectionRange = &selectionRanges[0];
|
|
|
|
while (it) |r| {
|
2022-12-27 06:47:57 +00:00
|
|
|
const slice = offsets.rangeToSlice(phr.new_source, r.range, .@"utf-16");
|
2022-11-26 01:14:33 +00:00
|
|
|
(try got.addOne()).* = slice;
|
|
|
|
it = r.parent;
|
|
|
|
}
|
|
|
|
const last = got.pop();
|
|
|
|
try std.testing.expectEqualStrings(phr.new_source, last);
|
|
|
|
try std.testing.expectEqual(want.len, got.items.len);
|
|
|
|
for (want) |w, i| {
|
|
|
|
try std.testing.expectEqualStrings(w, got.items[i]);
|
|
|
|
}
|
|
|
|
}
|