home
My favorite Zig feature
Zig has many awesome features, but there’s one that’s absolutely no. 1 for me: zls formatting features.
Example: I was adding Oniguruma C source code files to build.zig. Relevant code looked like this:
mod.addCSourceFiles(.{
.root = b.path(onig_path),
.files = &.{
"regerror.c",
"regparse.c",
"regext.c",
"regcomp.c",
"regexec.c",
"reggnu.c",
// 40 more lines
}
});
I figured - this is fine - but I don’t like having screenful of C files.
…so I joined first 3 items. First line of the array became like this:
&.{
"regerror.c", "regparse.c", "regext.c",
// ...
}
Saved. You know what zls did1? IT MADE CODE BEAUTIFUL:
mod.addCSourceFiles(.{
.root = b.path(onig_path),
.files = &.{
"regerror.c", "regparse.c", "regext.c",
"regcomp.c", "regexec.c", "reggnu.c",
"regenc.c", "regsyntax.c", "regtrav.c",
"regversion.c", "st.c", "onig_init.c",
"unicode.c", "ascii.c", "utf8.c",
"utf16_be.c", "utf16_le.c", "utf32_be.c",
"utf32_le.c", "euc_jp.c", "sjis.c",
"iso8859_1.c", "iso8859_2.c", "iso8859_3.c",
"iso8859_4.c", "iso8859_5.c", "iso8859_6.c",
"iso8859_7.c", "iso8859_8.c", "iso8859_9.c",
"iso8859_10.c", "iso8859_11.c", "iso8859_13.c",
"iso8859_14.c", "iso8859_15.c", "iso8859_16.c",
"euc_tw.c", "euc_kr.c", "big5.c",
"gb18030.c", "koi8_r.c", "cp1251.c",
"euc_jp_prop.c", "sjis_prop.c", "unicode_unfold_key.c",
"unicode_fold1_key.c", "unicode_fold2_key.c", "unicode_fold3_key.c",
},
});
Small thing, but I have a thing for nicely formatted code. By the way: zls isn’t opinionated and it formats depending on where you put commas/newlines. Both pretty and flexible.
In my mind it’s ✨zig✨ now.
-
I have auto-formatting on save enabled ↩︎
Przemysław Alexander Kamiński
vel xlii vel exlee
Powered by hugo and hugo-theme-nostyleplease.