From 3ae56929fcc695fb6fbd2c74f86feb7d4890e368 Mon Sep 17 00:00:00 2001 From: Casey Banner Date: Sun, 16 Apr 2023 12:04:42 -0400 Subject: [PATCH] Implement $/setTrace and add --enable-message-tracing (#1135) --- src/Server.zig | 15 +++++++++++++++ src/main.zig | 13 +++++++++++++ tests/context.zig | 2 +- 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/Server.zig b/src/Server.zig index 69d4caa..ac1870b 100644 --- a/src/Server.zig +++ b/src/Server.zig @@ -48,6 +48,7 @@ runtime_zig_version: ?ZigVersionWrapper, outgoing_messages: std.ArrayListUnmanaged([]const u8) = .{}, recording_enabled: bool, replay_enabled: bool, +message_tracing_enabled: bool = false, offset_encoding: offsets.Encoding = .@"utf-16", status: enum { /// the server has not received a `initialize` request @@ -450,6 +451,13 @@ fn initializeHandler(server: *Server, request: types.InitializeParams) Error!typ } } + if (request.trace) |trace| { + // To support --enable-message-tracing, only allow turning this on here + if (trace != .off) { + server.message_tracing_enabled = true; + } + } + log.info("zls initializing", .{}); log.info("{}", .{server.client_capabilities}); log.info("Using offset encoding: {s}", .{std.meta.tagName(server.offset_encoding)}); @@ -614,6 +622,10 @@ fn cancelRequestHandler(server: *Server, request: types.CancelParams) Error!void // TODO implement $/cancelRequest } +fn setTraceHandler(server: *Server, request: types.SetTraceParams) Error!void { + server.message_tracing_enabled = request.value != .off; +} + fn registerCapability(server: *Server, method: []const u8) Error!void { const allocator = server.arena.allocator(); @@ -1449,6 +1461,7 @@ pub fn processMessage(server: *Server, message: Message) Error!void { .{ "shutdown", shutdownHandler }, .{ "exit", exitHandler }, .{ "$/cancelRequest", cancelRequestHandler }, + .{ "$/setTrace", setTraceHandler }, .{ "textDocument/didOpen", openDocumentHandler }, .{ "textDocument/didChange", changeDocumentHandler }, .{ "textDocument/didSave", saveDocumentHandler }, @@ -1533,6 +1546,7 @@ pub fn create( config_path: ?[]const u8, recording_enabled: bool, replay_enabled: bool, + message_tracing_enabled: bool, ) !*Server { const server = try allocator.create(Server); server.* = Server{ @@ -1549,6 +1563,7 @@ pub fn create( .builtin_completions = null, .recording_enabled = recording_enabled, .replay_enabled = replay_enabled, + .message_tracing_enabled = message_tracing_enabled, .status = .uninitialized, }; server.analyser = Analyser.init(allocator, &server.document_store); diff --git a/src/main.zig b/src/main.zig index b3e6251..3659c3f 100644 --- a/src/main.zig +++ b/src/main.zig @@ -11,6 +11,7 @@ const Header = @import("Header.zig"); const debug = @import("debug.zig"); const logger = std.log.scoped(.zls_main); +const message_logger = std.log.scoped(.message); var actual_log_level: std.log.Level = switch (zig_builtin.mode) { .Debug => .debug, @@ -60,6 +61,7 @@ fn loop( const header = Header{ .content_length = outgoing_message.len }; try header.write(true, writer); try writer.writeAll(outgoing_message); + if (server.message_tracing_enabled) message_logger.info("sent: {s}\n", .{outgoing_message}); } try buffered_writer.flush(); for (server.outgoing_messages.items) |outgoing_message| { @@ -78,6 +80,7 @@ fn loop( try file.writeAll(json_message); } + if (server.message_tracing_enabled) message_logger.info("received: {s}\n", .{json_message}); server.processJsonRpc(json_message); if (server.status == .exiting_success or server.status == .exiting_failure) return; @@ -195,6 +198,7 @@ const ParseArgsResult = struct { config_path: ?[]const u8, replay_enabled: bool, replay_session_path: ?[]const u8, + message_tracing_enabled: bool, }; fn parseArgs(allocator: std.mem.Allocator) !ParseArgsResult { @@ -203,6 +207,7 @@ fn parseArgs(allocator: std.mem.Allocator) !ParseArgsResult { .config_path = null, .replay_enabled = false, .replay_session_path = null, + .message_tracing_enabled = false, }; const ArgId = enum { @@ -210,6 +215,7 @@ fn parseArgs(allocator: std.mem.Allocator) !ParseArgsResult { version, replay, @"enable-debug-log", + @"enable-message-tracing", @"show-config-path", @"config-path", }; @@ -236,6 +242,7 @@ fn parseArgs(allocator: std.mem.Allocator) !ParseArgsResult { .version = "Prints the compiler version with which the server was compiled.", .replay = "Replay a previous recorded zls session", .@"enable-debug-log" = "Enables debug logs.", + .@"enable-message-tracing" = "Enables message tracing.", .@"config-path" = "Specify the path to a configuration file specifying LSP behaviour.", .@"show-config-path" = "Prints the path to the configuration file to stdout", }); @@ -281,6 +288,7 @@ fn parseArgs(allocator: std.mem.Allocator) !ParseArgsResult { .help, .version, .@"enable-debug-log", + .@"enable-message-tracing", .@"show-config-path", => {}, .@"config-path" => { @@ -310,6 +318,10 @@ fn parseArgs(allocator: std.mem.Allocator) !ParseArgsResult { actual_log_level = .debug; logger.info("Enabled debug logging.\n", .{}); } + if (specified.get(.@"enable-message-tracing")) { + result.message_tracing_enabled = true; + logger.info("Enabled message tracing.\n", .{}); + } if (specified.get(.@"config-path")) { std.debug.assert(result.config_path != null); } @@ -390,6 +402,7 @@ pub fn main() !void { config.config_path, record_file != null, replay_file != null, + result.message_tracing_enabled, ); defer server.destroy(); diff --git a/tests/context.zig b/tests/context.zig index d9f0aa2..d8a1906 100644 --- a/tests/context.zig +++ b/tests/context.zig @@ -41,7 +41,7 @@ pub const Context = struct { config.* = default_config; - const server = try Server.create(allocator, config, null, false, false); + const server = try Server.create(allocator, config, null, false, false, false); errdefer server.destroy(); var context: Context = .{