[java] adds "Create module-info.java" action

This commit is contained in:
Roman Shevchenko
2016-08-17 13:46:14 +03:00
parent 55d6de4857
commit 20f36c672b
7 changed files with 180 additions and 2 deletions
@@ -0,0 +1,83 @@
/*
* Copyright 2000-2016 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 com.intellij.ide.actions;
import com.intellij.icons.AllIcons;
import com.intellij.ide.IdeBundle;
import com.intellij.ide.fileTemplates.FileTemplate;
import com.intellij.ide.fileTemplates.FileTemplateManager;
import com.intellij.ide.fileTemplates.actions.AttributesDefaults;
import com.intellij.ide.fileTemplates.actions.CreateFromTemplateActionBase;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.module.ModuleUtilCore;
import com.intellij.openapi.module.impl.scopes.ModulesScope;
import com.intellij.openapi.project.Project;
import com.intellij.psi.JavaDirectoryService;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.search.FilenameIndex;
import com.intellij.psi.util.PsiUtil;
import org.jetbrains.annotations.NotNull;
import java.util.Optional;
import java.util.Properties;
import static com.intellij.ide.fileTemplates.JavaTemplateUtil.INTERNAL_MODULE_INFO_TEMPLATE_NAME;
import static com.intellij.psi.PsiJavaModule.MODULE_INFO_CLASS;
import static com.intellij.psi.PsiJavaModule.MODULE_INFO_FILE;
public class CreateModuleInfoAction extends CreateFromTemplateActionBase {
public CreateModuleInfoAction() {
super(IdeBundle.message("action.create.new.module-info.title"), IdeBundle.message("action.create.new.module-info.description"), AllIcons.FileTypes.Java);
}
@Override
public void update(@NotNull AnActionEvent e) {
DataContext ctx = e.getDataContext();
boolean available = Optional.ofNullable(LangDataKeys.IDE_VIEW.getData(ctx))
.map(view -> getTargetDirectory(ctx, view))
.filter(dir -> PsiUtil.isLanguageLevel9OrHigher(dir) && JavaDirectoryService.getInstance().isSourceRoot(dir))
.map(ModuleUtilCore::findModuleForPsiElement)
.map(module -> FilenameIndex.getVirtualFilesByName(module.getProject(), MODULE_INFO_FILE, new ModulesScope(module)).isEmpty())
.orElse(false);
e.getPresentation().setEnabledAndVisible(available);
}
@Override
protected FileTemplate getTemplate(@NotNull Project project, @NotNull PsiDirectory dir) {
FileTemplate template = FileTemplateManager.getInstance(project).getInternalTemplate(INTERNAL_MODULE_INFO_TEMPLATE_NAME);
template.setLiveTemplateEnabled(true);
return template;
}
@Override
public AttributesDefaults getAttributesDefaults(@NotNull DataContext ctx) {
AttributesDefaults defaults = new AttributesDefaults(MODULE_INFO_CLASS).withFixedName(true);
copyDefaultProperties(ctx, defaults);
defaults.addPredefined("MODULE_NAME", "$module_name$");
return defaults;
}
private static void copyDefaultProperties(DataContext ctx, AttributesDefaults defaults) {
Project project = CommonDataKeys.PROJECT.getData(ctx);
if (project != null) {
Properties props = FileTemplateManager.getInstance(project).getDefaultProperties();
props.stringPropertyNames().forEach(name -> defaults.addPredefined(name, props.getProperty(name)));
}
}
}
@@ -43,6 +43,7 @@ public class JavaTemplateUtil {
INTERNAL_CLASS_TEMPLATE_NAME, INTERNAL_INTERFACE_TEMPLATE_NAME, INTERNAL_ANNOTATION_TYPE_TEMPLATE_NAME, INTERNAL_ENUM_TEMPLATE_NAME};
public static final String INTERNAL_PACKAGE_INFO_TEMPLATE_NAME = "package-info";
public static final String INTERNAL_MODULE_INFO_TEMPLATE_NAME = "module-info";
private JavaTemplateUtil() { }
@@ -356,6 +356,8 @@ action.create.new.package-info.description=Create new package-info.java
error.package.already.contains.package-info=''package-info.java'' already exists for package ''{0}''
error.package.already.contains.package.html=Package ''{0}'' already has a ''package.html'' file. Create ''package-info.java'' anyway?
error.package.html.found.title=Found 'package.html'
action.create.new.module-info.title=module-info.java
action.create.new.module-info.description=Create new module-info.java
button.create=&Create
prompt.enter.new.class.name=Enter a new class name:
title.new.class=New Class
@@ -0,0 +1,3 @@
#parse("File Header.java")
module ${MODULE_NAME} {
}
@@ -0,0 +1,86 @@
<html>
<body>
<table width="100%" border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse">
<tr>
<td><font face="verdana" size="-1">This is a built-in template used each time you create a new module-info.java file, by
selecting <i>New | module-info.java</i> from the popup menu in one of the project views.<br>
The template is editable. Along with Java expressions and comments, you can also use predefined variables
(listed below) that will then be expanded like macros into the corresponding values.<br>
It is also possible to specify an arbitrary number of custom variables in the format
<b>${&lt;VARIABLE_NAME&gt;}</b>. In this case, before the new
file is created, you will be prompted with a dialog where you can define particular values for all
custom variables.<br>
Using the <b>#parse</b> directive, you can include templates from the <i>Includes</i>
tab, by specifying the full name of the desired template as a parameter in quotation marks.
For example:<br>
<b>#parse("File Header.java")</b>
</font></td>
</tr>
</table>
<table width="100%" border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse">
<tr>
<td colspan="3"><font face="verdana" size="-1">Predefined variables will take the following values:</font></td>
</tr>
<tr>
<td valign="top"><nobr><font face="verdana" size="-2"><b>${MODULE_NAME}</b></font></nobr></td>
<td width="10">&nbsp;</td>
<td width="100%" valign="top"><font face="verdana" size="-1">name of the module in which the new module-info.java file is created</font></td>
</tr>
<tr>
<td valign="top"><nobr><font face="verdana" size="-2"><b>${USER}</b></font></nobr></td>
<td width="10">&nbsp;</td>
<td width="100%" valign="top"><font face="verdana" size="-1">current user system login name</font></td>
</tr>
<tr>
<td valign="top"><nobr><font face="verdana" size="-2"><b>${DATE}</b></font></nobr></td>
<td width="10">&nbsp;</td>
<td width="100%" valign="top"><font face="verdana" size="-1">current system date</font></td>
</tr>
<tr>
<td valign="top"><nobr><font face="verdana" size="-2"><b>${TIME}</b></font></nobr></td>
<td width="10">&nbsp;</td>
<td width="100%" valign="top"><font face="verdana" size="-1">current system time</font></td>
</tr>
<tr>
<td valign="top"><nobr><font face="verdana" size="-2"><b>${YEAR}</b></font></nobr></td>
<td width="10">&nbsp;</td>
<td width="100%" valign="top"><font face="verdana" size="-1">current year</font></td>
</tr>
<tr>
<td valign="top"><nobr><font face="verdana" size="-2"><b>${MONTH}</b></font></nobr></td>
<td width="10">&nbsp;</td>
<td width="100%" valign="top"><font face="verdana" size="-1">current month</font></td>
</tr>
<tr>
<td valign="top"><nobr><font face="verdana" size="-2"><b>${MONTH_NAME_SHORT}</b></font></nobr></td>
<td width="10">&nbsp;</td>
<td width="100%" valign="top"><font face="verdana" size="-1">first 3 letters of the current month name. Example: Jan, Feb, etc.</font></td>
</tr>
<tr>
<td valign="top"><nobr><font face="verdana" size="-2"><b>${MONTH_NAME_FULL}</b></font></nobr></td>
<td width="10">&nbsp;</td>
<td width="100%" valign="top"><font face="verdana" size="-1">full name of the current month. Example: January, February, etc.</font></td>
</tr>
<tr>
<td valign="top"><nobr><font face="verdana" size="-2"><b>${DAY}</b></font></nobr></td>
<td width="10">&nbsp;</td>
<td width="100%" valign="top"><font face="verdana" size="-1">current day of the month</font></td>
</tr>
<tr>
<td valign="top"><nobr><font face="verdana" size="-2"><b>${HOUR}</b></font></nobr></td>
<td width="10">&nbsp;</td>
<td width="100%" valign="top"><font face="verdana" size="-1">current hour</font></td>
</tr>
<tr>
<td valign="top"><nobr><font face="verdana" size="-2"><b>${MINUTE}</b></font></nobr></td>
<td width="10">&nbsp;</td>
<td width="100%" valign="top"><font face="verdana" size="-1">current minute</font></td>
</tr>
<tr>
<td valign="top"><nobr><font face="verdana" size="-2"><b>${PROJECT_NAME}</b></font></nobr></td>
<td width="10">&nbsp;</td>
<td width="100%" valign="top"><font face="verdana" size="-1">the name of the current project</font></td>
</tr>
</table>
</body>
</html>
+1
View File
@@ -1400,6 +1400,7 @@
<internalFileTemplate name="Enum"/>
<internalFileTemplate name="AnnotationType" subject="@interface"/>
<internalFileTemplate name="package-info"/>
<internalFileTemplate name="module-info"/>
<saveFileAsTemplateHandler implementation="com.intellij.ide.fileTemplates.SaveJavaAsTemplateHandler"/>
+4 -2
View File
@@ -25,9 +25,11 @@
<add-to-group group-id="NewGroup1" anchor="first"/>
</action>
<action id="NewPackageInfo" class="com.intellij.ide.actions.CreatePackageInfoAction">
<group id="NewJavaSpecialFile">
<action id="NewPackageInfo" class="com.intellij.ide.actions.CreatePackageInfoAction"/>
<action id="NewModuleInfo" class="com.intellij.ide.actions.CreateModuleInfoAction"/>
<add-to-group group-id="NewGroup" anchor="after" relative-to-action="NewDir"/>
</action>
</group>
<action id="CollapseBlock" class="com.intellij.codeInsight.folding.impl.actions.CollapseBlockAction">
<add-to-group group-id="FoldingGroup" anchor="after" relative-to-action="CollapseSelection"/>