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

2

6

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.

Downy answered 20/7, 2017 at 19:15 Comment(5)
It sounds like you are using a workaround to prevent code-formatters from messing up with your code formatting. In the process, you are now having to do another work-around to have the IDE not do something that would be beneficial for most (except in your case, it isn't). My gut feel when I have to do a work-around for a work-around is that I am (perhaps) heading in the wrong direction. Firstly, comments at the end of a line a discouraged by many code-quality analysis tools. Then, formatting should be defined externally to the IDE (with a file) so that it can be shared by/with others.Dim
You can turn of locally the formatter with a comment before and after the region with -by default disabled- // @formatter:off/// @formatter:on comments (search the settings for Enable formatter markers in comments)Twibill
@ochi I believe it is a good thing to be able to force line breaks in meaningful places. The formatter sometimes does a poor job at choosing a good place. The // isn't very pretty but is the best I have found. The // is also necessary to prevent an external formatter to mess up the code. My feeling is that I work against deficiencies in the IDE. When I insert //<enter>, Intellij should remember that the end of the line was actual code and should retain it as code, not as a comment.Downy
Definitely a personal opinion here but I find that workarounds to formatting personal preferences are not the job of an IDE. How is it to distinguish from, I want to comment out this code vs I want to continue using this code (but I put a // in front of it)? For example, you could just simply break line first and then add the // at the end of the line or add the @formatter:off annotation.... (which these unique, personal use-cases are for) - I am not sure the IDE is meant to read our minds (yet ;) )Dim
It works well in Eclipse. Eclipse lets me add the //'s where I need them and doesn't try to guess when I need them and add them when it decides I do. I only want to disable this particular automation, many automations can be disabled, why not this one?Downy
Y
1

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.

Yamamoto answered 21/7, 2017 at 17:10 Comment(0)
J
1

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.

Jonasjonathan answered 17/11, 2017 at 21:3 Comment(3)
Thank you for the hint. It doesn't answer the question but gives an alternative to using //. It could help some but in my case that doesn't work because the code style is imposed by my company.Downy
I think that option should still prevent IntelliJ from moving // to the next line, right?Jonasjonathan
I just tried. It doesn't. Not in IntelliJ Ultimate 2017.2 on OS X.Downy

© 2022 - 2024 — McMap. All rights reserved.