Java 我们如何在 IntelliJ IDEA 中编写我们自己的代码模板文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19717135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 19:38:41  来源:igfitidea点击:

How can we write our own code template file in IntelliJ IDEA

javaintellij-ideacode-templates

提问by ashish

I need to create a sample code template in my IntelliJ IDEA project so that everybody in the team can also kind of import it in their IDEs and use it.

我需要在我的 IntelliJ IDEA 项目中创建一个示例代码模板,以便团队中的每个人也可以将它导入到他们的 IDE 中并使用它。

I am able to do it on my own machine by changing the "class" template myself, but i just don't have any way to make it available to my team like exporting it to a file that can be used, in Eclipse it is possible to have one sampleCodeFormatter.xml file that everybody can import in their eclipse workspace.

我可以通过自己更改“类”模板在我自己的机器上完成它,但是我没有任何方法可以将其提供给我的团队,例如将其导出到可以使用的文件中,在 Eclipse 中它是可以有一个 sampleCodeFormatter.xml 文件,每个人都可以在他们的 eclipse 工作区中导入。

How does the same thing works in IntelliJ IDEA?

同样的事情在 IntelliJ IDEA 中是如何工作的?

采纳答案by vikingsteve

First, in IntelliJ, open Settings (ctrl-alt-s), under IDE Settings find File and Code Templatesand have a look at the Templatestab.

首先,在 IntelliJ 中,打开 Settings (ctrl-alt-s),在 IDE Settings 下找到File and Code Templates并查看Templates选项卡。

You will see some templates there already, for example there is one called Class:

你会在那里看到一些模板,例如有一个叫做Class

#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
#parse("File Header.java")
public class ${NAME} {
}

Note that it includes File Header, which is found under the Includestab.

请注意,它包括File HeaderIncludes选项卡下找到的。

Now, you can easily create your own file templates here. Under the Templatestab, just hit the green "Add" button, give your new template a name (Foo?),ensure the extension is javaand write a valid java class.

现在,您可以在此处轻松创建自己的文件模板。在Templates选项卡下,只需点击绿色的“添加”按钮,为您的新模板命名(Foo?),确保扩展名为 java并编写一个有效的 java 类。

Save, and your newly configured template exists here:

保存,您新配置的模板存在于此处:

C:\Users\{USER}\.IntelliJIdea12\config\fileTemplates\Foo.java

You should be able to share this file with your team.

您应该能够与您的团队共享此文件。

A neat feature is that from within the Projectpane of a java project, under a source root, hit alt-enter, choose a New Java Classand under the Kinddrop-down - voila! You can see your Footemplate as a valid option.

一个巧妙的功能是,从Java 项目的Project窗格中,在源根目录下,点击alt-enter,选择一个 New Java Class,然后在Kind下拉菜单下 - 瞧!您可以将Foo模板视为有效选项。

回答by ranma2913

Here's the contents of my File Header.java

这是我的内容 File Header.java

/**
 * Project: ${PROJECT_NAME}
 * Package: ${PACKAGE_NAME}
 * <p>
 * User: ${USER}
 * Date: ${DATE}
 * Time: ${TIME}
 * <p>
 * Created with IntelliJ IDEA
 * To change this template use File | Settings | File Templates.
 */