eclipse-archived / ceylon.formatter

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

Aliased annotations get formatted wrongly #126

Closed ePaul closed 8 years ago

ePaul commented 8 years ago

I'm recently doing code golf in Ceylon, and one of the tricks to get a shorter program is using alias-imports for everything which appears several times.

One of my recent programs started this way (after being formatted):

import ceylon.language {
    sh=shared,
    fo=formal
}

interface X {
    sh
    fo
    V v(S t);
    sh
    fo
    G g;
}

(G, V and S are classes defined lower down.)

I actually would expect the formatter to format the interface like this:

interface X {
    sh fo V v(S t);
    sh fo G g;
}

The formatter doesn't seem to recognize the aliases as inline annotations, and formats them as "normal" annotations on separate lines.

Mentioning them with --inlineAnnotations doesn't seem to help either.

(I'm using the 1.2.0 version.)

lucaswerkmeister commented 8 years ago

I don’t think I can do this without typechecking the code, sorry.

gavinking commented 8 years ago

Yes Lucas you're right about that.

gavinking commented 8 years ago

Although you could do it with a crappy implementation that doesn't try to disambiguate inner scoped declarations and just looks at the import list.

lucaswerkmeister commented 8 years ago

I guess that would actually work almost all of the time… if you have an inner scoped sh value, then sh as annotation will be an error anyways.

lucaswerkmeister commented 8 years ago

Hang on, I missed this bit:

Mentioning them with --inlineAnnotations doesn't seem to help either.

This should definitely work. What’s your exact invocation?

ePaul commented 8 years ago

I'm currently not at that computer, I will check again in the evening ... I think it was something like this:

ceylon format --inlineAnnotations 'sh fo va default actual' source/codegolf/tinylisp63352/expressions.ceylon

I also tried with a = instead of the space after --inlineAnnotations, no difference. Should it be comma-separated? (Then the documentation needs to be changed.)

ePaul commented 8 years ago

Update: I tried again ... if using --inlineAnnotations 'sh fo va default actual', it doesn't destroy the annotation formatting, but it doesn't restore it either. So if the input is like either example in my first post, it stays this way.

Unfortunately the formatter in the IDE doesn't seem to have a way of configuring any formatting options, so it "destroys" it on the next Source → Format, even if I manually fix everything :-/

lucaswerkmeister commented 8 years ago

Wow, this took me a while to figure out. Here’s what happened:

  1. You ran ceylon format file.ceylon. The formatter, unexpectedly, put newlines after your annotations.
  2. You ran ceylon format --inlineAnnotations 'sh fo va default actual' file.ceylon on the same file. With these inlineAnnotations, the formatter didn’t enforce line breaks after these annotations… but it also didn’t explicitly remove the line breaks that the previous run had introduced.

In other words, running ceylon format --inlineAnnotations 'sh fo va default actual' on the following file:

import ceylon.language {
    sh=shared,
    fo=formal
}

interface X {
    sh fo V v(S t);
    sh fo G g;
}

indeed leaves it untouched – but if you already have line breaks, the formatter keeps them.

I think I’ll change it so that inline annotations will always be forced to have no line breaks after them.