eclipse-archived / ceylon.formatter

A formatter for the Ceylon programming language, written in Ceylon.
Apache License 2.0
14 stars 11 forks source link

Support one line per initializer/constructor/function parameter for long param lists, possibly as a configuration option #117

Open lukedegruchy opened 9 years ago

lukedegruchy commented 9 years ago

This is obviously not intended for 1.2 and is a feature request.

My personal preference is to take a really long parameter list, for example:

void functionWithReallyLongParamList(String one, String two, String three, String four, String five, String six, String seven) {}

And format it this way:

void functionWithReallyLongParamList(String one,
                                     String two,
                                     String three,
                                     String four,
                                     String five,
                                     String six,
                                     String seven) {}

I realize most Ceylon developers won't necessarily prefer that style but I think it should be an available option.

lucaswerkmeister commented 9 years ago

Alright, this requires at least #18 (alignment) and #11 (line breaks after every argument, even though not all of them are necessary to make it fit). Eventually, I hope to move ceylon.formatter to something more powerful than it is today, similar to dartfmt (I think this feature requires what they call constraints), but that’ll take a while.

lucaswerkmeister commented 9 years ago

Also, a commentary on code style: I much prefer this style:

void functionWithReallyLongParamList(one, two, three, four, five, six, seven) {

    "This is documentation.

     It takes up lots of space, so it’s better to put it in the body,
     rather than flood the parameter list with TMI."
    String one;

    "This is also documentation."
    String two;

    "Look at all this
     documentation!"
    String three;

    "Much document.
     Very clear.
     Such readable.
     Wow."
    String four;
    // …
}
gavinking commented 9 years ago

@lucaswerkmeister yeah, definitely.

lukedegruchy commented 9 years ago

I keep forgetting you can do that trick with functions as well as classes.

lukedegruchy commented 8 years ago

I think my suggestion actually is more applicable to the expression function syntax, where it's not possible to declare variables outside of the param list.

Boolean functionWithReallyLongParamList(String one,
                                        String two,
                                        String three,
                                        String four,
                                        String five,
                                        String six,
                                        String seven)
    =>  ....
lucaswerkmeister commented 8 years ago

@lukedegruchy also, function headers for formal or native functions.

Also, https://github.com/ceylon/ceylon.formatter/issues/117#issuecomment-141699968 is rubbish. If I change FormattingVisitor to enforce a line break after every comma, you get this:

void functionWithReallyLongParamList(String one,
    String two,
    String three,
    String four,
    String five,
    String six,
    String seven) {}

I don’t see why alignment would be needed here, this is just standard indentation of the opening parenthesis.

Remaining questions:

  1. How do we decide whether to split up the parameter list or not? The simplest mechanism would be a static threshold on the parameter count: up to five parameters stay inline, six and above are wrapped. With zero you can enforce wrapping for every function, with minus one you turn off this feature completely (which would be the default, I think).
    • Note: if this was implemented in the line-break strategy as suggested in the above comment, then we could do this iff the parameter list would otherwise break the maximum line limit (that is, either break after every comma, or after none of them). But this would mean adding a lot more information to the line-break strategy (it would have to know that this sequence of tokens corresponds to a parameter list).
  2. Shouldn’t there also be a line break between the ( and the String one?
lukedegruchy commented 8 years ago

I guess what I'm asking for is the Ceylon equivalent of the following Eclipse Java formatter settings. Whether or not these are feasible given the technical concerns raised by @lucaswerkmeister in the comment immediately preceding this one is another question.

Preferences > Java > Code Style > Formatter >

Indentation > Tab Policy: Spaces only

Line Wrapping > Line Wrapping Policy: Indent all, except first element if not necessary Indentation Policy: Indent on column

What's odd is that it doesn't work at all as intended in the preview if the tab policy is the default, which is tabs only.