如何让IDEA在格式化时自动为代码与//注释间添加空格?
// in IntelliJ IDEA Formatting Let's start by outlining the specific formatting style you want to achieve:
- Example A:
T result = (T) node.value; // keep return value(space between semicolon and//) - Example B:
if (k < 0 || k > size) // out of range, return false;(space between closing parenthesis and//) - Example C:
if (size == 0 || k == size) { // append at end, append(value); return true; }(space between opening brace and//)
You mentioned you searched for solutions and found a post about adding space after //—but that's IDEA's default behavior, and it doesn't address your need for space before // between code and the comment delimiter.
Current State of IDEA's Formatting Settings
As of the latest versions of IntelliJ IDEA, there is no built-in configuration option to automatically insert this space during code formatting. All existing comment-related formatting settings focus on spacing after the // or comment alignment, not the gap between code and the comment delimiter.
Update: Feature Request Submitted
To address this, a feature request has been created on JetBrains' YouTrack platform with the title Provide option to add a space between code and "//" on formatting. If you or other developers share this preference, voting for the ticket will help JetBrains gauge demand and potentially prioritize adding this feature in a future update.
内容的提问来源于stack exchange,提问作者Eric

