eclipse-archived / ceylon.formatter

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

How should the "nullsafe" and "spread" member operators be formatted? #21

Closed lucaswerkmeister closed 10 years ago

lucaswerkmeister commented 10 years ago

The regular member operator, ., is usually put on the start of the new line:

value result = mySequence
    .map(...)
    .filter(...)
    .fold(...);

How should this look for the nullsafe member operator ?. and the spread member operator *.?

lucaswerkmeister commented 10 years ago

Aligning the periods, 1

value result = mySequence
    .fold(...)
   ?.operation(...);

But this breaks my entire indentation architecture and just doesn’t work with tabs instead of spaces.

Aligning the periods, 2

value result = mySequence
    .fold(...)?
    .operation(...);

Unfortunately, this isn’t syntactically valid. I suppose it’s also hard to read.

Aligning the columns

value result = mySequence
    .fold(...)
    ?.operation(...);

That’s what the IDE’s indentation strategy does.

Operators on the previous line

value result = mySequence
    .fold(...)?.
    operation(...);

All operators on the previous line

value result = mySequence.
    fold(...)?.
    operation(...);
lucaswerkmeister commented 10 years ago

For now, I’ll go with Aligning the columns. I might add an option for All operators on the previous line later.

gavinking commented 10 years ago

Strangely, I kinda liked Aligning the periods, 2. But that would mean changing the grammar of the language. I think Aligning the columns is reasonable.

lucaswerkmeister commented 10 years ago

I’m kinda torn about Aligning the periods, 2 myself... I think it makes sense in a long call chain where you just don’t care if some intermediate result is null (“What, that’s optional? Fine, I’ll just add a ?. Ugh, that makes it all misaligned. Can’t I stick the ? up there?”), but if the null case is important (for example, if you’re assigning the result to some variable, maybe with an else clause as well), then I think that ? is too hidden and makes the code very unreadable.

gavinking commented 10 years ago

I don't think aligning the periods makes sense. More often you've got just one invocation, not a whole chain of them. Then for some crazy reason lines starting with ?. are indented differently to the rest of the code.

lucaswerkmeister commented 10 years ago

You mean 1? Yeah, I don’t like that one either. I don’t know why I put it first in the list...

gavinking commented 10 years ago

Ah, ok, yes, that's what I meant.

gavinking commented 10 years ago

Also, FTR, I don't like seeing lines ending in periods, it looks much too much like a full stop. And there's nothing warning you, if you just look at the next line, that it is a member reference you're seeing.

lucaswerkmeister commented 10 years ago

Good point. I don’t think that’s really used in Java either (Eclipse formatter doesn’t seem to have a setting for it), so I’ll just drop that and close the issue.