快速的template编码
下面快速的写代码,你可能见过吧:
这些代码模板是怎么定义的呢?让我们我们从Live Template讲起.
Live Template是什么
Live Template是程序员的好帮手,只需要输入几个字母就可以调出一大段代码,而且还可以关联代码环境上下文.
使用Live Template可以在代码中插入常见构造,如循环、条件、声明或打印语句, 还可以自定义专属的代码块, 可以大大地提升编码速度.
参见Live Template官方地址 https://www.jetbrains.com/help/idea/using-live-templates.html
官方预定义的Live Template
Abbreviation | Expands to |
---|---|
psfs | public static final String |
main or psvm | public static void main(String[] args){ } |
sout | System.out.println(); |
演示:
如何自定义Live Template
打开配置页面,进入 Live Template 选项卡,我们可以看到 IDEA 预先设置的模板配置。这些模板都是最常用的一些语句,我们先来看一下它们是干啥的。
1.key就是 IDEA 识别的模板的别名,就像开头展示的当你键入 main时,IDEA 就会自动识别为该模板.
2.描述就是一个注释,方便你使用时,知道这个key是干什么的.
3.value就是你自己定义的具体的模板内容.
4.变量就是设置自定义变量值,可以使用 IDEA 提供的一些内置函数,还可以使用强大的 Groovy 脚本,需要更多详情请移步 IDEA 的官网.
5.而应用上下文则表示该模板在什么上下文中生效。可以在java中生效,css中生效,SQL中生效等等等.
自定义Live Template演示
这里介绍一下上面说到的变量:
系统预定义
E
N
D
END
END:指示代码段完成时光标的位置,您不能再按Tab键跳到下一个变量。
用户自定义变量:你可以定义一个变量,然后编辑变量,可以编辑表达式,默认值.
我自定义的Template:
如何导入到你的idea
复制以下文件:
<template name="acc" value="@Data @Accessors(chain = true)" description="@Data @Accessors(chain = true)" toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_CODE" value="true" />
</context>
</template>
<template name="copy" value="BeanUtil.copyProperties($VAR$,$START1$); $END$ " description="copy" toReformat="false" toShortenFQNames="true">
<variable name="VAR" expression="suggestFirstVariableName("Object")" defaultValue="" alwaysStopAt="true" />
<variable name="START1" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="JAVA_CODE" value="true" />
</context>
</template>
<template name="copyl" value="BeanUtil.copyToList($VAR$,$START1$); $END$ " description="copyl" toReformat="false" toShortenFQNames="true">
<variable name="VAR" expression="suggestFirstVariableName("Object")" defaultValue="" alwaysStopAt="true" />
<variable name="START1" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="JAVA_CODE" value="true" />
</context>
</template>
<template name="emptyIfNull" value="return CollUtil.emptyIfNull($VAR$);" description="emptyIfNull" toReformat="false" toShortenFQNames="true">
<variable name="VAR" expression="suggestFirstVariableName("Object")" defaultValue="" alwaysStopAt="true" />
<context>
<option name="JAVA_CODE" value="true" />
</context>
</template>
<template name="first" value="CollUtil.isEmpty(list) ? null : list.get(0);" description="null or list.get(0)" toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_CODE" value="true" />
</context>
</template>
<template name="group" value="$VAR$.stream().collect(Collectors.groupingBy($START$));" description="group" toReformat="true" toShortenFQNames="true">
<variable name="VAR" expression="suggestFirstVariableName("Object")" defaultValue="" alwaysStopAt="true" />
<variable name="START" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="JAVA_CODE" value="true" />
</context>
</template>
<template name="ifcm" value="if (CollUtil.isEmpty($VAR$)) { $START$ } " description="ifcm" toReformat="true" toShortenFQNames="true">
<variable name="VAR" expression="suggestFirstVariableName("Object")" defaultValue="var" alwaysStopAt="true" />
<variable name="START" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="ACTION_SCRIPT" value="false" />
<option name="ASPECTJ" value="false" />
<option name="CSS" value="false" />
<option name="CUCUMBER_FEATURE_FILE" value="false" />
<option name="CoffeeScript" value="false" />
<option name="ColdFusion" value="false" />
<option name="ECMAScript6" value="false" />
<option name="GROOVY" value="false" />
<option name="GSP" value="false" />
<option name="HAML" value="false" />
<option name="HTML" value="false" />
<option name="JAVA_SCRIPT" value="false" />
<option name="JSON" value="false" />
<option name="JSP" value="false" />
<option name="MAVEN" value="false" />
<option name="MXML" value="false" />
<option name="OGNL" value="false" />
<option name="OTHER" value="true" />
<option name="REQUEST" value="false" />
<option name="SQL" value="false" />
<option name="TypeScript" value="false" />
<option name="XML" value="false" />
</context>
</template>
<template name="ifcmr" value="if (CollUtil.isEmpty($VAR$)) { return; } $START$" description="ifcmr" toReformat="true" toShortenFQNames="true">
<variable name="VAR" expression="suggestFirstVariableName("Object")" defaultValue="" alwaysStopAt="true" />
<variable name="START" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="JAVA_CODE" value="true" />
</context>
</template>
<template name="ifcmrl" value="if (CollUtil.isEmpty($VAR$)) { return Collections.emptyList(); } $START$" description="ifcmrl" toReformat="true" toShortenFQNames="true">
<variable name="VAR" expression="suggestFirstVariableName("Object")" defaultValue="" alwaysStopAt="true" />
<variable name="START" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="JAVA_CODE" value="true" />
</context>
</template>
<template name="ifcmrm" value="if (CollUtil.isEmpty($VAR$)) { return Collections.emptyMap(); } $START$" description="ifcmrm" toReformat="true" toShortenFQNames="true">
<variable name="VAR" expression="suggestFirstVariableName("Object")" defaultValue="" alwaysStopAt="true" />
<variable name="START" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="JAVA_CODE" value="true" />
</context>
</template>
<template name="ifon" value="if (Objects.isNull($VAR$)) { $START$ }" description="ifon" toReformat="true" toShortenFQNames="true">
<variable name="VAR" expression="suggestFirstVariableName("Object")" defaultValue="" alwaysStopAt="true" />
<variable name="START" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="JAVA_CODE" value="true" />
</context>
</template>
<template name="ifonn" value="if (Objects.nonNull($VAR$)) { $START$ }" description="ifonn" toReformat="true" toShortenFQNames="true" useStaticImport="true">
<variable name="VAR" expression="suggestFirstVariableName("Object")" defaultValue="" alwaysStopAt="true" />
<variable name="START" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="JAVA_CODE" value="true" />
</context>
</template>
<template name="ifsb" value="if (StrUtil.isBlank($VAR$)) { $START$ } " description="ifsb" toReformat="true" toShortenFQNames="true">
<variable name="VAR" expression="suggestFirstVariableName("Object")" defaultValue="" alwaysStopAt="true" />
<variable name="START" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="JAVA_CODE" value="true" />
</context>
</template>
<template name="ifsbr" value="if (StrUtil.isBlank($VAR$)) { return; } $START$" description="ifsbr" toReformat="true" toShortenFQNames="true">
<variable name="VAR" expression="suggestFirstVariableName("Object")" defaultValue="" alwaysStopAt="true" />
<variable name="START" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="JAVA_CODE" value="true" />
</context>
</template>
<template name="ifsnb" value="if (StrUtil.isNotBlank($VAR$)) { 	$START$ } " description="ifsnb" toReformat="false" toShortenFQNames="true">
<variable name="VAR" expression="suggestFirstVariableName("Object")" defaultValue="" alwaysStopAt="true" />
<variable name="START" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="JAVA_CODE" value="true" />
</context>
</template>
<template name="ifsnbr" value="if (StrUtil.isNotBlank($VAR$)) { return; } $START$" description="ifsnbr" toReformat="false" toShortenFQNames="true">
<variable name="VAR" expression="suggestFirstVariableName("Object")" defaultValue="" alwaysStopAt="true" />
<variable name="START" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="JAVA_CODE" value="true" />
</context>
</template>
<template name="is" value="if (Objects.equals($VAR$, $VAR2$)) { 	$START$ }" description="equals" toReformat="false" toShortenFQNames="true">
<variable name="VAR" expression="suggestFirstVariableName("Object")" defaultValue="" alwaysStopAt="true" />
<variable name="VAR2" expression="suggestFirstVariableName("Object")" defaultValue="" alwaysStopAt="true" />
<variable name="START" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="JAVA_CODE" value="true" />
</context>
</template>
<template name="li" value="List<Integer> $START$ = Lists.newArrayList();" description="li" toReformat="true" toShortenFQNames="true">
<variable name="START" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="JAVA_CODE" value="true" />
</context>
</template>
<template name="ll" value="List<Long> $START$ = Lists.newArrayList();" description="ll" toReformat="false" toShortenFQNames="true">
<variable name="START" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="JAVA_CODE" value="true" />
</context>
</template>
<template name="ls" value="List<String> $START$ = Lists.newArrayList();" description="ls" toReformat="true" toShortenFQNames="true">
<variable name="START" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="JAVA_CODE" value="true" />
</context>
</template>
<template name="mapn" value="Map<$VAR$> $STAST$ = Maps.newHashMap(); $END$" description="mapn" toReformat="false" toShortenFQNames="true">
<variable name="VAR" expression="" defaultValue="" alwaysStopAt="true" />
<variable name="STAST" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="JAVA_CODE" value="true" />
</context>
</template>
<template name="mapv" value="$VAR$.stream().collect(Collectors.toMap($START1$, v -> v, (v1, v2) -> v2));" description="mapv" toReformat="false" toShortenFQNames="true">
<variable name="VAR" expression="suggestFirstVariableName("Object")" defaultValue="" alwaysStopAt="true" />
<variable name="START1" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="JAVA_CODE" value="true" />
</context>
</template>
<template name="mapv2" value="$VAR$.stream().collect(Collectors.toMap($START1$, $START2$, (v1, v2) -> v2));" description="mapv2" toReformat="true" toShortenFQNames="true">
<variable name="VAR" expression="suggestFirstVariableName("Object")" defaultValue="" alwaysStopAt="true" />
<variable name="START1" expression="" defaultValue="" alwaysStopAt="true" />
<variable name="START2" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="JAVA_CODE" value="true" />
<option name="KOTLIN" value="true" />
</context>
</template>
<template name="newo" value="$VAR$ $VAR1$ = new $VAR$(); " description="" toReformat="false" toShortenFQNames="true">
<variable name="VAR" expression="camelCase(String)" defaultValue="" alwaysStopAt="true" />
<variable name="VAR1" expression="camelCase(VAR)" defaultValue="" alwaysStopAt="true" />
<context>
<option name="JAVA_CODE" value="true" />
</context>
</template>
<template name="nis" value="if (!Objects.equals($VAR$, $VAR2$)) { 	$END$ }" description="nequals" toReformat="true" toShortenFQNames="true">
<variable name="VAR" expression="suggestFirstVariableName("Object")" defaultValue="" alwaysStopAt="true" />
<variable name="VAR2" expression="suggestFirstVariableName("Object")" defaultValue="" alwaysStopAt="true" />
<context>
<option name="JAVA_CODE" value="true" />
</context>
</template>
<template name="setn" value="Set<$VAR$> $STAST$ = Sets.newHashSet(); $END$" description="setn" toReformat="false" toShortenFQNames="true">
<variable name="VAR" expression="" defaultValue="" alwaysStopAt="true" />
<variable name="STAST" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="JAVA_CODE" value="true" />
</context>
</template>
<template name="smc" value="$VAR$.stream().map($START$).collect(Collectors.toList());" description="smc" toReformat="true" toShortenFQNames="true">
<variable name="VAR" expression="suggestFirstVariableName("Object")" defaultValue="" alwaysStopAt="true" />
<variable name="START" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="JAVA_CODE" value="true" />
</context>
</template>
<template name="smdc" value="$VAR$.stream().map($START$).distinct().collect(Collectors.toList());" description="smdc" toReformat="true" toShortenFQNames="true">
<variable name="VAR" expression="suggestFirstVariableName("Object")" defaultValue="" alwaysStopAt="true" />
<variable name="START" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="JAVA_CODE" value="true" />
</context>
</template>
<template name="tc" value="Thread.currentThread()" description="Thread.currentThread()" toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_CODE" value="true" />
</context>
</template>
<template name="test" value="@SpringBootTest(classes = TestApplication.class, properties = {"eureka.client.register-with-eureka=false"}) @RunWith(SpringRunner.class) @Transactional @Rollback" description="test" toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_CODE" value="true" />
</context>
</template>
<template name="testm" value="@Test public void test() { }" description="testm" toReformat="false" toShortenFQNames="true">
<context>
<option name="JAVA_CODE" value="true" />
</context>
</template>
<template name="tsleep" value="try { TimeUnit.SECONDS.sleep($START$); } catch (InterruptedException e) { e.printStackTrace(); }" description="tsleep" toReformat="false" toShortenFQNames="true">
<variable name="START" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="JAVA_CODE" value="true" />
</context>
</template>
粘贴到你的IDEA
打开IDEA->Setting->Editor->Live Template
新建一个group
选中新建的group将上面的文本复制
好了,大功告成,可以自定义自己的Template了.