IntelliJ IDEA 学习笔记

来源:互联网 发布:数据助理是做什么的 编辑:程序博客网 时间:2024/05/26 02:52

本文简单介绍IntelliJ IDEA中文件代码模板的使用,参考IntelliJ IDEA 简体中文专题教程,英文好的同学可以查看英文官网文档

文件代码模板的介绍

  文件代码模板:我们在项目中创建某些类型文件时,已模板的形式预设代码内容在对应的这些新文件中

  以预设的Singleton模板为例

  这里写图片描述

  新建Java Class时可以选择使用Singleton模板

  这里写图片描述

  新建的文件如下

  这里写图片描述

文件代码模板的设置

  文件代码模板的设置在File -> Settings -> Editor -> File and Code Templates下,如下图

  这里写图片描述

  • 基本常见的文件类型都都支持使用文件代码模板
  • IntelliJ IDEA 的文件代码模板是可以使用 Velocity Template Language (VTL) 进行书写的。如图上的 #if ... #end#parse 都是 VTL 的语法
  • 图示的操作按钮作用分别是
    • Create Template 创建一个文件代码模板
    • Remove Template 删除一个文件代码模板,预设模板是不允许删除的,只能删除预设之外的新增的
    • Copy Template 复制一个文件代码模板
    • Reset To Default 对被修改的预设文件代码模板,还原到默认状态

  Other选项卡下有更多的文件类型,不用担心有不支持的文件类型,常用的基本都被涵盖了,如下

  这里写图片描述

  Includes选项卡下是供其他文件代码模板的头文件模板,如下

  这里写图片描述

  引用的File Header.javaIncludes选项卡下

  这里写图片描述

  Code选项卡 中主要是 IntelliJ IDEA 自动帮我们生成某些代码的时候引用的模板,无法新建、删除 Code 里面的代码模板,如下

  这里写图片描述

  下面举两个制作文件代码模板的例子

xml文件模板

  1.第一步,制作

   这里写图片描述

  2.第二步,使用

   这里写图片描述

   这里写图片描述

   这里写图片描述

添加 Java Class 注释

  1.第一步,制作

  这里写图片描述

  • ${DESCRIPTION} 是我们自己定义的变量,自定义变量格式有两种方式:$变量名${变量名}
  • 当我们需要用到一个固定值的自定义变量的时候并且该变量多个地方被引用,我们可以通过 VTL 语法的 #set( $变量名 = "变量值内容" ); 来设置

  2.第二步,使用

  这里写图片描述

  这里写图片描述

  这里写图片描述

文件代码模板预设的变量

  • ${PACKAGE_NAME} - the name of the target package where the new class or interface will be created.
  • ${PROJECT_NAME} - the name of the current project.
  • ${FILE_NAME} - the name of the PHP file that will be created.
  • ${NAME} - the name of the new file which you specify in the New File dialog box during the file creation.
  • ${USER} - the login name of the current user.
  • ${DATE} - the current system date.
  • ${TIME} - the current system time.
  • ${YEAR} - the current year.
  • ${MONTH} - the current month.
  • ${DAY} - the current day of the month.
  • ${HOUR} - the current hour.
  • ${MINUTE} - the current minute.
  • ${PRODUCT_NAME} - the name of the IDE in which the file will be created.
  • ${MONTH_NAME_SHORT} - the first 3 letters of the month name. Example: Jan, Feb, etc.
  • ${MONTH_NAME_FULL} - full name of a month. Example: January, February, etc.
原创粉丝点击