zls/tests/context.zig

201 lines
11 KiB
Zig
Raw Permalink Normal View History

2022-08-01 19:25:22 +01:00
const std = @import("std");
const zls = @import("zls");
const builtin = @import("builtin");
const tres = @import("tres");
const Header = zls.Header;
const Config = zls.Config;
const Server = zls.Server;
2022-09-18 23:47:06 +01:00
const types = zls.types;
2022-08-01 19:25:22 +01:00
/// initialize request taken from Visual Studio Code with the following changes:
/// - removed locale, rootPath, rootUri, trace, workspaceFolders
/// - removed capabilities.workspace.configuration
/// - removed capabilities.workspace.didChangeConfiguration
/// - removed capabilities.textDocument.publishDiagnostics
2022-08-01 19:25:22 +01:00
const initialize_msg =
\\{"processId":0,"clientInfo":{"name":"Visual Studio Code","version":"1.73.1"},"capabilities":{"workspace":{"applyEdit":true,"workspaceEdit":{"documentChanges":true,"resourceOperations":["create","rename","delete"],"failureHandling":"textOnlyTransactional","normalizesLineEndings":true,"changeAnnotationSupport":{"groupsOnLabel":true}},"didChangeWatchedFiles":{"dynamicRegistration":true,"relativePatternSupport":true},"symbol":{"dynamicRegistration":true,"symbolKind":{"valueSet":[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]},"tagSupport":{"valueSet":[1]},"resolveSupport":{"properties":["location.range"]}},"codeLens":{"refreshSupport":true},"executeCommand":{"dynamicRegistration":true},"workspaceFolders":true,"semanticTokens":{"refreshSupport":true},"fileOperations":{"dynamicRegistration":true,"didCreate":true,"didRename":true,"didDelete":true,"willCreate":true,"willRename":true,"willDelete":true},"inlineValue":{"refreshSupport":true},"inlayHint":{"refreshSupport":true},"diagnostics":{"refreshSupport":true}},"textDocument":{"synchronization":{"dynamicRegistration":true,"willSave":true,"willSaveWaitUntil":true,"didSave":true},"completion":{"dynamicRegistration":true,"contextSupport":true,"completionItem":{"snippetSupport":true,"commitCharactersSupport":true,"documentationFormat":["markdown","plaintext"],"deprecatedSupport":true,"preselectSupport":true,"tagSupport":{"valueSet":[1]},"insertReplaceSupport":true,"resolveSupport":{"properties":["documentation","detail","additionalTextEdits"]},"insertTextModeSupport":{"valueSet":[1,2]},"labelDetailsSupport":true},"insertTextMode":2,"completionItemKind":{"valueSet":[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]},"completionList":{"itemDefaults":["commitCharacters","editRange","insertTextFormat","insertTextMode"]}},"hover":{"dynamicRegistration":true,"contentFormat":["markdown","plaintext"]},"signatureHelp":{"dynamicRegistration":true,"signatureInformation":{"documentationFormat":["markdown","plaintext"],"parameterInformation":{"labelOffsetSupport":true},"activeParameterSupport":true},"contextSupport":true},"definition":{"dynamicRegistration":true,"linkSupport":true},"references":{"dynamicRegistration":true},"documentHighlight":{"dynamicRegistration":true},"documentSymbol":{"dynamicRegistration":true,"symbolKind":{"valueSet":[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]},"hierarchicalDocumentSymbolSupport":true,"tagSupport":{"valueSet":[1]},"labelSupport":true},"codeAction":{"dynamicRegistration":true,"isPreferredSupport":true,"disabledSupport":true,"dataSupport":true,"resolveSupport":{"properties":["edit"]},"codeActionLiteralSupport":{"codeActionKind":{"valueSet":["","quickfix","refactor","refactor.extract","refactor.inline","refactor.rewrite","source","source.organizeImports"]}},"honorsChangeAnnotations":false},"codeLens":{"dynamicRegistration":true},"formatting":{"dynamicRegistration":true},"rangeFormatting":{"dynamicRegistration":true},"onTypeFormatting":{"dynamicRegistration":true},"rename":{"dynamicRegistration":true,"prepareSupport":true,"prepareSupportDefaultBehavior":1,"honorsChangeAnnotations":true},"documentLink":{"dynamicRegistration":true,"tooltipSupport":true},"typeDefinition":{"dynamicRegistration":true,"linkSupport":true},"implementation":{"dynamicRegistration":true,"linkSupport":true},"colorProvider":{"dynamicRegistration":true},"foldingRange":{"dynamicRegistration":true,"rangeLimit":5000,"lineFoldingOnly":true,"foldingRangeKind":{"valueSet":["comment","imports","region"]},"foldingRange":{"collapsedText":false}},"declaration":{"dynamicRegistration":true,"linkSupport":true},"selectionRange":{"dynamicRegistration":true},"callHierarchy":{"dynamicRegistration":true},"semanticTokens":{"dynamicRegistration":true,"tokenTypes":["namespace","type","class","enum","interface","struct","typeParameter","parameter","variable","property","enumMember","event","function","method","macro","keyword","modifier","comment","string","number","regexp","operator","decorator"],"tokenModifiers":["declaration","definiti
2022-08-01 19:25:22 +01:00
;
const default_config: Config = .{
.enable_ast_check_diagnostics = false,
.semantic_tokens = .full,
.enable_inlay_hints = true,
.inlay_hints_exclude_single_argument = false,
.inlay_hints_show_builtin = true,
};
2022-08-01 19:25:22 +01:00
const allocator = std.testing.allocator;
pub const Context = struct {
server: *Server,
arena: std.heap.ArenaAllocator,
config: *Config,
2022-08-01 19:25:22 +01:00
request_id: u32 = 1,
file_id: u32 = 0,
2022-08-01 19:25:22 +01:00
pub fn init() !Context {
var config = try allocator.create(Config);
errdefer allocator.destroy(config);
config.* = default_config;
const server = try Server.create(allocator, config, null, false, false, false);
errdefer server.destroy();
var context: Context = .{
.server = server,
.arena = std.heap.ArenaAllocator.init(allocator),
.config = config,
2022-08-01 19:25:22 +01:00
};
try context.request("initialize", initialize_msg, null);
try context.notification("initialized", "{}");
// TODO this line shouldn't be needed
context.server.client_capabilities.label_details_support = false;
2022-08-01 19:25:22 +01:00
return context;
}
pub fn deinit(self: *Context) void {
2023-06-21 16:47:58 +01:00
zls.legacy_json.parseFree(Config, allocator, self.config.*);
allocator.destroy(self.config);
2022-08-01 19:25:22 +01:00
self.request("shutdown", "{}", null) catch {};
self.server.destroy();
self.arena.deinit();
2022-08-01 19:25:22 +01:00
}
pub fn notification(
2022-08-01 19:25:22 +01:00
self: *Context,
method: []const u8,
params: []const u8,
) !void {
2022-08-23 11:44:26 +01:00
var output = std.ArrayListUnmanaged(u8){};
defer output.deinit(allocator);
2022-08-01 19:25:22 +01:00
// create the request
const req = try std.fmt.allocPrint(allocator,
\\{{"jsonrpc":"2.0","method":"{s}","params":{s}}}
, .{ method, params });
defer allocator.free(req);
// send the request to the server
self.server.processJsonRpc(req);
for (self.server.outgoing_messages.items) |outgoing_message| {
self.server.allocator.free(outgoing_message);
}
self.server.outgoing_messages.clearRetainingCapacity();
}
pub fn requestAlloc(
self: *Context,
method: []const u8,
params: []const u8,
) ![]const u8 {
2022-08-01 19:25:22 +01:00
// create the request
self.request_id += 1;
const req = try std.fmt.allocPrint(allocator,
\\{{"jsonrpc":"2.0","id":{},"method":"{s}","params":{s}}}
, .{ self.request_id, method, params });
defer allocator.free(req);
// send the request to the server
self.server.processJsonRpc(req);
2022-08-01 19:25:22 +01:00
const messages = self.server.outgoing_messages.items;
2022-08-01 19:25:22 +01:00
try std.testing.expect(messages.len != 0);
2022-08-01 19:25:22 +01:00
for (messages[0..(messages.len - 1)]) |message| {
self.server.allocator.free(message);
}
defer self.server.outgoing_messages.clearRetainingCapacity();
2022-08-01 19:25:22 +01:00
return messages[messages.len - 1];
}
pub fn request(
self: *Context,
method: []const u8,
params: []const u8,
expect: ?[]const u8,
) !void {
const response_bytes = try self.requestAlloc(method, params);
defer self.server.allocator.free(response_bytes);
const expected = expect orelse return;
2023-06-21 16:24:29 +01:00
var tree = try std.json.parseFromSlice(std.json.Value, allocator, response_bytes, .{});
2022-08-01 19:25:22 +01:00
defer tree.deinit();
2023-06-21 16:24:29 +01:00
const response = tree.value.object;
2022-08-01 19:25:22 +01:00
// assertions
try std.testing.expectEqualStrings("2.0", response.get("jsonrpc").?.string);
try std.testing.expectEqual(self.request_id, @intCast(u32, response.get("id").?.integer));
2022-08-01 19:25:22 +01:00
try std.testing.expect(!response.contains("error"));
const result_json = try std.json.stringifyAlloc(allocator, response.get("result").?, .{});
defer allocator.free(result_json);
try std.testing.expectEqualStrings(expected, result_json);
}
2022-09-18 23:47:06 +01:00
// helper
pub fn addDocument(self: *Context, source: []const u8) ![]const u8 {
const fmt = switch (builtin.os.tag) {
.windows => "file:///C:\\test-{d}.zig",
else => "file:///test-{d}.zig",
};
const uri = try std.fmt.allocPrint(
self.arena.allocator(),
fmt,
.{self.file_id},
);
const open_document = types.DidOpenTextDocumentParams{
.textDocument = .{
.uri = uri,
.languageId = "zig",
.version = 420,
.text = source,
2022-09-18 23:47:06 +01:00
},
};
const params = try std.json.stringifyAlloc(allocator, open_document, .{});
2022-09-18 23:47:06 +01:00
defer allocator.free(params);
try self.notification("textDocument/didOpen", params);
self.file_id += 1;
return uri;
2022-09-18 23:47:06 +01:00
}
pub fn Response(comptime Result: type) type {
return struct {
jsonrpc: []const u8,
id: types.RequestId,
result: Result,
};
}
pub fn requestGetResponse(self: *Context, comptime Result: type, method: []const u8, params: anytype) !Response(Result) {
var buffer = std.ArrayListUnmanaged(u8){};
defer buffer.deinit(allocator);
2022-09-18 23:47:06 +01:00
try tres.stringify(params, .{}, buffer.writer(allocator));
2022-09-18 23:47:06 +01:00
const response_bytes = try self.requestAlloc(method, buffer.items);
defer self.server.allocator.free(response_bytes);
2022-09-18 23:47:06 +01:00
2023-06-21 16:24:29 +01:00
var tree = try std.json.parseFromSlice(std.json.Value, self.arena.allocator(), try self.arena.allocator().dupe(u8, response_bytes), .{});
2022-09-18 23:47:06 +01:00
// TODO validate jsonrpc and id
2023-06-21 16:24:29 +01:00
return tres.parse(Response(Result), tree.value, self.arena.allocator());
2022-09-18 23:47:06 +01:00
}
2022-08-03 22:29:03 +01:00
};