diff --git a/README.md b/README.md index cf8227a..97070bc 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Provides the necessary building blocks to develop Language Server Protocol imple # Installation > [!NOTE] -> The default branch requires Zig `0.16.0-dev.1859+212968c57` or later. Checkout the `0.15.x` branch when using Zig 0.15 +> The default branch requires Zig `0.16.0-dev.1976+8e091047b` or later. Checkout the `0.15.x` branch when using Zig 0.15 ```bash # Initialize a `zig build` project if you haven't already diff --git a/build.zig.zon b/build.zig.zon index 7b0cde3..e2ffaf9 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -1,7 +1,7 @@ .{ .name = .lsp_kit, .version = "0.1.0", - .minimum_zig_version = "0.16.0-dev.1859+212968c57", + .minimum_zig_version = "0.16.0-dev.1976+8e091047b", .dependencies = .{}, .paths = .{ "build.zig", diff --git a/examples/hello_client.zig b/examples/hello_client.zig index 2e2dbc4..4688072 100644 --- a/examples/hello_client.zig +++ b/examples/hello_client.zig @@ -46,23 +46,11 @@ const usage = \\ ; -var debug_allocator: std.heap.DebugAllocator(.{}) = .init; - -pub fn main() !void { - const gpa, const is_debug = switch (builtin.mode) { - .Debug, .ReleaseSafe => .{ debug_allocator.allocator(), true }, - .ReleaseFast, .ReleaseSmall => .{ std.heap.smp_allocator, false }, - }; - defer if (is_debug) { - _ = debug_allocator.deinit(); - }; - - var threaded: std.Io.Threaded = .init(gpa, .{}); - defer threaded.deinit(); - const io = threaded.ioBasic(); - - const args = try std.process.argsAlloc(gpa); - defer std.process.argsFree(gpa, args); +pub fn main(init: std.process.Init) !void { + const io = init.io; + const gpa = init.gpa; + const arena = init.arena.allocator(); + const args = try init.minimal.args.toSlice(arena); if (args.len < 3) fatalWithUsage("expected at least 2 arguments but got {d}", .{args.len - 1}); @@ -71,13 +59,12 @@ pub fn main() !void { defer gpa.free(input_file); // Spawn the language server as a child process. - var child_process: std.process.Child = .init(args[2..], gpa); - child_process.stdin_behavior = .Pipe; - child_process.stdout_behavior = .Pipe; - child_process.stderr_behavior = if (show_langauge_server_stderr) .Inherit else .Ignore; - - child_process.spawn(io) catch |err| fatal("child process could not be created: {}", .{err}); - child_process.waitForSpawn() catch |err| fatal("child process could not be created: {}", .{err}); + var child_process = std.process.spawn(io, .{ + .argv = args[2..], + .stdin = .pipe, + .stdout = .pipe, + .stderr = if (show_langauge_server_stderr) .inherit else .ignore, + }) catch |err| fatal("child process could not be created: {}", .{err}); // Language servers can support multiple communication channels (e.g. stdio, pipes, sockets). // See https://microsoft.github.io/language-server-protocol/specifications/specification-current/#implementationConsiderations diff --git a/examples/hello_server.zig b/examples/hello_server.zig index 0383890..0020c02 100644 --- a/examples/hello_server.zig +++ b/examples/hello_server.zig @@ -30,21 +30,9 @@ pub const std_options: std.Options = .{ .log_level = std.log.default_level, // Customize the log level here }; -var debug_allocator: std.heap.DebugAllocator(.{}) = .init; - -pub fn main() !void { - const gpa, const is_debug = switch (builtin.mode) { - .Debug, .ReleaseSafe => .{ debug_allocator.allocator(), true }, - .ReleaseFast, .ReleaseSmall => .{ std.heap.smp_allocator, false }, - }; - defer if (is_debug) { - _ = debug_allocator.deinit(); - }; - - var threaded: std.Io.Threaded = .init(gpa, .{}); - defer threaded.deinit(); - const io = threaded.ioBasic(); - +pub fn main(init: std.process.Init) !void { + const io = init.io; + const gpa = init.gpa; // Language servers can support multiple communication channels (e.g. stdio, pipes, sockets). // See https://microsoft.github.io/language-server-protocol/specifications/specification-current/#implementationConsiderations // diff --git a/examples/my_first_server.zig b/examples/my_first_server.zig index 093bb9e..b8b0452 100644 --- a/examples/my_first_server.zig +++ b/examples/my_first_server.zig @@ -4,33 +4,19 @@ const std = @import("std"); const builtin = @import("builtin"); const lsp = @import("lsp"); -var debug_allocator: std.heap.DebugAllocator(.{}) = .init; - -pub fn main() !void { - const gpa, const is_debug = switch (builtin.mode) { - .Debug, .ReleaseSafe => .{ debug_allocator.allocator(), true }, - .ReleaseFast, .ReleaseSmall => .{ std.heap.smp_allocator, false }, - }; - defer if (is_debug) { - _ = debug_allocator.deinit(); - }; - - var threaded: std.Io.Threaded = .init(gpa, .{}); - defer threaded.deinit(); - const io = threaded.ioBasic(); - +pub fn main(init: std.process.Init) !void { // LSP implementations typically communicate over stdio (stdin and stdout) var read_buffer: [256]u8 = undefined; - var stdio_transport: lsp.Transport.Stdio = .init(io, &read_buffer, .stdin(), .stdout()); + var stdio_transport: lsp.Transport.Stdio = .init(init.io, &read_buffer, .stdin(), .stdout()); const transport: *lsp.Transport = &stdio_transport.transport; // The handler is a user provided type that stores the state of the // language server and provides callbacks for the desired LSP messages. - var handler: Handler = .init(gpa); + var handler: Handler = .init(init.gpa); defer handler.deinit(); try lsp.basic_server.run( - gpa, + init.gpa, transport, &handler, std.log.err, diff --git a/src/codegen/codegen.zig b/src/codegen/codegen.zig index cb06929..167f383 100644 --- a/src/codegen/codegen.zig +++ b/src/codegen/codegen.zig @@ -3,7 +3,7 @@ const std = @import("std"); const MetaModel = @import("MetaModel.zig"); -pub fn main() !u8 { +pub fn main(init: std.process.Init.Minimal) !u8 { var debug_allocator: std.heap.DebugAllocator(.{}) = .init; defer _ = debug_allocator.deinit(); @@ -12,7 +12,7 @@ pub fn main() !u8 { var threaded: std.Io.Threaded = .init_single_threaded; const io = threaded.ioBasic(); - var arg_it: std.process.ArgIterator = try .initWithAllocator(gpa); + var arg_it = try init.args.iterateAllocator(gpa); defer arg_it.deinit(); _ = arg_it.skip(); // skip self exe