diff --git a/src/rpc.zig b/src/rpc.zig index 704131e..935f1c9 100644 --- a/src/rpc.zig +++ b/src/rpc.zig @@ -71,7 +71,7 @@ pub fn RpcClientType( const MethodHashMap = std.StringHashMap(Method); pub const TransType: type = switch (client_tag) { - .pipe, .stdio => std.fs.File, + .pipe => std.fs.File, .socket => std.net.Stream, }; diff --git a/src/tools.zig b/src/tools.zig index 093527c..0f5c10a 100644 --- a/src/tools.zig +++ b/src/tools.zig @@ -8,8 +8,6 @@ const named_pipe = @import("named_pipe.zig"); /// client type pub const ClientType = enum { - /// for stdio on unix-* - stdio, // for windows pipe, /// for all playform @@ -70,7 +68,7 @@ const listenErrors = error{ // listenPipe for windows pub fn listen(comptime client_tag: ClientType, pl: anytype) !bool { const trans = switch (client_tag) { - .pipe, .stdio => @as(std.fs.File, pl), + .pipe => @as(std.fs.File, pl), .socket => @as(std.net.Stream, pl), }; switch (comptime builtin.target.os.tag) { diff --git a/src/znvim.zig b/src/znvim.zig index 3105749..b337df7 100644 --- a/src/znvim.zig +++ b/src/znvim.zig @@ -56,10 +56,6 @@ pub fn Client( comptime user_data: type, comptime delay_time: u64, ) type { - if (builtin.os.tag == .windows and client_tag == .stdio) { - @compileError("for windows, we only can use .pipe or .socket!"); - } - return struct { /// znvim type self const Self = @This(); diff --git a/test/test.zig b/test/test.zig index 28cabd2..d85142f 100644 --- a/test/test.zig +++ b/test/test.zig @@ -2,6 +2,10 @@ const std = @import("std"); const znvim = @import("znvim"); test "basic embed connect" { + // create a client type + // .socket is just for unix-socket and socket (tcp) + // others use .pipe + // u32 is the user data type, recommend to use pointer type const ClientType = znvim.defaultClient(.pipe, u32); const args = [_][]const u8{ "nvim", "--embed", "--headless", "-u", "NONE" }; @@ -9,16 +13,23 @@ test "basic embed connect" { var nvim = try create_nvim_process(std.testing.allocator, &args, true); defer _ = nvim.kill() catch unreachable; + // init the client var client = try ClientType.init( nvim.stdin.?, nvim.stdout.?, std.testing.allocator, ); + // do not forget deinit client defer client.deinit(); + // texit client defer client.exit(); + // run client event loop try client.loop(); + // get neovim api infos + // when you call this function + // this will enable api check on debug mode try client.getApiInfo(); } @@ -42,9 +53,14 @@ test "call function" { try client.loop(); try client.getApiInfo(); + // make a params + // we no need to free params manually + // znvim will do this automatically const params = try client.paramArr(0); + // call api, and current thread will be blocked utill response comes const res = try client.call("nvim_get_current_buf", params); + // note: we must free result manually defer client.freeResultType(res); try std.testing.expect(res.result.ext.data[0] == 1); @@ -70,19 +86,27 @@ test "notify function" { try client.loop(); try client.getApiInfo(); + // make a params + // this is a 4 length array, in lua this is a list or array + // we no need to free params manually + // znvim will do this automatically var params = try client.paramArr(3); + // arrary zero element const param_0 = try client.paramStr("hello, world!"); + // array first element const param_1 = client.paramUint(1); + // array second element const param_2 = try client.paramArr(0); + // set element try params.setArrElement(0, param_0); try params.setArrElement(1, param_1); try params.setArrElement(2, param_2); - // try params + // notify will not block current thread, it will return immediately _ = try client.notify("nvim_notify", params); } @@ -101,6 +125,7 @@ test "socket connect test" { defer _ = nvim.kill() catch unreachable; const stream: std.net.Stream = while (true) { + // for unix-socket, we can use `std.net.connectUnixSocket` const res = std.net.tcpConnectToHost(std.testing.allocator, address, port) catch { continue; };