eclipse-archived / ceylon-ide-eclipse

Eclipse Plugin for Ceylon
http://ceylon-lang.org/documentation/ide
Eclipse Public License 1.0
59 stars 28 forks source link

quick assist to reverse branches of if/else #303

Closed gavinking closed 12 years ago

gavinking commented 12 years ago

By reversing the order of the blocks, and negating the boolean condition.

gavinking commented 12 years ago

@sjurba OK, I'm really happy with this, well done! :-)

Just two missing issues, though:

First, if I have:

return process.arguments.first else "gavin";

Then the required syntax is this:

if (exists first = process.arguments.first) {
    return first;
}
else {
    return "gavin";
}

(This is specifically for converting else->if (exists) with a "potentially-variable" expression.)

Second, if I have anything like this, with an inferred type:

    value x = 1==1 then "yes";

Then it needs an explicit type declaration:

String x;
if (1==1) {
    x = "yes";
}
else {
    x = null;
}

There is existing machinery for doing the necessary type inference, in SpecifyTypeProposal, for example.

sjurba commented 12 years ago

So, I've had a crack at this. I'm converting: true <-> false == <-> != < <-> >= > <-> <=

For the rest I'm adding or removing the NotOp (!): member <-> !member

For is, exists, empty, and possibly others (Not BooleanExpressions): (is Type) <-> (! (is Type))

<operator expression> <-> ! (<operator expression)

I.e anything not an operator expression does not need parenthesis. I guess there are a operators with higher precedence than ! which would not require the parenthesis, but for simplicity and also clarity I suggest we leave it like that.

Did I leave anything out?

For instance, is there an operator for not identical? (===)

I had to do some magic to fix indentation, especially for handling else if

gavinking commented 12 years ago

@sjurba dude, awesome, I tried out the patch before reading your last message and was totally caught offguard by the cool handling of comparison operators. Very nice!

The only quibble I have is about sticking a space after ! haha

gavinking commented 12 years ago

Hrm, an idea I just had:

I think it should recurse down logical expressions, changing || to && and vice-versa, and then reversing the constituent comparison expressions, rather than just wrapping the whole logical expression in a !. WDYT?

sjurba commented 12 years ago

We'll of course. if (! (x==2)) would look kinda stupid..

I was not sure how you liked your braces either:

if (..) { } else { }

or

if (..) { } else { }

So I implemented both, depending on what style the if currently has...

I can get rid of the space, but I'll give you a minute to see if you have something else.. And there you go.. :)

gavinking commented 12 years ago

So I implemented both, depending on what style the if currently has...

Good, perfect.

In general that is what we should always try to do i.e. not interfere with the code style that the user has. (I personally hate } else {, but it's not at all uncommon.)

gavinking commented 12 years ago

One extra minor quibble: you're inserting an extra newline at the end each time. Can we get rid of that please?

sjurba commented 12 years ago

So I noticed that I though we needed parens around !is, !exists, etc. But now I see we don't.. if (!s is String), if(!exists s), etc..

I'll get rid of that and push again including the minor quibbles.