欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

在Intellij IDEA中设置文件模板

程序员文章站 2022-06-13 18:06:57
...

在 IDEA 中设置文件模板能够提高工作效率,此处以 Java 的类文件设置模板为例。

菜单路径:File -> Editor -> File and Code Templates,在 Includes 选项卡中添加如下四个文件:
CompanyHeader.java

#if(${PACKAGE_NAME} && ${PACKAGE_NAME.startsWith("com.yourcompany")})
/**
* Yourcompany.com Inc.
* Copyright (c) 2009-${YEAR} All Rights Reserved.
*/
#end

PackageHeader.java

#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end

ClassHeaderYourCompany.java

/**
 * @author ${MyName} ${Email} on ${YEAR}-${MONTH}-${DAY}
 */

ClassHeaderSelf.java

#set($MyName = "congyou.wu")
/**
*
* @author ${MyName}
* @version $Id: ${NAME}.java, v 0.1 ${YEAR}-${MONTH}-${DAY} ${TIME} ${MyName} Exp $
*/

ClassHeaderController.java

#set($MyName = "congyou.wu")
#set($Email = "[email protected]")
#if(${PACKAGE_NAME} && ${PACKAGE_NAME.startsWith("com.yourcompany")})
    #parse("ClassHeaderYourCompany.java")
#else
    #parse("ClassHeaderSelf.java")
#end

CommonHeader.java

#parse("CompanyHeader.java")
#parse("PackageHeader.java")
#parse("ClassHeaderController.java")

添加完以上几个文件之后,点击 Files,修改如下四个项目的模板内容:
Class

#parse("CommonHeader.java")
public class ${NAME} {
}

Interface

#parse("CommonHeader.java")
public interface ${NAME} {
}

Enum

#parse("CommonHeader.java")
public enum ${NAME} {
}

Annotation

#parse("CommonHeader.java")
public @interface ${NAME} {
}

Singleton

#parse("CommonHeader.java")
public class ${NAME}{

    private static ${NAME} instance = new ${NAME}();

    public static ${NAME} getInstance() {
        return instance;
    }

    private ${NAME}() {
    }
}