0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

IntelliJの自動整形における差異とその対策

0
Last updated at Posted at 2025-07-06

はじめに

現場で軽微なコード修正を行っただけなのに、GitHub Desktopを開くと大量の差分が発生して困った…という経験、ありませんか?
私自身、JUnitで .doReturn() のような「.から始まる表現」が保存時に自動整形される問題に直面しました。

Ctrl + Z で整形結果を戻しても…

Ctrl + S を押すと再び自動整形されてしまう。

今回は、そのような 「望まない自動整形」を防ぐための設定項目と対策 をまとめました。
※ 実際の現場での適用結果によっては、この記事も随時更新予定です。

目的:整形は「明示的な操作時のみ」に限定したい

たとえば、次のようにしたい場合です:

保存時(Ctrl + S)には整形させたくない

整形ショートカット(Ctrl + Alt + L)を使ったときだけ整形したい

部分的に整形を無効化したい場合

コメントベースで整形のオン・オフを制御する
File → Settings → Editor → Code Style

Enable formatter markers in comments にチェックを入れる

その後、コード内に以下を記述:

// @formatter:off
.doReturn() // ← ここは整形されない
// @formatter:on

保存時の自動整形を無効化する

File → Settings → Tools → Actions on Save

以下のチェックを確認し、解消したいことに応じて外す:

✅ Reformat code(コードの再整形)

✅ Optimize imports(インポートの最適化)

✅ Rearrange code(コードの再編成)

必要に応じて「対象:変更された行のみ」を選択

コード補完時の自動整形を抑止する

File → Settings → Editor → General → Code Completion

「Insert selected suggestion by pressing enter」など、補完動作に連動する自動整形をオフにする設定を確認・調整

整形対象からファイルを除外する(Do not format)

Settings → Editor → Code Style → Formatter

Do not format に *.java などの対象ファイルを設定
 ※ 「Do not format に *.java を設定した場合、その .java ファイルには整形ショートカット(Ctrl + Alt + L)も効かなくなります」

メソッドの「1行表示」自動変換を抑止する

Settings → Editor → General → Code Folding

「One-line methods」のチェックを外す
 → 複数行メソッドが勝手に1行に変形されるのを防げます

タブが勝手に隠されるのを防ぐ

File → Settings → Editor → General → Editor Tabs

「Hide tabs if there is no space」のチェックを外す
 → タブが省略されず、常に表示されるようになります

注意点:Save Actions プラグインを使っている場合

もし以下のようなプラグインが導入されている場合:

Save Actions

Prettier(JavaScript/HTML/CSSなど)

この場合は別途そのプラグイン内の保存時トリガーを無効化する必要があります。

コメント歓迎

「うちの会社ではこうしてる」

「こんなトラブルがあった」
など、ぜひ気軽にコメントください!

参考記事

https://qiita.com/daijinload/items/0a66c900dbd14e452f17?utm_source=chatgpt.com
https://codingtechroom.com/question/disable-intellij-automatic-formatting?utm_source=chatgpt.com
https://stackoverflow.com/questions/2977324/disable-reformatting-code-when-saving-files?utm_source=chatgpt.com
https://platform.jetbrains.com/t/how-to-programatically-update-actions-on-save-settings/504

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?