Creating new live-templates with import statements in IntelliJ IDEA

前端 未结 4 924
广开言路
广开言路 2021-02-02 06:06

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)         


        
4条回答
  •  逝去的感伤
    2021-02-02 06:09

    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 automatically shortens it and adds the necessary import statements:

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

    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:

提交回复
热议问题