eclipse-archived / ceylon.formatter

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

Not formatting imports and nested elements #85

Closed akberc closed 9 years ago

akberc commented 9 years ago

This code does not get formatted properly:

import ceylon.collection {
    MutableList,
    freq=frequencies
}
"Preview"
shared abstract class Abstr(text, string) of ab {
    shared String text;
    "The name of the object"
    shared actual String string;
} shared object ab extends Abstr(operatingSystem.newline, "ab") {
    shared void test() {

        class Test() {}
        value t => Test();
        print(t.string);
    } }

shared void run2() {
    ab.test();
}
lucaswerkmeister commented 9 years ago
  • ending braces
  • shared object does not being on new line

The formatter allows you to have no line breaks after a closing brace, mostly for the reason that otherwise this wouldn’t be allowed:

void run() {
} // This line comment would be moved to the next line, and perhaps you had valid reasons for putting it where it is now

singleLine import is not honoured :

singleLine means that the imports are allowed to be on a single line, but they’re also allowed to be across multiple lines. In other words, in singleLine, you’re allowed to write something like this:

import some.module {
    a, b, c, d, e,
    moreVerboseName=x,
    anotherBetterName=y
}

You can also put it on a single line, but I think it’s more readable this way.

Perhaps the option should be renamed to better reflect that… suggestions? enforceMultiLine vs permitSingleLine perhaps?

akberc commented 9 years ago

Thanks, makes sense. I guess I am used to aggressive formatters that lay down the law! However, if I may suggest:

Closing.

lucaswerkmeister commented 9 years ago

With 18f2176, your code is now formatted as:

import ceylon.collection {
    MutableList,
    freq=frequencies
}
"Preview"
shared abstract class Abstr(text, string) of ab {
    shared String text;
    "The name of the object"
    shared actual String string;
}
shared object ab extends Abstr(operatingSystem.newline, "ab") {
    shared void test() {

        class Test() {}
        value t => Test();
        print(t.string);
    } }

shared void run2() {
    ab.test();
}

Furthermore, you’re running into a corner case with ab / test: If you had more than a single declaration / statement in ab’s body, the double brace wouldn’t happen. The formatter allows you to write a body with at most one declaration or statement “inline”, which is really useful for switches:

switch (quantity)
case (0) { print("None"); }
case (1) { print("One"); }
case (2) { print("Two"); }
else { print("Many"); }

In your case the one declaration is itself multiple lines long, which is why having the outer body “inline” looks a bit stupid… but that’s not something the formatter can easily detect.