7

猛用 IntelliJ IDEA 的F2 快捷键,你会飘的

 2 years ago
source link: https://dayarch.top/p/intellj-idea-f2-shortcut.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

优雅永不过时,优秀的程序员写的代码要体现出优雅(至少得有范儿),IntelliJ IDEA 是多数程序员的 IDE,可很少有程序员注意它的细小功能,功能虽小却又大用处,本文就介绍一组超级简单的 IDEA 组合拳来达到优雅

F2 功能键

每当我写完代码,我基本上会使用 F2 快速检查一下所写的代码,然后按照提示作出相应的修改,比如这是我的烂代码:

public class AltEnterHelper {
private final String[] strArray = new String[]{"I", "Love", "IntelliJ IDEA", "so", "much"};

public void error() {
HashMap<Integer, String> strings = new HashMap<>();
}

public void lambda() {
//lambda can be replaced with method reference
sort(strArray, String::compareToIgnoreCase);

//Statement lambda can be replaced with expression lambda, and then expand lambda expression body to {...}
Function<Function, Function> fa = (Function function) -> {
return function.compose(function);
};

//Remove redundant types
Function<Function, Function> fb = (Function function) -> function.compose(function);
}

}

光标在编辑器任何位置,只需要按一下 F2,鼠标就会自动跳转到存在错误或者可以优化的位置,同时给出修改提示和修改快捷键,为了更好的演示,我将整个过程做成了动图

  • F2 自动跳转到错误或可优化位置
  • Alt + Enter 选择优化选项
  • Enter 选择选项

blog-imgFeb-10-202222-12-33.gif

代码继续向下优化,和上面步骤一样,只不过用快捷键 Shift + Alter + Enter 快速选择选项, 来看两个例子

blog-imgFeb-13-202209-19-03.gif

blog-imgFeb-13-202209-20-46.gif

我个人非常喜欢这类小功能,优化后的代码看着优雅,还会节省下来很多时间用来摸鱼,这并不是什么复杂的操作,希望可以帮到你


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK