IDEA编程助手之 Live Template

快速的template编码

下面快速的写代码,你可能见过吧:
20-04-17.gif
20-09-35.gif
这些代码模板是怎么定义的呢?让我们我们从Live Template讲起.

Live Template是什么

Live Template是程序员的好帮手,只需要输入几个字母就可以调出一大段代码,而且还可以关联代码环境上下文.
使用Live Template可以在代码中插入常见构造,如循环、条件、声明或打印语句, 还可以自定义专属的代码块, 可以大大地提升编码速度.
参见Live Template官方地址 https://www.jetbrains.com/help/idea/using-live-templates.html

官方预定义的Live Template

AbbreviationExpands to
psfspublic static final String
main or psvmpublic static void main(String[] args){
}
soutSystem.out.println();

演示:
18-20-22.gif
18-38-38.gif

如何自定义Live Template

打开配置页面,进入 Live Template 选项卡,我们可以看到 IDEA 预先设置的模板配置。这些模板都是最常用的一些语句,我们先来看一下它们是干啥的。
image.png
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键跳到下一个变量。
用户自定义变量:你可以定义一个变量,然后编辑变量,可以编辑表达式,默认值.
image.png

我自定义的Template:
image.png

如何导入到你的idea

复制以下文件:

<template name="acc" value="@Data&#10;@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$);&#10;$END$&#10;" description="copy" toReformat="false" toShortenFQNames="true">
  <variable name="VAR" expression="suggestFirstVariableName(&quot;Object&quot;)" 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$);&#10;$END$&#10;" description="copyl" toReformat="false" toShortenFQNames="true">
  <variable name="VAR" expression="suggestFirstVariableName(&quot;Object&quot;)" 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(&quot;Object&quot;)" 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(&quot;Object&quot;)" 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$)) {&#10;$START$&#10;}&#10;" description="ifcm" toReformat="true" toShortenFQNames="true">
  <variable name="VAR" expression="suggestFirstVariableName(&quot;Object&quot;)" 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$)) {&#10;    return;&#10;}&#10;$START$" description="ifcmr" toReformat="true" toShortenFQNames="true">
  <variable name="VAR" expression="suggestFirstVariableName(&quot;Object&quot;)" 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$)) {&#10;    return Collections.emptyList();&#10;}&#10;$START$" description="ifcmrl" toReformat="true" toShortenFQNames="true">
  <variable name="VAR" expression="suggestFirstVariableName(&quot;Object&quot;)" 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$)) {&#10;    return Collections.emptyMap();&#10;}&#10;$START$" description="ifcmrm" toReformat="true" toShortenFQNames="true">
  <variable name="VAR" expression="suggestFirstVariableName(&quot;Object&quot;)" 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$)) {&#10;    $START$&#10;}" description="ifon" toReformat="true" toShortenFQNames="true">
  <variable name="VAR" expression="suggestFirstVariableName(&quot;Object&quot;)" 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$)) {&#10;    $START$&#10;}" description="ifonn" toReformat="true" toShortenFQNames="true" useStaticImport="true">
  <variable name="VAR" expression="suggestFirstVariableName(&quot;Object&quot;)" 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$)) {&#10;$START$&#10;}&#10;" description="ifsb" toReformat="true" toShortenFQNames="true">
  <variable name="VAR" expression="suggestFirstVariableName(&quot;Object&quot;)" 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$)) {&#10;    return;&#10;}&#10;$START$" description="ifsbr" toReformat="true" toShortenFQNames="true">
  <variable name="VAR" expression="suggestFirstVariableName(&quot;Object&quot;)" 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$)) {&#10;&#9;$START$    &#10;}&#10;" description="ifsnb" toReformat="false" toShortenFQNames="true">
  <variable name="VAR" expression="suggestFirstVariableName(&quot;Object&quot;)" 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$)) {&#10;    return;&#10;}&#10;$START$" description="ifsnbr" toReformat="false" toShortenFQNames="true">
  <variable name="VAR" expression="suggestFirstVariableName(&quot;Object&quot;)" 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$)) {&#10;&#9;$START$&#10;}" description="equals" toReformat="false" toShortenFQNames="true">
  <variable name="VAR" expression="suggestFirstVariableName(&quot;Object&quot;)" defaultValue="" alwaysStopAt="true" />
  <variable name="VAR2" expression="suggestFirstVariableName(&quot;Object&quot;)" defaultValue="" alwaysStopAt="true" />
  <variable name="START" expression="" defaultValue="" alwaysStopAt="true" />
  <context>
    <option name="JAVA_CODE" value="true" />
  </context>
</template>
<template name="li" value="List&lt;Integer&gt; $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&lt;Long&gt; $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&lt;String&gt; $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&lt;$VAR$&gt; $STAST$ = Maps.newHashMap();&#10;$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 -&gt; v, (v1, v2) -&gt; v2));" description="mapv" toReformat="false" toShortenFQNames="true">
  <variable name="VAR" expression="suggestFirstVariableName(&quot;Object&quot;)" 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) -&gt; v2));" description="mapv2" toReformat="true" toShortenFQNames="true">
  <variable name="VAR" expression="suggestFirstVariableName(&quot;Object&quot;)" 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$();&#10;" 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$)) {&#10;&#9;$END$&#10;}" description="nequals" toReformat="true" toShortenFQNames="true">
  <variable name="VAR" expression="suggestFirstVariableName(&quot;Object&quot;)" defaultValue="" alwaysStopAt="true" />
  <variable name="VAR2" expression="suggestFirstVariableName(&quot;Object&quot;)" defaultValue="" alwaysStopAt="true" />
  <context>
    <option name="JAVA_CODE" value="true" />
  </context>
</template>
<template name="setn" value="Set&lt;$VAR$&gt; $STAST$ = Sets.newHashSet();&#10;$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(&quot;Object&quot;)" 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(&quot;Object&quot;)" 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 = {&quot;eureka.client.register-with-eureka=false&quot;})&#10;@RunWith(SpringRunner.class)&#10;@Transactional&#10;@Rollback" description="test" toReformat="false" toShortenFQNames="true">
  <context>
    <option name="JAVA_CODE" value="true" />
  </context>
</template>
<template name="testm" value="@Test&#10;    public void test() {&#10;&#10;    }" description="testm" toReformat="false" toShortenFQNames="true">
  <context>
    <option name="JAVA_CODE" value="true" />
  </context>
</template>
<template name="tsleep" value="try {&#10;    TimeUnit.SECONDS.sleep($START$);&#10;} catch (InterruptedException e) {&#10;    e.printStackTrace();&#10;}" 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
image.png
新建一个group
image.png
选中新建的group将上面的文本复制
image.png
好了,大功告成,可以自定义自己的Template了.

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值