dfmt icon indicating copy to clipboard operation
dfmt copied to clipboard

Line break too early

Open qznc opened this issue 9 years ago • 3 comments

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.

qznc avatar Aug 19 '15 22:08 qznc

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"?

qznc avatar Aug 25 '15 17:08 qznc

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.

qznc avatar Aug 25 '15 18:08 qznc

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.

Hackerpilot avatar Aug 25 '15 22:08 Hackerpilot