How to keep comment on same line as annotation when reformatting code

已回答

I have a annotated java method like

@SuppressWarnings("PMD") // Explanation of why we are suppressing PMD warnings
public void foo() {
// do something
}


Whenever I reformat the code it changes it to

@SuppressWarnings("PMD")
// Explanation of why we are suppressing PMD warnings
public void foo() {
// do something
}

 

I want to keep the comment on the same line as the annotation when reformatting the code but I can't figure out how to do that.

0
Which version of the IDE do you use? Can you please export the code style scheme which is being used?
0

I'm using IntelliJ IDEA 2022.3.3 (Ultimate Edition), Build #IU-223.8836.41, built on March 9, 2023. 

Here's the code scheme:

<component name="ProjectCodeStyleConfiguration">
<code_scheme name="Project" version="173">
<JavaCodeStyleSettings>
<option name="CLASS_COUNT_TO_USE_IMPORT_ON_DEMAND" value="999" />
<option name="NAMES_COUNT_TO_USE_IMPORT_ON_DEMAND" value="999" />
<option name="PACKAGES_TO_USE_IMPORT_ON_DEMAND">
<value />
</option>
</JavaCodeStyleSettings>
<JetCodeStyleSettings>
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
</JetCodeStyleSettings>
<ScalaCodeStyleSettings>
<option name="MULTILINE_STRING_CLOSING_QUOTES_ON_NEW_LINE" value="true" />
</ScalaCodeStyleSettings>
<editorconfig>
<option name="ENABLED" value="false" />
</editorconfig>
<codeStyleSettings language="JAVA">
<option name="BLANK_LINES_AROUND_METHOD" value="0" />
<option name="BLANK_LINES_AROUND_METHOD_IN_INTERFACE" value="0" />
<option name="KEEP_SIMPLE_METHODS_IN_ONE_LINE" value="true" />
<option name="KEEP_SIMPLE_LAMBDAS_IN_ONE_LINE" value="true" />
<option name="KEEP_SIMPLE_CLASSES_IN_ONE_LINE" value="true" />
<option name="IF_BRACE_FORCE" value="1" />
<option name="DOWHILE_BRACE_FORCE" value="3" />
<option name="WHILE_BRACE_FORCE" value="3" />
<option name="FOR_BRACE_FORCE" value="3" />
<indentOptions>
<option name="USE_TAB_CHARACTER" value="true" />
</indentOptions>
</codeStyleSettings>
<codeStyleSettings language="XML">
<indentOptions>
<option name="USE_TAB_CHARACTER" value="true" />
</indentOptions>
</codeStyleSettings>
<codeStyleSettings language="kotlin">
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
<indentOptions>
<option name="USE_TAB_CHARACTER" value="true" />
</indentOptions>
</codeStyleSettings>
</code_scheme>
</component>
0
Not reproducible from my side. Does it help if you disable the `File | Settings | Editor | Code Style | Enable EditorConfig support` option?

Please also try disabling all the downloaded plugins from `Settings | Plugins | Click the gear button | Disable all the downloaded plugins`.
0

Thanks for checking. I looked into the issue further and found it's occurring because of line length wrapping. I was using the default line length of 120 and I had a longer comment so it was moving it to the next line. I increase the "Hard wrap at" line length setting and that resolved the problem. Thanks!

0

请先登录再写评论。