Eclipse 和 IntelliJ IDEA 都提供了寫代碼的輔助功能,包括代碼補全、代碼生成、快速修飾和動態模板等功能。
1. 快速修復(Quick-fixes)
快捷鍵:Alt+Enter
所有的快速修復都是基於 Settings | Editor | Inspections 中的選項進行提示。
批量修復
如果需要一次多多個地方進行修復,如整個目錄(folder)、一個模塊(module)或整個項目(project),可以使用下面兩個功能:
- Analyze | Run Inspection By Name
- Analyze | Inspect Code
| Eclipse |
|
IntelliJ IDEA |
|
| Action |
Shortcut |
Action |
Shortcut |
| Quick fix |
Ctrl+1 |
Show intention action |
Alt+Enter |
2. 生成代碼(Generating code)
生成代碼操作:Code | Generate (Alt+Insert)
該操作不僅可以在編輯器中使用,在項目窗口和導航欄也可以使用:
此時用於新建文件或包。
3. 代碼補全(Code completion)
IntelliJ IDEA提供了多種類型的代碼補全功能,包括:
- 基本補全(Basic completion)
- 次級基本補全(Second basic completion)
- 智能補全(Smart completion)
- 次級智能補全(Second smart completion)
- 語句補全(Statement completion)
對於這些補全功能的詳細描述,可以參考文章:
https://dzone.com/articles/top-20-code-completions-in-intellij-idea
IDEA默認不顯示選擇項的文檔,不過可以通過如下設置啟用:
不過不建議啟用,在需要時,可以用Ctrl+Q快捷鍵調用,如此可以看到對應方法的文檔,如下:
當光標在構造函數或方法的括號中,通過 Ctrl+P 可以看到參數信息:
如圖,StringReader只有一個構造函數,接受字符串參數。
| Eclipse |
|
IntelliJ IDEA |
|
| Action |
Shortcut |
Action |
Shortcut |
| Code completion |
Ctrl+Space |
Basic completion |
Ctrl+Space |
| - |
- |
Smart completion |
Ctrl+Shift+Space |
| - |
- |
Statement completion |
Ctrl+Shift+Enter |
- 模板
使用方法:輸入觸發字符串,按Tab鍵,如下:
IDEA的模板觸發方式和Eclipse稍有不同,如下:
| Template |
Eclipse |
IntelliJ IDEA |
| Define a main method |
main |
psvm |
| Iterate over an array |
for |
itar |
| Iterate over a collection |
for |
itco |
| Iterate over a list |
for |
itli |
| Iterate over an iterable using foreach syntax |
foreach |
iter |
| Print to System.out |
sysout |
sout |
| Print to System.err |
syserr |
serr |
| Define a static field |
static_final |
psf |
可用模板列表: Settings | Editor | Live Templates
除了這種常規模板,還有一些其他形式的模板。
后綴模板(postfix templates)
可用后綴模板列表:Settings | Editor | General | Postfix Completion
包含式動態模板(Surround with live template)
快捷鍵:Ctrl+Alt+J
和上面不同,此時按 Enter 鍵選擇

![clip_image001[6] clip_image001[6]](/image/aHR0cHM6Ly9pbWFnZXMyMDE1LmNuYmxvZ3MuY29tL2Jsb2cvNzIyMDY3LzIwMTYwNS83MjIwNjctMjAxNjA1MzEwOTM1MzgxNjQtMTA3ODQyMzA4MC5wbmc=.png)
![clip_image002[4] clip_image002[4]](/image/aHR0cHM6Ly9pbWFnZXMyMDE1LmNuYmxvZ3MuY29tL2Jsb2cvNzIyMDY3LzIwMTYwNS83MjIwNjctMjAxNjA1MzEwOTM1NDAwNzEtMzAxMjQ3ODk0LnBuZw==.png)
![clip_image003[4] clip_image003[4]](/image/aHR0cHM6Ly9pbWFnZXMyMDE1LmNuYmxvZ3MuY29tL2Jsb2cvNzIyMDY3LzIwMTYwNS83MjIwNjctMjAxNjA1MzEwOTM1NDE5NzctMTkwMzgxMzU5Mi5wbmc=.png)
![clip_image004[4] clip_image004[4]](/image/aHR0cHM6Ly9pbWFnZXMyMDE1LmNuYmxvZ3MuY29tL2Jsb2cvNzIyMDY3LzIwMTYwNS83MjIwNjctMjAxNjA1MzEwOTM1NDM2ODAtNTI2MTY4ODIyLnBuZw==.png)
![clip_image005[4] clip_image005[4]](/image/aHR0cHM6Ly9pbWFnZXMyMDE1LmNuYmxvZ3MuY29tL2Jsb2cvNzIyMDY3LzIwMTYwNS83MjIwNjctMjAxNjA1MzEwOTM1NDUzNTItOTc4OTQ4NjAwLnBuZw==.png)
![clip_image006[4] clip_image006[4]](/image/aHR0cHM6Ly9pbWFnZXMyMDE1LmNuYmxvZ3MuY29tL2Jsb2cvNzIyMDY3LzIwMTYwNS83MjIwNjctMjAxNjA1MzEwOTM1NDc1MjQtMTg5NDgwMzA5LnBuZw==.png)
![clip_image007[4] clip_image007[4]](/image/aHR0cHM6Ly9pbWFnZXMyMDE1LmNuYmxvZ3MuY29tL2Jsb2cvNzIyMDY3LzIwMTYwNS83MjIwNjctMjAxNjA1MzEwOTM1NDg0MzAtMTUxMjE3OTg0MC5wbmc=.png)
![clip_image008[4] clip_image008[4]](/image/aHR0cHM6Ly9pbWFnZXMyMDE1LmNuYmxvZ3MuY29tL2Jsb2cvNzIyMDY3LzIwMTYwNS83MjIwNjctMjAxNjA1MzEwOTM1NDk5NzctMjAwMTgwODQyNC5naWY=.png)
![clip_image009[4] clip_image009[4]](/image/aHR0cHM6Ly9pbWFnZXMyMDE1LmNuYmxvZ3MuY29tL2Jsb2cvNzIyMDY3LzIwMTYwNS83MjIwNjctMjAxNjA1MzEwOTM1NTE0NzctMTAzNTc0NjQ5MS5naWY=.png)
