A CMake code formatter written in zig

The code is here: GitHub - Waqar144/cmakefmt: A cmake formatter written in Zig

I did this project to learn zig. The existing cmake formatters are all written in python for some weird reason which makes them annoying to use/keep e.g., they break every time my distro updates.

Overall, I think Zig is nice to use and pragmatic. It took maybe a couple of hours before I was comfortably coding in the language. The things that annoyed me:

  • std.debug.print is annoying AF. Typing in .{} every single time for the second arg is frustrating. I worked around it by using editor snippets.
  • std.ArrayList() doesn’t provide a [] operator. Gets me everytime I use it. Really bad ergonomics. Lack of an emptyness checking function felt weird initially, but not a big deal.
  • Would love some way to alias a specialization of a function. Perhaps there is already a way to do it? Something like: const stringEqual = std.mem.eql(u8, _, _);
  • The anon/nested function workaround where you return a function from a struct and store it in a var is just bad with horrible ergonomics. But at least there is a way to do it.
  • Errors messages can sometimes be very confusing.
  • Test failure output is hard to read for me. It always takes me a while to find out which case is failing.
5 Likes

Welcome to ziggit!

std.ArrayList() doesn’t provide a [] operator. Gets me everytime I use it. Really bad ergonomics. Lack of an emptyness checking function felt weird initially, but not a big deal.

I would lie if I said it doesn’t get me sometimes as well. However, It makes sense. [] syntax is limited to arrays and slices. In general, once I know I’m done growing an arraylist, I’ll usually call toOwnedSlice() on it and just pass around the slice.

3 Likes

… I mostly call .items and pray nothing breaks…

1 Like

Would love some way to alias a specialization of a function. Perhaps there is already a way to do it? Something like: const stringEqual = std.mem.eql(u8, _, _);

You can achieve this with just a regular helper function:

fn stringEqual(a: []const u8, b: []const u8) bool {
    return std.mem.eql(u8, a, b);
}

std.debug.print is annoying AF. Typing in .{} every single time for the second arg is frustrating. I worked around it by using editor snippets.

Yeah, that’s the price we pay for not having function overloading. You could probably also make helper functions print0, print1, … which has other downsides of course.

You can achieve this with just a regular helper function:

Yeah, this is what I am doing atm.

Also, a println function would be nice.

If you want a newline at the end you could also use std.log.debug.

3 Likes

Good one, didn’t think of that.

That’s exactly how it’s supposed to be used.

       /// Contents of the list. This field is intended to be accessed
       /// directly.
       ///
       /// Pointers to elements in this slice are invalidated by various
       /// functions of this ArrayList in accordance with the respective
       /// documentation. In all cases, "invalidated" means that the memory
       /// has been passed to an allocator's resize or free function.
       items: Slice = &[_]T{},


mylist.items[i]

3 Likes

I also can’t commit print(…, .{}) into muscle memory. Just too many years of experience with printf (C) and print (Python). I workaround this a bit in VSCode by using a snippet for std.debug.print() which helps a bit.

1 Like

Did you use your own snippet?
zls provides std.debug.print snippet and it works everythere.

it works everythere

It only works if the text editor supports the LSP snippet format. Afaik, only vscode and neovim-coc support it fully. I use Kate (I happen to be one of its core devs), which supports a small subset of the said snippet format and thus it doesn’t work that well.

I use it from zls on VSCode. Not that I enjoy using snippets for something like this as it feels to me more like a language ergonomics issue than an editor/muscle memory thing. But I’m probably in the minority with this opinion.

It worked for me in helix for some time and they addes snippet support in helix only resently. So it worked even without snippets support

If it worked, then helix supported lsp completion snippets before as well. Otherwise you’d see weird stuff or just nothing at all. zls/src/snippets.zig at 9fc45caeafe3136070ea7abcf2f28addec069b5d · zigtools/zls · GitHub

prior to 25.x snippet support was a hack that ignored all the snippet features, after 25.x its has actual snippet support