eclipse-archived / ceylon.formatter

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

Spacing in string templates #41

Closed lucaswerkmeister closed 10 years ago

lucaswerkmeister commented 10 years ago

Currently, string templates will always be spaced:

print("Hello, `` name ``!");

instead of

print("Hello, ``name``!");

I’m not sure if this is critical… I’m kinda reluctant to fix it, because it means I no longer get to use true for spaceBefore and spaceAfter, but instead have to use some slightly lower value that the string template’s false can still beat. Not difficult, but annoying :D

lucaswerkmeister commented 10 years ago

I suppose the spacing rules should be the same one as for #35…

lucaswerkmeister commented 10 years ago

Okay, regardless of that, this is definitely a bug:

options.``option.name`` = option;
// →
options.``option.name `` = option;

I have no idea why that space is missing there, because writer.write(" return baseOptions.`` option.name ``;\n"); works just fine.

lucaswerkmeister commented 10 years ago

Seeing as spaces in string templates account for the biggest part of the changes for #29, I guess I’ll implement it like #35… that should also fix the bug mentioned above.

lucaswerkmeister commented 10 years ago

Oh, I see now why the spacing is strange… there’s no visitStringLiteral, string literals are handled in the generic visitLiteral, with this:

fWriter.writeToken {
    that.mainToken;
    spaceBefore = 1;
    spaceAfter = 1;
    linebreaksBefore = visitingAnnotation then 0..3 else 0..1;
};
lucaswerkmeister commented 10 years ago

No, actually, there’s a different reason for that: In

"foo
 bar ``1`` baz"

the "foo\nbar``` is written as a line, and then3 ` baz" is written as the next line. That means that when the 3 token is checking for its previous token’s spaceAfter, it’s gone – already wiped from the token queue. That means another FormattingWriter attribute to carry over that state. Gah :-/

lucaswerkmeister commented 10 years ago

So, this issue went completely differently than I anticipated :D I had thought I’d have to get rid of most trues and falses for spaceBefore, spaceAfter, but it turns out I didn’t have to change any at all – which makes sense, because any expression that can appear in a string template can also appear, for example, as the single argument in a positional argument list, where you wouldn’t want spaces around it as well. Instead, I found some bugs in the FormattingWriter.