1. 情景展示
自从做 Java 开发之后,IDEA 编辑器是不可少的。 在 IDEA 编辑器中,有很多高效的代码补全功能,尤其是 Postfix Completion 功能,可以让编写代码更加的流畅。
Postfix completion 本质上也是代码补全,它比 Live Templates 在使用上更加流畅一些,我们可以看一下下面的这张图。
2. 设置界面
可以通过如下的方法打开 Postfix 的设置界面,并开启 Postfix。
3. 常用的 Postfix 模板
3.1. boolean 变量模板
!: Negates boolean expression
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
//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'
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
//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'.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
//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.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
//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.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
//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.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
//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.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
//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.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
//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.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
//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.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
//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.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
//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.
|
1
2
3
4
5
|
//before
Foo.new
//after
new Foo()
|
return: Returns value from containing method.
|
1
2
3
4
5
6
7
8
9
10
11
12
|
//before
public class Foo {
String m() {
"result".return
}
}
//after
public class Foo {
String m() {
return "result";
}
}
|
到此这篇关于IDEA巧用Postfix Completion让码速起飞(小技巧)的文章就介绍到这了,更多相关IDEA Postfix Completion 内容请搜索快网idc以前的文章或继续浏览下面的相关文章希望大家以后多多支持快网idc!
原文链接:https://juejin.im/post/6864742910290722829
相关文章
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
2025-06-04 89
-
2025-06-04 51
-
2025-05-25 83
-
2025-05-29 42
-
2025-05-29 98



