一、設置基礎提示
在進行代碼輸入的時候,Idea能夠根據之前輸入的內容進行基礎提示,這需要設置一些基礎的配置。
1、設置Java的基礎提示:在Idea的設置菜單中找到Editor -> General -> Code Completion,將”Match case”和”Show popup”選項打開。這樣,Idea在進行Java代碼輸入時會根據之前輸入的代碼進行基礎提示。
<!-- Sample code -->
public class Main {
public static void main(String[] args) {
// Typing "Sy" will prompt "System" as an option
System.out.println("Hello, world!");
}
}
2、設置其他語言的基礎提示:在Idea的設置菜單中找到對應語言的Code Completion頁面,按照自己的需求進行設置。如:Python的設置路徑為Editor -> General -> Code Completion -> Python。
<!-- Sample code -->
def main():
# Typing "pri" will prompt "print" as an option
print("Hello, world!")
if __name__ == "__main__":
main()
二、設置代碼自動導入
當我們在使用Java等編程語言時,可能會經常用到一些未導入的類庫,在這時Idea能夠自動提示並導入。
1、在Idea的設置菜單中找到Editor -> General -> Auto Import,在這裡你可以選擇”Add unambiguous imports on the fly”來實現當導入的類庫唯一時,Idea會自動導入,避免手動導入帶來的代碼冗餘。
2、在Java開發中,我們可能會遇到重名的類庫,在這種情況下,Idea會根據我們的設置來確定需要導入哪一個。
<!-- Sample code -->
import java.util.Date;
import java.sql.Date;
public class Main {
public static void main(String[] args) {
// Typing "Date" will prompt two options
// If we chose java.util.Date, the import statement will be added automatically
Date now = new Date();
}
}
三、設置參數提示
在進行方法或函數調用時,Idea可能會根據我們的輸入進行參數提示,可以大大提高編程效率。
1、在Idea的設置菜單中找到Editor -> General -> Code Completion,將”Parameter Info”選項打開。這樣,當我們輸入方法或函數名時,Idea會提示該方法或函數的參數列表。
<!-- Sample code -->
public class Main {
public static void main(String[] args) {
String hello = "Hello, world!";
// Typing "hello.substring(" will prompt "beginIndex" and "endIndex" as options
String subString = hello.substring(0, 5);
}
}
2、我們也可以通過按下Ctrl + P來手動觸發參數提示。
四、設置代碼重構提示
在進行代碼編寫時,我們可能需要進行一些代碼重構操作,這時Idea能夠根據我們的代碼進行提示,方便我們快速進行代碼重構操作。
1、在Idea的設置菜單中找到Editor -> Code Style -> Java -> JavaDoc,將”Generate ‘equals’ and ‘hashCode'”選項打開。這樣,當我們需要進行代碼重構時,Idea會根據我們的代碼提示我們是否需要實現equals和hashCode方法。
<!-- Sample code -->
public class Student {
private String name;
private int age;
// Ctrl + R to refactor to equals and hashCode
// Typing "new Student(" will prompt "name" and "age" as options
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() { return name; }
public int getAge() { return age; }
}
2、在進行對代碼進行重構操作時,Idea會根據我們的代碼分析,提示相關的代碼塊,方便我們快速進行代碼重構操作。
五、設置快捷鍵提示
當我們使用Idea進行編程時,可能會經常使用一些快捷鍵,Idea能夠對這些快捷鍵進行提示。
1、在Idea的設置菜單中找到Keymap,Idea會列出所有的快捷鍵,並提供搜索功能方便查找快捷鍵。
2、在進行代碼編寫時,Idea會根據我們的操作提示相關的快捷鍵,方便我們進行操作。
<!-- Sample code -->
public class Main {
public static void main(String[] args) {
String hello = "Hello, world!";
// Ctrl + Alt + V to extract to variable
String subString = hello.substring(0, 5);
}
}
原創文章,作者:SDZKU,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/370956.html