Skip to content

Commit

Permalink
remove useless code and add some guidence
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhongjia committed Jun 8, 2024
1 parent 80b8ddf commit aff7287
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/rpc.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down
4 changes: 1 addition & 3 deletions src/tools.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 0 additions & 4 deletions src/znvim.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
27 changes: 26 additions & 1 deletion test/test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,34 @@ 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" };

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();
}

Expand All @@ -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);
Expand All @@ -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);
}

Expand All @@ -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;
};
Expand Down

0 comments on commit aff7287

Please sign in to comment.