Skip to content

Commit 922e9c2

Browse files
committed
fix compile errors in transport usage
1 parent 053cced commit 922e9c2

5 files changed

Lines changed: 36 additions & 18 deletions

File tree

examples/hello_client.zig

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pub fn main(init: std.process.Init) !void {
7171
//
7272
// The `lsp.Transport.Stdio` implements the necessary logic to read and write messages over stdio.
7373
var read_buffer: [256]u8 = undefined;
74-
var stdio_transport: lsp.Transport.Stdio = .init(io, &read_buffer, .{ .handle = child_process.stdout.?.handle }, child_process.stdin.?);
74+
var stdio_transport: lsp.Transport.Stdio = .init(&read_buffer, .{ .handle = child_process.stdout.?.handle }, child_process.stdin.?);
7575
const transport: *lsp.Transport = &stdio_transport.transport;
7676

7777
// The order of exchanged messages will look similar to this:
@@ -84,6 +84,7 @@ pub fn main(init: std.process.Init) !void {
8484

8585
std.log.debug("sending 'initialize' request to server", .{});
8686
try transport.writeRequest(
87+
io,
8788
gpa,
8889
.{ .number = 0 },
8990
"initialize", // https://microsoft.github.io/language-server-protocol/specifications/specification-current/#initialize
@@ -95,7 +96,7 @@ pub fn main(init: std.process.Init) !void {
9596
// Wait for the response from the server
9697
// For the sake of simplicity, we will block here and read messages until the response to our request has been found. All other messages will be ignored.
9798
// A more sophisticated client implementation will need to handle messages asynchronously.
98-
const initialize_response = try readAndIgnoreUntilResponse(gpa, transport, .{ .number = 0 }, "initialize");
99+
const initialize_response = try readAndIgnoreUntilResponse(io, gpa, transport, .{ .number = 0 }, "initialize");
99100
defer initialize_response.deinit();
100101

101102
const initialize_result: lsp.types.InitializeResult = initialize_response.value;
@@ -120,6 +121,7 @@ pub fn main(init: std.process.Init) !void {
120121

121122
std.log.debug("sending 'initialized' notification to server", .{});
122123
try transport.writeNotification(
124+
io,
123125
gpa,
124126
"initialized", // https://microsoft.github.io/language-server-protocol/specifications/specification-current/#initialized
125127
lsp.types.InitializedParams,
@@ -132,6 +134,7 @@ pub fn main(init: std.process.Init) !void {
132134
std.log.info("This document recently came in by the CLI.", .{});
133135
std.log.debug("sending 'textDocument/didOpen' notification to server", .{});
134136
try transport.writeNotification(
137+
io,
135138
gpa,
136139
"textDocument/didOpen", // https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_didOpen
137140
lsp.types.TextDocument.DidOpenParams,
@@ -149,6 +152,7 @@ pub fn main(init: std.process.Init) !void {
149152
std.log.info("Just to double check, could you verify that it is formatted correctly?", .{});
150153
std.log.debug("sending 'textDocument/formatting' request to server", .{});
151154
try transport.writeRequest(
155+
io,
152156
gpa,
153157
.{ .number = 1 },
154158
"textDocument/formatting", // https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_formatting
@@ -160,7 +164,7 @@ pub fn main(init: std.process.Init) !void {
160164
.{ .emit_null_optional_fields = false },
161165
);
162166

163-
const formatting_response = try readAndIgnoreUntilResponse(gpa, transport, .{ .number = 1 }, "textDocument/formatting");
167+
const formatting_response = try readAndIgnoreUntilResponse(io, gpa, transport, .{ .number = 1 }, "textDocument/formatting");
164168
defer formatting_response.deinit();
165169

166170
const text_edits = formatting_response.value orelse &.{};
@@ -177,6 +181,7 @@ pub fn main(init: std.process.Init) !void {
177181
// Even though this is a request, we do not wait for a response because we are going to close the server anyway.
178182
std.log.debug("sending 'shutdown' request to server", .{});
179183
try transport.writeRequest(
184+
io,
180185
gpa,
181186
.{ .number = 2 },
182187
"shutdown", // https://microsoft.github.io/language-server-protocol/specifications/specification-current/#shutdown
@@ -187,6 +192,7 @@ pub fn main(init: std.process.Init) !void {
187192

188193
std.log.debug("sending 'exit' notification to server", .{});
189194
try transport.writeNotification(
195+
io,
190196
gpa,
191197
"exit", // https://microsoft.github.io/language-server-protocol/specifications/specification-current/#exit
192198
void,
@@ -215,14 +221,15 @@ fn fatal(comptime format: []const u8, args: anytype) noreturn {
215221

216222
/// Do not use such a function in an actual implementation.
217223
fn readAndIgnoreUntilResponse(
224+
io: std.Io,
218225
allocator: std.mem.Allocator,
219226
transport: *lsp.Transport,
220227
id: lsp.JsonRPCMessage.ID,
221228
comptime method: []const u8,
222229
) !std.json.Parsed(lsp.ResultType(method)) {
223230
while (true) {
224231
// read the unparsed JSON-RPC message
225-
const json_message = try transport.readJsonMessage(allocator);
232+
const json_message = try transport.readJsonMessage(io, allocator);
226233
defer allocator.free(json_message);
227234
std.log.debug("received message from server: {s}", .{json_message});
228235

examples/hello_server.zig

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub fn main(init: std.process.Init) !void {
3838
//
3939
// The `lsp.Transport.Stdio` implements the necessary logic to read and write messages over stdio.
4040
var read_buffer: [256]u8 = undefined;
41-
var stdio_transport: lsp.Transport.Stdio = .init(io, &read_buffer, .stdin(), .stdout());
41+
var stdio_transport: lsp.Transport.Stdio = .init(&read_buffer, .stdin(), .stdout());
4242
const transport: *lsp.Transport = &stdio_transport.transport;
4343

4444
// keep track of opened documents
@@ -51,7 +51,7 @@ pub fn main(init: std.process.Init) !void {
5151

5252
while (true) {
5353
// read the unparsed JSON-RPC message
54-
const json_message = try transport.readJsonMessage(gpa);
54+
const json_message = try transport.readJsonMessage(io, gpa);
5555
defer gpa.free(json_message);
5656
// std.log.debug("received message from client: {s}", .{json_message});
5757

@@ -90,6 +90,7 @@ pub fn main(init: std.process.Init) !void {
9090
.initialize => |params| {
9191
_ = params.capabilities; // the client capabilities tell the server what "features" the client supports
9292
try transport.writeResponse(
93+
io,
9394
gpa,
9495
request.id,
9596
lsp.types.InitializeResult,
@@ -105,11 +106,11 @@ pub fn main(init: std.process.Init) !void {
105106
.{ .emit_null_optional_fields = false },
106107
);
107108
},
108-
.shutdown => try transport.writeResponse(gpa, request.id, void, {}, .{}),
109+
.shutdown => try transport.writeResponse(io, gpa, request.id, void, {}, .{}),
109110
.@"textDocument/formatting" => |params| {
110111
const source = documents.get(params.textDocument.uri) orelse {
111112
// We should read the document from the file system
112-
try transport.writeResponse(gpa, request.id, void, {}, .{});
113+
try transport.writeResponse(io, gpa, request.id, void, {}, .{});
113114
continue;
114115
};
115116
const source_z = try gpa.dupeZ(u8, source);
@@ -119,15 +120,15 @@ pub fn main(init: std.process.Init) !void {
119120
defer tree.deinit(gpa);
120121

121122
if (tree.errors.len != 0) {
122-
try transport.writeResponse(gpa, request.id, void, {}, .{});
123+
try transport.writeResponse(io, gpa, request.id, void, {}, .{});
123124
continue;
124125
}
125126

126127
const formatte_source = try tree.renderAlloc(gpa);
127128
defer gpa.free(formatte_source);
128129

129130
if (std.mem.eql(u8, source, formatte_source)) {
130-
try transport.writeResponse(gpa, request.id, void, {}, .{});
131+
try transport.writeResponse(io, gpa, request.id, void, {}, .{});
131132
continue;
132133
}
133134

@@ -139,9 +140,9 @@ pub fn main(init: std.process.Init) !void {
139140
.newText = formatte_source,
140141
}};
141142

142-
try transport.writeResponse(gpa, request.id, []const lsp.types.TextEdit, result, .{});
143+
try transport.writeResponse(io, gpa, request.id, []const lsp.types.TextEdit, result, .{});
143144
},
144-
.other => try transport.writeResponse(gpa, request.id, void, {}, .{}),
145+
.other => try transport.writeResponse(io, gpa, request.id, void, {}, .{}),
145146
},
146147
.notification => |notification| switch (notification.params) {
147148
.initialized => {},

examples/my_first_server.zig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const lsp = @import("lsp");
77
pub fn main(init: std.process.Init) !void {
88
// LSP implementations typically communicate over stdio (stdin and stdout)
99
var read_buffer: [256]u8 = undefined;
10-
var stdio_transport: lsp.Transport.Stdio = .init(init.io, &read_buffer, .stdin(), .stdout());
10+
var stdio_transport: lsp.Transport.Stdio = .init(&read_buffer, .stdin(), .stdout());
1111
const transport: *lsp.Transport = &stdio_transport.transport;
1212

1313
// The handler is a user provided type that stores the state of the
@@ -16,6 +16,7 @@ pub fn main(init: std.process.Init) !void {
1616
defer handler.deinit();
1717

1818
try lsp.basic_server.run(
19+
init.io,
1920
init.gpa,
2021
transport,
2122
&handler,

src/basic_server.zig

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const offsets = lsp.offsets;
1414
const types = lsp.types;
1515

1616
pub fn run(
17+
io: std.Io,
1718
allocator: std.mem.Allocator,
1819
transport: *lsp.Transport,
1920
/// Must be a pointer to a container type (e.g. `struct`) that implements
@@ -59,7 +60,7 @@ pub fn run(
5960
comptime std.debug.assert(@hasDecl(Handler, "initialize") or @hasField(Handler, "initialize")); // The 'initialize' function cannot be omitted.
6061

6162
while (true) {
62-
const json_message = try transport.readJsonMessage(allocator);
63+
const json_message = try transport.readJsonMessage(io, allocator);
6364
defer allocator.free(json_message);
6465

6566
var arena_allocator: std.heap.ArenaAllocator = .init(allocator);
@@ -74,6 +75,7 @@ pub fn run(
7475
) catch |err| {
7576
if (logErr) |log| log("Failed to handle message: {}", .{err});
7677
try transport.writeErrorResponse(
78+
io,
7779
allocator,
7880
null,
7981
.{ .code = .parse_error, .message = @errorName(err) },
@@ -96,6 +98,7 @@ pub fn run(
9698
, .{ Handler, std.zig.fmtId(method), lsp.ResultType(method), @TypeOf(result) }));
9799
}
98100
try transport.writeResponse(
101+
io,
99102
allocator,
100103
request.id,
101104
lsp.ResultType(method),
@@ -138,6 +141,7 @@ pub fn run(
138141
}
139142

140143
try transport.writeErrorResponse(
144+
io,
141145
allocator,
142146
request.id,
143147
.{ .code = code, .message = @errorName(err) },
@@ -147,6 +151,7 @@ pub fn run(
147151
},
148152
.other => {
149153
try transport.writeResponse(
154+
io,
150155
allocator,
151156
request.id,
152157
?void,

src/lsp.zig

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,6 +1167,7 @@ pub const Transport = struct {
11671167

11681168
pub fn writeRequest(
11691169
transport: *Transport,
1170+
io: std.Io,
11701171
allocator: std.mem.Allocator,
11711172
id: JsonRPCMessage.ID,
11721173
method: []const u8,
@@ -1181,11 +1182,12 @@ pub const Transport = struct {
11811182
};
11821183
const json_message = try std.json.Stringify.valueAlloc(allocator, request, options);
11831184
defer allocator.free(json_message);
1184-
try transport.writeJsonMessage(json_message);
1185+
try transport.writeJsonMessage(io, json_message);
11851186
}
11861187

11871188
pub fn writeNotification(
11881189
transport: *Transport,
1190+
io: std.Io,
11891191
allocator: std.mem.Allocator,
11901192
method: []const u8,
11911193
comptime Params: type,
@@ -1198,11 +1200,12 @@ pub const Transport = struct {
11981200
};
11991201
const json_message = try std.json.Stringify.valueAlloc(allocator, request, options);
12001202
defer allocator.free(json_message);
1201-
try transport.writeJsonMessage(json_message);
1203+
try transport.writeJsonMessage(io, json_message);
12021204
}
12031205

12041206
pub fn writeResponse(
12051207
transport: *Transport,
1208+
io: std.Io,
12061209
allocator: std.mem.Allocator,
12071210
id: ?JsonRPCMessage.ID,
12081211
comptime Result: type,
@@ -1215,11 +1218,12 @@ pub const Transport = struct {
12151218
};
12161219
const json_message = try std.json.Stringify.valueAlloc(allocator, request, options);
12171220
defer allocator.free(json_message);
1218-
try transport.writeJsonMessage(json_message);
1221+
try transport.writeJsonMessage(io, json_message);
12191222
}
12201223

12211224
pub fn writeErrorResponse(
12221225
transport: *Transport,
1226+
io: std.Io,
12231227
allocator: std.mem.Allocator,
12241228
id: ?JsonRPCMessage.ID,
12251229
err: JsonRPCMessage.Response.Error,
@@ -1231,7 +1235,7 @@ pub const Transport = struct {
12311235
};
12321236
const json_message = try std.json.Stringify.valueAlloc(allocator, request, options);
12331237
defer allocator.free(json_message);
1234-
try transport.writeJsonMessage(json_message);
1238+
try transport.writeJsonMessage(io, json_message);
12351239
}
12361240
};
12371241

0 commit comments

Comments
 (0)