[groovy] organize file templates

This commit is contained in:
Daniil Ovchinnikov
2016-11-10 19:03:22 +03:00
parent 7c07796438
commit 070546e3e1
24 changed files with 35 additions and 153 deletions
@@ -1,16 +0,0 @@
<%--
Created by IntelliJ IDEA.
User: ${USER}
Date: ${DATE}
Time: ${TIME}
--%>
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<title></title>
</head>
<body>
</body>
</html>
@@ -1,30 +0,0 @@
<!--
~ Copyright 2000-2007 JetBrains s.r.o.
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
~
-->
<html>
<body>
<table width="100%" border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111">
<tr>
<td colspan="3">
<font face="verdana" size="-1">
This is a built-in template used each time you create new
Groovy Server Page
</font>
</td>
</tr>
</table>
</body>
</html>
+1
View File
@@ -5,6 +5,7 @@
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/resources" type="java-resource" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
@@ -0,0 +1,14 @@
<idea-plugin>
<extensions defaultExtensionNs="com.intellij">
<internalFileTemplate name="Groovy Class"/>
<internalFileTemplate name="Groovy Interface"/>
<internalFileTemplate name="Groovy Trait"/>
<internalFileTemplate name="Groovy Enum"/>
<internalFileTemplate name="Groovy Annotation"/>
<internalFileTemplate name="Groovy Script"/>
<internalFileTemplate name="Groovy DSL Script"/>
<internalFileTemplate name="Gant Script"/>
</extensions>
</idea-plugin>
+1 -5
View File
@@ -568,8 +568,6 @@
<targetElementEvaluator language="Groovy"
implementationClass="org.jetbrains.plugins.groovy.lang.psi.impl.GroovyTargetElementEvaluator"/>
<fileTemplateGroup implementation="org.jetbrains.plugins.groovy.actions.GroovyTemplatesFactory"/>
<projectConfigurable instance="org.jetbrains.plugins.groovy.compiler.GroovyCompilerConfigurable" id="Groovy compiler"
displayName="Groovy Compiler" parentId="project.propCompiler"/>
<java.compiler implementation="org.jetbrains.plugins.groovy.compiler.GreclipseIdeaCompiler"/>
@@ -1778,9 +1776,6 @@
</actions>
<application-components>
<component>
<implementation-class>org.jetbrains.plugins.groovy.gant.GantLoader</implementation-class>
</component>
<component>
<implementation-class>org.jetbrains.plugins.groovy.mvc.MvcPathMacros</implementation-class>
</component>
@@ -1808,6 +1803,7 @@
</component>
</project-components>
<xi:include href="groovy-templates.xml" xpointer="xpointer(/idea-plugin/*)"/>
<xi:include href="groovy-indexed-property-support.xml" xpointer="xpointer(/idea-plugin/*)"/>
<xi:include href="groovy-logback-support.xml" xpointer="xpointer(/idea-plugin/*)"/>
</idea-plugin>
@@ -21,15 +21,17 @@ import org.jetbrains.annotations.NonNls;
* @author Max Medvedev
*/
public interface GroovyTemplates {
@NonNls String GROOVY_CLASS = "GroovyClass.groovy";
@NonNls String GROOVY_INTERFACE = "GroovyInterface.groovy";
@NonNls String GROOVY_TRAIT = "GroovyTrait.groovy";
@NonNls String GROOVY_ENUM = "GroovyEnum.groovy";
@NonNls String GROOVY_ANNOTATION = "GroovyAnnotation.groovy";
@NonNls String GROOVY_SCRIPT = "GroovyScript.groovy";
@NonNls String GROOVY_DSL_SCRIPT = "GroovyDslScript.gdsl";
@NonNls String GANT_SCRIPT = "GantScript.gant";
@NonNls String GROOVY_SERVER_PAGE = "GroovyServerPage.gsp";
@NonNls String GROOVY_CLASS = "Groovy Class.groovy";
@NonNls String GROOVY_INTERFACE = "Groovy Interface.groovy";
@NonNls String GROOVY_TRAIT = "Groovy Trait.groovy";
@NonNls String GROOVY_ENUM = "Groovy Enum.groovy";
@NonNls String GROOVY_ANNOTATION = "Groovy Annotation.groovy";
@NonNls String GROOVY_SCRIPT = "Groovy Script.groovy";
@NonNls String GROOVY_DSL_SCRIPT = "Groovy DSL Script.gdsl";
@NonNls String GANT_SCRIPT = "Gant Script.gant";
@NonNls String GROOVY_FROM_USAGE_METHOD_BODY = "Groovy New Method Body.groovy";
@NonNls String GROOVY_JUNIT_TEST_METHOD_GROOVY = "Groovy JUnit Test Method.groovy";
@NonNls String GROOVY_JUNIT_SET_UP_METHOD_GROOVY = "Groovy JUnit SetUp Method.groovy";
@@ -17,65 +17,27 @@
package org.jetbrains.plugins.groovy.actions;
import com.intellij.codeInsight.actions.ReformatCodeProcessor;
import com.intellij.ide.fileTemplates.*;
import com.intellij.openapi.fileTypes.FileTypeManager;
import com.intellij.ide.fileTemplates.FileTemplate;
import com.intellij.ide.fileTemplates.FileTemplateManager;
import com.intellij.ide.fileTemplates.JavaTemplateUtil;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiFileFactory;
import com.intellij.util.ArrayUtil;
import com.intellij.util.IncorrectOperationException;
import icons.JetgroovyIcons;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.groovy.GroovyBundle;
import org.jetbrains.plugins.groovy.GroovyFileType;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Properties;
public class GroovyTemplatesFactory implements FileTemplateGroupDescriptorFactory {
@NonNls
public static final String[] TEMPLATES = {GroovyTemplates.GROOVY_CLASS, GroovyTemplates.GROOVY_SCRIPT};
public void registerCustromTemplates(String... templates) {
Collections.addAll(myCustomTemplates, templates);
}
private static class GroovyTemplatesFactoryHolder {
private static final GroovyTemplatesFactory myInstance = new GroovyTemplatesFactory();
}
public static GroovyTemplatesFactory getInstance() {
return GroovyTemplatesFactoryHolder.myInstance;
}
private final ArrayList<String> myCustomTemplates = new ArrayList<>();
public class GroovyTemplatesFactory {
@NonNls
static final String NAME_TEMPLATE_PROPERTY = "NAME";
static final String LOW_CASE_NAME_TEMPLATE_PROPERTY = "lowCaseName";
@Override
public FileTemplateGroupDescriptor getFileTemplatesDescriptor() {
final FileTemplateGroupDescriptor group = new FileTemplateGroupDescriptor(GroovyBundle.message("file.template.group.title.groovy"), JetgroovyIcons.Groovy.Groovy_16x16);
final FileTypeManager fileTypeManager = FileTypeManager.getInstance();
for (String template : TEMPLATES) {
group.addTemplate(new FileTemplateDescriptor(template, fileTypeManager.getFileTypeByFileName(template).getIcon()));
}
//add GSP Template
group.addTemplate(new FileTemplateDescriptor(
GroovyTemplates.GROOVY_SERVER_PAGE, fileTypeManager.getFileTypeByFileName(GroovyTemplates.GROOVY_SERVER_PAGE).getIcon()));
// register custom templates
for (String template : getInstance().getCustomTemplates()) {
group.addTemplate(new FileTemplateDescriptor(template, fileTypeManager.getFileTypeByFileName(template).getIcon()));
}
return group;
}
public static PsiFile createFromTemplate(@NotNull final PsiDirectory directory,
@NotNull final String name,
@NotNull String fileName,
@@ -89,7 +51,7 @@ public class GroovyTemplatesFactory implements FileTemplateGroupDescriptorFactor
Properties properties = new Properties(FileTemplateManager.getInstance(project).getDefaultProperties());
JavaTemplateUtil.setPackageNameAttribute(properties, directory);
properties.setProperty(NAME_TEMPLATE_PROPERTY, name);
properties.setProperty(LOW_CASE_NAME_TEMPLATE_PROPERTY, name.substring(0, 1).toLowerCase() + name.substring(1));
properties.setProperty(LOW_CASE_NAME_TEMPLATE_PROPERTY, StringUtil.decapitalize(name));
for (int i = 0; i < parameters.length; i += 2) {
properties.setProperty(parameters[i], parameters[i + 1]);
}
@@ -98,7 +60,8 @@ public class GroovyTemplatesFactory implements FileTemplateGroupDescriptorFactor
text = template.getText(properties);
}
catch (Exception e) {
throw new RuntimeException("Unable to load template for " + FileTemplateManager.getInstance(project).internalTemplateToSubject(templateName), e);
String message = "Unable to load template for " + FileTemplateManager.getInstance(project).internalTemplateToSubject(templateName);
throw new RuntimeException(message, e);
}
final PsiFileFactory factory = PsiFileFactory.getInstance(project);
@@ -112,8 +75,4 @@ public class GroovyTemplatesFactory implements FileTemplateGroupDescriptorFactor
return file;
}
public String[] getCustomTemplates() {
return ArrayUtil.toStringArray(myCustomTemplates);
}
}
@@ -1,44 +0,0 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.plugins.groovy.gant;
import com.intellij.openapi.components.ApplicationComponent;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.groovy.actions.GroovyTemplates;
import org.jetbrains.plugins.groovy.actions.GroovyTemplatesFactory;
/**
* @author ilyas
*/
public class GantLoader implements ApplicationComponent {
@Override
@NonNls
@NotNull
public String getComponentName() {
return "Gant loader";
}
@Override
public void initComponent() {
GroovyTemplatesFactory.getInstance().registerCustromTemplates(GroovyTemplates.GANT_SCRIPT);
}
@Override
public void disposeComponent() {
}
}