How to remove spaces after comments in Eclipse auto formatting?

911 views Asked by At

I'm using the OpenJML plugin for my project, but Eclipse auto formatting messes with my JML code. JML is written after a //@ symbol.

//@ requires password != null;
//@ ensures !isActive() && getPassword().testWord(password) ? isActive() && \result : isActive() == \old(isActive()) && !\result;

Eclipse auto formatting, however, adds a space in between of the // and the @ symbols, rendering my JML code useless.

// @ requires password != null;
// @ ensures !isActive() && getPassword().testWord(password) ? isActive() && \result : isActive() == \old(isActive()) && !\result;

Is there a way in which I can disable adding a space between // and @ symbols, or otherwise a way in which I can disable spacing after comments altogether?

I've tried changing the formatter profile, but I couldn't find the setting there.

I've also tried auto-removing trailing whitespace as explained here: How to auto-remove trailing whitespace in Eclipse? But that didn't work either. I assume because I'm specifically trying to change the comment auto formatting.

1

There are 1 answers

0
AxelH On BEST ANSWER

My solution would be simple, disable the line comment formatting.

Window > Preferences > Java > Code Style > Formatter

Edit the current profile (you need to rename it if this is the default profile).

In the Comment tab, uncheck Enable line comment formatting.

Reason

Even if this is possible to remove that space so that the formatting give you a correct

//@

This would impact every comment, not a big deal you can say. But what about a commented annotation ?

// @SuppressWarning("...") 

This would be formatted into

//@SuppressWarning("...") 

That will become a problem for the OpenJML, it will be a conflict. You can see more about that in the User guide - 4.2 Syntactic conflicts with @

Another solution, for both annotation or JML is to disable the formatter, but this will not be usable to be honest :

// @formatter:off 
...
// @formatter:on

Everything between those tags while not be formatted (if the formatter:on is omitted, everything after that in the file won't be formatted