IDEA巧用Postfix Completion让码速起飞(小技巧)

发布时间:2020-09-20 02:30:08 作者:白菜Java自习室
来源:脚本之家 阅读:221

1. 情景展示

自从做 Java 开发之后,IDEA 编辑器是不可少的。 在 IDEA 编辑器中,有很多高效的代码补全功能,尤其是 Postfix Completion 功能,可以让编写代码更加的流畅。

Postfix completion 本质上也是代码补全,它比 Live Templates 在使用上更加流畅一些,我们可以看一下下面的这张图。

IDEA巧用Postfix Completion让码速起飞(小技巧) 

2. 设置界面

可以通过如下的方法打开 Postfix 的设置界面,并开启 Postfix。

IDEA巧用Postfix Completion让码速起飞(小技巧) 

3. 常用的 Postfix 模板

3.1. boolean 变量模板

!: Negates boolean expression

//before
public class Foo {
   void m(boolean b) {
     m(b!);
   }
 }
 
//after
public class Foo {
  void m(boolean b) {
    m(!b);
  }
}

if: Checks boolean expression to be 'true'

//before
public class Foo {
  void m(boolean b) {
    b.if
  }
}

//after
public class Foo {
  void m(boolean b) {
    if (b) {

    }
  }
}

else: Checks boolean expression to be 'false'.

//before
public class Foo {
  void m(boolean b) {
    b.else
  }
}

//after
public class Foo {
  void m(boolean b) {
    if (!b) {

    }
  }
}

3.2. array 变量模板

for: Iterates over enumerable collection.

//before
public class Foo {
  void m() {
    int[] values = {1, 2, 3};
    values.for
  }
}

//after
public class Foo {
  void m() {
    int[] values = {1, 2, 3};
    for (int value : values) {

    }
  }
}

fori: Iterates with index over collection.

//before
public class Foo {
  void m() {
    int foo = 100;
    foo.fori
  }
}

//after
public class Foo {
  void m() {
    int foo = 100;
    for (int i = 0; i < foo; i++) {

    }
  }
}

3.3. 基本类型模板

opt: Creates Optional object.

//before
public void m(int intValue, double doubleValue, long longValue, Object objValue) {
 intValue.opt
 doubleValue.opt
 longValue.opt
 objValue.opt
}

//after
public void m(int intValue, double doubleValue, long longValue, Object objValue) {
 OptionalInt.of(intValue)
 OptionalDouble.of(doubleValue)
 OptionalLong.of(longValue)
 Optional.ofNullable(objValue)
}

sout: Creates System.out.println call.

//before
public class Foo {
 void m(boolean b) {
  b.sout
 }
}

//after
public class Foo {
 void m(boolean b) {
   System.out.println(b);
 }
}

3.4. Object 模板

nn: Checks expression to be not-null.

//before
public class Foo {
  void m(Object o) {
    o.nn
  }
}
//after
public class Foo {
  void m(Object o) {
    if (o != null){

    }
  }
}

null: Checks expression to be null.

//before
public class Foo {
  void m(Object o) {
    o.null
  }
}
//after
public class Foo {
  void m(Object o) {
    if (o != null){

    }
  }
}

notnull: Checks expression to be not-null.

//before
public class Foo {
  void m(Object o) {
    o.notnull
  }
}
//after
public class Foo {
  void m(Object o) {
    if (o != null){

    }
  }
}

val: Introduces variable for expression.

//before
public class Foo {
  void m(Object o) {
    o instanceof String.var
  }
}

//after
public class Foo {
  void m(Object o) {
    boolean foo = o instanceof String;
  }
}

3.5. 其他模板

new: Inserts new call for the class.

//before
Foo.new

//after
new Foo()

return: Returns value from containing method.

//before
public class Foo {
  String m() {
    "result".return
  }
}
//after
public class Foo {
  String m() {
    return "result";
  }
}

到此这篇关于IDEA巧用Postfix Completion让码速起飞(小技巧)的文章就介绍到这了,更多相关IDEA Postfix Completion 内容请搜索亿速云以前的文章或继续浏览下面的相关文章希望大家以后多多支持亿速云!

推荐阅读:
  1. 我收藏的IntelliJ IDEA使用教程(2019图文版)
  2. Java Idea如何实现高效率配置的技巧讲解

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

idea postfix completion

上一篇:Spring Boot如何动态创建Bean示例代码

下一篇:一文让你彻底搞清楚javascript中的require、import与export

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》