在IntelliJ IDEA中使用import语句创建新的实时模板 [英] Creating new live-templates with import statements in IntelliJ IDEA

查看:125
本文介绍了在IntelliJ IDEA中使用import语句创建新的实时模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我要移植的Eclipse模板:

Here is the Eclipse template that I want to port:

${:import(org.apache.log4j.Logger)}
private static final Logger LOG = Logger.getLogger(${enclosing_type}.class);

我在IDEA中的当前版本如下:

My current version in IDEA is as follows:

private static final Logger LOG = Logger.getLogger($CLASS_NAME$.class);$END$

其中$CLASS_NAME$配置为使用className()作为其表达式.

where $CLASS_NAME$ is configured to use className() as its expression.

不幸的是,我找不到有关添加import语句的任何文档.是否有与Eclipse ${:import(...)}等效的东西?

Unfortunately, I don't find any documentation on adding the import statement. Is there somehing equivalent to Eclipse ${:import(...)}?

推荐答案

根据这篇文章,旨在仅使用完全限定的表达式.我尝试了一下,这对我有用:

According to this post, it is intended to use only full-qualified expressions. I tried it out and this worked for me:

private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger($CLASS_NAME$.class);$END$

IDEA会自动将其缩短并添加必要的导入语句:

IDEA automatically shortens it and adds the necessary import statements:

import org.apache.log4j.Logger;
// ...
private static final Logger LOG = Logger.getLogger(MyClass.class);


如果想尝试一下,请注意,首先必须通过编辑变量CLASS_NAME定义为className().另外,请确保通过 Change (位于底部)允许Java的实时模板声明.这是最终设置的屏幕截图:


If you want to try yourself, note that you first have to define CLASS_NAME as className() via Edit variables. Also make sure that you allowed your Live Template for Java declarations via Change (at the bottom). Here is a screenshot with the final setup:

这篇关于在IntelliJ IDEA中使用import语句创建新的实时模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆