Florian F
Florian F

Reputation: 1377

How to prevent IntelliJ to stop continuing double-slash comments on the next line?

Short version

When pressing <enter> at the end of a // comment, Intellij sometimes decides to continue the // comment on the next line. How can I prevent that? Is there a setting somewhere to disable this automation?

Long version

There is a thing I do regularily, it is to break a long expression with a double-slash.

Let's say I have a line like

boolean isHex = c >= '0' && c <= '9' || c >= 'A' && c <= 'F' || c >= 'a' && c <= 'f';

and I want to split it like that

boolean isHex = c >= '0' && c <= '9' //
        || c >= 'A' && c <= 'F' //
        || c >= 'a' && c <= 'f';

Note that I want the final // in order to prevent any formatter to join the lines again.

So I insert a double-slash-return after the '9', by pressing //<enter>. But Intellij will auto-continue the comment on the next line.

boolean isHex = c >= '0' && c <= '9' //
// || c >= 'A' && c <= 'F' || c >= 'a' && c <= 'f';

It forces me to uncomment and reindent the line manually.

I want Intellij to not continue the comment on the next line and optionally indent my code:

boolean isHex = c >= '0' && c <= '9' //
        || c >= 'A' && c <= 'F' || c >= 'a' && c <= 'f';

So I want to disable this "continue // comment after <enter>" feature. Is it possible? I haven't found any setting related to that.

Upvotes: 6

Views: 1442

Answers (2)

Andrej Petrović
Andrej Petrović

Reputation: 907

Go to Settings → Code Style → Java → Wrapping and Braces and check "Line breaks" under "Keep when reformatting". This will make IntelliJ's formatter respect any manual line breaks, even if they contradict other formatting rules.

Upvotes: 1

matt helliwell
matt helliwell

Reputation: 2678

The closest you are going to get is to define a macro to insert a new line and remove the comment and then bind that macro to a suitable key.

Upvotes: 1

Related Questions