You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让IDEA在格式化时自动为代码与//注释间添加空格?

How to Auto-Add Space Between Code and // 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 09:21:36