Intellij IDEA使用教程相關係列 目錄
目錄
1、安裝
點擊File->Setting ->Plugins->Install plugin from disk...,選中解壓好的/EclipseFormat/EclipseFormatter.zip安裝包安裝,重啓後生效。
注:EclipseFormatter有針對不同idea版本,如果上述壓縮文件不支持,請下載對應的版本
下載地址:https://plugins.jetbrains.com/plugin/6546-eclipse-code-formatter/versions
2、配置
基礎配置
選擇File->Setting ->Eclipse Code Formatter,勾選Use the Eclipse-code-formatter,在Supported file types:勾選Enable Java和Enable JavaScript。
並在Eclipse Java Formatter config file選擇Eclipse用的formatter-profile-java.xml文件,這個選項會決定格式化Java代碼的方式。
配置Import order,這個選項會決定組織import區域的方式。輸入
java;javax;net;org;com;
配置Eclipse JavaScript Formatter config file的From File也爲Eclipse用的formatter-profile-js.xml文件,這個選項會決定格式化Javascript代碼方式。如下圖:
注:上面的文件:formatter-profile-java.xml、formatter-profile-js.xml可在eclipse導出
助力通道:eclipse 代碼模板導出配置
import順序配置
File->Setting ->Editor
設置Class count to use import with '*': 99
Names count to use static import with '*' : 99
插入順序
以公司或者團隊的eclipse import包結構順序爲準,不一定要按照我給的例子
static all other,
blank,
java.*,
blank,
javax.*,
blank,
net.*,
blank,
org.*,
blank,
com.*,
blank,
all other imports
使用Intellij的格式化快捷鍵"Ctrl+ALT+L"即可進行格式化。如果出錯會輸出提示到Event Log裏,如果看到'xxx formatted sucessfully by Eclipse code formatter'則表示格式化成功。
使用Intellij的格式化快捷鍵"Ctrl+ALT+O"即可進行import包順序格式化。
大招:保存自動格式化
1、打開setting,找到Plugins選項(安裝Save Action 插件(如果有就跳過這一步))
搜索:Save Action
安裝成功之後,重啓Intellij
2、重啓之後,打開setting,找到我們的save actions(直接搜索)
第一個√標識保存代碼時執行動作;(只有用ctrl+s纔有效,idea自動保存不會觸發)
第二個√格式化導包;(相當於觸發了"Ctrl+ALT+O")
第三個√格式化代碼;(相當於觸發了"Ctrl+ALT+L")
這樣就可以了。