ormolu icon indicating copy to clipboard operation
ormolu copied to clipboard

Hang function applications

Open judah opened this issue 4 years ago • 7 comments

It would be nice for Ormolu to "hang" function applications when possible:

main = f
  x

instead of (as it does now)

main =
  f
    x

I apologize if this was already covered by another ticket; I didn't see it in any open ones. It does seem like the recent #492 affected this behavior (from looking at the diff in the tests) for several kinds of terms. In the absence of comments, hanging applications could be a good way to help reduce vertical space.

judah avatar Feb 13 '20 20:02 judah

It is not obvious to me why this would be "nice". Sure, it saves a bit of vertical space. But vertical space is not as precious as horizontal space because we already accept the necessity for vertical scrolling. And writing Haskell you already often do a lot in little vertical space compared to other languages. On the other hand, I do not like about

main = f
  x

that there is a stronger visual connection between main and f than f and x.

prednaz avatar Mar 01 '20 22:03 prednaz

@prednaz I disagree that "vertical space is not as precious as horizontal space" is universally true. Rather, I think it's a tradeoff. Just because you can scroll vertically doesn't mean it's not better to be able to view larger chunks of a function at once. And indeed, Ormolu by design lets the user make decisions about some of this behavior.

Also, as a side note for the motivation of this ticket, part of the problem is that our team already has code in this format, and Ormolu transforms it to something more verbose.

main =
  f
    x

That indentation seems (to me) to be strictly worse than any of the other options, including either keep it on one line, or breaking between = and f:

main =
  f x

which would have been acceptable to Ormolu.

So even if Ormolu doesn't support hanging indents, it would be more helpful for us to pick one of the more compact forms. Otherwise, running ormolu over our codebase as it is now will produce a large number of redundant line breaks, probably more than are reasonable to clean up by hand.

judah avatar Mar 02 '20 20:03 judah

I suspect the issue is that Ormolu sees a multi-line expression, since you added a line break in the middle of f x, so it keeps it so. However,

main =
  f x

should be a fixpoint for Ormolu.

mboes avatar Mar 03 '20 09:03 mboes

f x stays f x, but if there is a newline between f and x, no matter how they are positioned relative to each other with respect to indentation, there will be a newline in the output. I think what we do is reasonable :man_shrugging: The only thing is that we could consider hanging applications, but I think we did this in the past and it led to some jarring output in other cases.

mrkkrp avatar Mar 03 '20 09:03 mrkkrp

As an aside, Ormolu is currently following the official elm style guide in this regard.

Always bring the body of the declaration down one line.

https://elm-lang.org/docs/style-guide#declarations

EDIT: At least in the case of the input being

main = f
  x

.

prednaz avatar Mar 18 '20 19:03 prednaz

I believe a concise version would be allowing all those user's preferred handing style for a particular case:

fn = foldl goSome initialValue -- allow keeping params in the first line
  [ somelongLine
  , somelongLine2
  ]

or:

fn = foldl goSome -- allow keeping params in the first line
  initialValue -- the rest params are spread vertically
  [ somelongLine
  , somelongLine2
  ]

or:

fn = -- allow the assignment to move to the next line 
  foldl goSome initialValue
    [ somelongLine
    , somelongLine2
    ]

or:

fn = foldl 
  goSome 
  initialValue
  [ somelongLine
  , somelongLine2
  ]

I wonder if this is going to be addressed or if it should be closed as not intended to be implemented?

wclr avatar Apr 08 '23 08:04 wclr

Related: #692

brandonchinn178 avatar Apr 08 '23 15:04 brandonchinn178