I really like “opinionated” code formatters like Black, Prettier or StyLua. They make it very easy to get code formatted in a consistent and readable way without having to bikeshed or configure anything.
I wish there was something like this for Nim. Nimpretty is a thing, but it doesn't seem to do much — just wraps long lines at the nearest space, often in a not very logical place.
I tried to look at Nimpretty's source code to see if I could extend it, but just like the rest of the compiler, it seems to be beyond my understanding.
For example, consider this code:
foofoofoofoofoofoofoofoofoofoo(barbarbarbarbarbarbarbarbarbar, bazbazbazbazbazbazbazbazbazbaz)
Black will automatically change it to:
foofoofoofoofoofoofoofoofoofoo(
barbarbarbarbarbarbarbarbarbar, bazbazbazbazbazbazbazbazbazbaz
)
Nimpretty doesn't do anything.
Myself I want this style of formatting:
logMessage(myLogFile,
error,
"This is a test log message)
If the lines go too long (over 80 chars):
logMessage(
myLogFile,
error,
"This is a test log message)
Just the option for this style.
I'm not sure which of these properties hold for Nimpretty, I'll try to find counterexamples.
Nimpretty doesn't do that by design, it assumes that if you took the effort to type in newlines, it's because they are meaningful to you and that you want it this way.
The problem with this is that you will end up with inconsistent formatting.
I do also wish for a Black for Nim.
As a side note I've also grown to dislike this style:
logMessage(myLogFile,
error,
"This is a test log message")
This should be formatted as:
logMessage(
myLogFile,
error,
"This is a test log message"
)
Anything wrong with nimpretty **.nim?
Yes, it simply does not work:
> nimpretty **.nim
Error: cannot open file: C:\p\steamworks\tests\**.nim
I think ** its a shell expansion which windows does not have.
Not sure if it's helpful, but on Windows you have PowerShell which allows for all sorts of cool things:
$ ls -R tests *.nim | %{nimpretty $_}
or, if you prefer the ** syntax:
$ ls tests/**/**.nim | %{nimpretty $_}
You can parallelize that too to speed up formatting:
$ ls -R tests *.nim | % -Parallel {nimpretty $_}
I believe Zsh pioneered ** back in the mid 1990s but it was a long time before you could say **.nim instead of **/*.nim. (Then it was a very short time to grow triple-* to chase symlinks.)
It surprised me how much more I liked double-* vs **/*. ** feels much more than twice as keystroke efficient. Not that such is easy to define/measure or the end-all/be-all or that @moigagoo's **/** expressions cannot be saved in a file. It's just a strange little case where I wonder if "Am I the only one who thinks this?". (Yes, yes..Slightly off topic but related to identifying the many little efficiencies that might improve life - eternally relevant, lol.)
@moigagoo's / expressions cannot be saved in a file.
I don't get it. Yes they can.
nimfmt is based on nimpretty and add a couple of features.
The most important one is detecting is the same variable or proc appears with inconsistent style (myVar, my_var, myvar) and optionally fix it.
It can be configured to prefer a specify style or follow the style that appears more frequently in the codebase.
Style insensitivity has always been a hot topic, so this is my 2 cents to improve readability.
I'm happy to receive PRs and add more features.
** only works in BASH if globstar is enabled:
$ shopt -s globstar
It seems to work in fish shell by default.