dfmt
dfmt copied to clipboard
Line break too early
Another dfmt dog fooding issue.
class FormatVisitor
{
void visit()
{
if (unary.prefix.type == tok!"~" || unary.prefix.type == tok!"&"
|| unary.prefix.type == tok!"*" || unary.prefix.type == tok!"+"
|| unary.prefix.type == tok!"-")
{
}
}
}
via dfmt becomes
class FormatVisitor
{
void visit()
{
if (unary.prefix.type == tok!"~" || unary.prefix.type == tok!"&"
|| unary.prefix.type == tok!"*"
|| unary.prefix.type == tok!"+" || unary.prefix.type == tok!"-")
{
}
}
}
but should stay unchanged.
After thinking it through, it is questionable, if we should really prefer the first code snippet. According to dfmt's cost model, they are equivalent. Same number of newline, all lines shorter than 80 characters.
Intuitively, I prefer the first snippet, but how do we model this as "costs"? Maybe "Earlier line breaks are better"?
Here is another possibility:
class FormatVisitor
{
void visit()
{
if (unary.prefix.type == tok!"~" || unary.prefix.type == tok!"&" || unary.prefix.type == tok!"*"
|| unary.prefix.type == tok!"+" || unary.prefix.type == tok!"-")
{
}
}
}
I only changed the sort order heuristic of the search, but not the cost model. It is one overlong line (105 characters), but one less newline.
Did that change break any of the other tests? I don't see much of a problem with it, but we could end up tweaking the weight calculation forever.