Intellij reformat on file save

2019-01-02 19:18发布

I remember seeing in either IntelliJ or Eclipse the setting to reformat (cleanup) files whenever they are saved. How do I find it (didn't find it in the settings)

11条回答
弹指情弦暗扣
2楼-- · 2019-01-02 19:33

I set it to automatically clean up on check-in, which is usually good enough for me. If something is too ugly, I'll just hit the shortcut (Ctrl-Alt-L, Return). And I see they have an option for auto-formatting pasted code, although I've never used that.

查看更多
高级女魔头
3楼-- · 2019-01-02 19:34

Below is Neil's answer updated.

IntelliJ 13 Steps:

  1. Code -> Reformat Code enter image description here
  2. Edit -> Macros -> Start Macro Recording
  3. Code -> Reformat Code
  4. File -> Save all
  5. Edit -> Macros -> Stop Macro Recording
  6. Name the macro (something like "formatted save")
  7. File -> Settings -> Keymap
  8. Right click on the macro. Add Keyboard Shortcut. Set the keyboard shortcut to Control + S. enter image description here
  9. IntelliJ will inform you of a hotkey conflict. Select "remove" to remove other assignments.
查看更多
唯独是你
4楼-- · 2019-01-02 19:35

If you're developing in Flutter, there's a new experimental option as of 5/1/2018 that allows you to format code on save. Settings

查看更多
余生请多指教
5楼-- · 2019-01-02 19:41

IntellIJ 14 && 15: When you are checking in code in Commit changes dialog, tick the Reformat code checkbox, then IntelliJ will reformatting all the code that you are checking in.

Source: www.udemy.com/intellij-idea-secrets-double-your-coding-speed-in-2-hours

查看更多
妖精总统
6楼-- · 2019-01-02 19:41

If it's about Prettier, just use a File Watcher :

references => Tools => File Watchers => click + to add a new watcher => Prettier

https://prettier.io/docs/en/webstorm.html#running-prettier-on-save-using-file-watcher

查看更多
路过你的时光
7楼-- · 2019-01-02 19:42

If you have InteliJ Idea Community 2018.2 the steps are as fallows:

  1. In the top menu you click: Edit > Macros > Start Macro Recordings (you'll see a window lower right corner of your screen confirming that macros are being recorded)
  2. In the top menu you click: Code > Reformat Code (you'll see the option being selected in the lower right corner)
  3. In the top menu you click: Code > Optimize Imports (you'll see the option being selected in the lower right corner)
  4. In the top menu you click: File > Save All
  5. In the top menu you click: Edit > Macros > Stop Macro Recording
  6. You name the macro: "Format Code, Organize Imports, Save"
  7. In the top menu you clock: File > Settings. In the settings windows you click Keymap
  8. In the search box on the right you search "save". You'll find Save All (Ctrl+S). Right click on it and select "Remove Ctrl+S"
  9. Remove your search text from the box, press on the Collapse All button (Second button from the top left)
  10. Go to macros, press on the arrow to expand your macros, find your saved macro and right click on it. Select Add Keyboard Shortcut, and press Ctrl+S and okay.

Restart your IDE and try it.

I know what you're going to say, the guys before me wrote the same thing. But I got confused using the steps above this post, and I wanted to write a dumb down version for people who have the latest version of the IDE.

查看更多
登录 后发表回答