Working with vscode I cannot jump to source (F12) or get code-completion. Is that because this ZLS is outdated? (BTW: I always get completely lost in the plethora of options and settings).
Got the writer working.
var out_buffer: [1024]u8 = undefined;
var stdout_writer = std.fs.File.stdout().writer(&out_buffer);
const out = &stdout_writer.interface;
try out.print("hello there", .{});
try out.flush();
But how to read from stdin? My 0.14 code like this:
var in: std.fs.File.Reader = std.io.getStdIn().reader()
var buffer: [4096]u8 = undefined;
const line = try in.readUntilDelimiter(&buffer, '\n');
I tried this:
var in_buffer: [1024]u8 = undefined;
var stdin_reader = std.fs.File.stdin().reader(&in_buffer);
const in = &stdin_reader.interface;
const line = try stdin_reader.readUntilDelimiter(&in_buffer, '\n');
but there is no readUntilDelimiter
(All this is a bit difficult without being able to navigate).
I have a local clone of the zls repo and occasionally pull & rebuild it when updating Zig version, and it does work with latest Zig for me. If you’re a serious Zig+ZLS user I would recommend doing the same for the time being.
As for the missing function, the Reader interface is completely different, you will have to learn the new way of doing things, but the good news is that it’s not just churn, changing where buffering happens opens the door to new possibilities compared to the past.
Go to where you keep Zig in your file system and open lib/std. The file in question in that case is lib/std/Io/Reader.zig. Sure it’s easier with zls, but it’s good to know how to consult the source code when in a pinch. The source code is the ultimate truth.
ZLS supports all 0.15 features. It checks at startup if its version version kinda matches zig version. And when version upgrades from 0.14.99999...... to 0.15.0 it thinks version is too different and refuses to work (even tho there are no changes between two). It will work in couple of days when zls team bumps version number
I’m on the current zls master (commit hash 3ed95ecd) and getting this error, that reads a little funny, when I zig build:
affine-root-system@pop-os:~/code/Zig/zls$ zig build
build.zig:725:9: error: Your Zig version does not meet the minimum build requirement:
required Zig version: 0.15.0-dev.1526+b8124d9c0 (or greater)
actual Zig version: 0.15.1
Please download or compile a tagged release of ZLS.
-> https://github.com/zigtools/zls/releases
-> https://github.com/zigtools/zls/releases/tag/0.15.1 (may not exist yet)
@compileError(message);
^~~~~~~~~~~~~~~~~~~~~~
However, I just went to build.zig and commented out the minimum build version check, and everything worked after that. Just a heads up.
const std = @import("std");
It compiles but cannot jump to std
Who delivers the “jump to source” zig? codeLDB? ZLS? vscode?
Did I forget to edit some path?
(I edited the windows path thats it).
“zig version” gives the correct result.
latest master of zls wants a “dev” version of zig, not a stable one, so it doesn’t compile atm, AFAIK. I guess it wont be long until there is a “dev” version of zig again (0.16).
Only thing is I am struggling to initialize this struct below.
Because in-out is global for my program I wanted it to make a bit convenient and centralize the needed things.
But I was not able to put these “// outside” variables inside IoContext.
How to put them inside and initialize and return properly?
(I had this problem before: creating local structs, returning and get stuck with some invalid pointer or array, because it was locally created).
pub fn lib_initialize() void {
io_context = .init();
}
pub const io: : *const IoContext = &io_context; // my global used all over the place
var io_context: IoContext = undefined; // this one is initialized
var in_buffer: [4096]u8 = undefined; // outside
var out_buffer: [4096]u8 = undefined; // outside
var stdin: std.fs.File.Reader = undefined; // outside
var stdout: std.fs.File.Writer = undefined; // outside
const IoContext = struct {
in: *std.Io.Reader,
out: *std.Io.Writer,
fn init() IoContext {
stdin = std.fs.File.stdin().reader(&in_buffer);
stdout = std.fs.File.stdout().writer(&out_buffer);
return .{
.in = &stdin.interface,
.out = &stdout.interface,
};
}
pub fn print(self: *const IoContext, comptime str: []const u8, args: anytype) !void {
try self.out.print(str, args);
try self.out.flush();
}
}
fyi: stdio files (and other fifo files) require a reader/writer in streaming mode, they can detect when they are in an invalid mode and change mode, but the result is they read/write nothing on the first call.
Use .[reader/writer]Streaming to initialise in the correct mode.