当前位置: 移动技术网 > IT编程>开发语言>Java > IDEA巧用Postfix Completion让码速起飞(小技巧)

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

2020年08月26日  | 移动技术网IT编程  | 我要评论
1. 情景展示自从做 java 开发之后,idea 编辑器是不可少的。 在 idea 编辑器中,有很多高效的代码补全功能,尤其是 postfix completion 功能,可以让编写代码更加的流畅。

1. 情景展示

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

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

 

2. 设置界面

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

 

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 内容请搜索移动技术网以前的文章或继续浏览下面的相关文章希望大家以后多多支持移动技术网!

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网