diff --git a/java/java-impl/src/com/intellij/ide/actions/CreateModuleInfoAction.java b/java/java-impl/src/com/intellij/ide/actions/CreateModuleInfoAction.java new file mode 100644 index 000000000000..93036467b451 --- /dev/null +++ b/java/java-impl/src/com/intellij/ide/actions/CreateModuleInfoAction.java @@ -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))); + } + } +} \ No newline at end of file diff --git a/java/java-impl/src/com/intellij/ide/fileTemplates/JavaTemplateUtil.java b/java/java-impl/src/com/intellij/ide/fileTemplates/JavaTemplateUtil.java index 45494d341570..ba42df93dd69 100644 --- a/java/java-impl/src/com/intellij/ide/fileTemplates/JavaTemplateUtil.java +++ b/java/java-impl/src/com/intellij/ide/fileTemplates/JavaTemplateUtil.java @@ -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() { } diff --git a/platform/platform-resources-en/src/messages/IdeBundle.properties b/platform/platform-resources-en/src/messages/IdeBundle.properties index c26690fb8057..b86bd2fb0376 100644 --- a/platform/platform-resources-en/src/messages/IdeBundle.properties +++ b/platform/platform-resources-en/src/messages/IdeBundle.properties @@ -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 diff --git a/resources-en/src/fileTemplates/internal/module-info.java.ft b/resources-en/src/fileTemplates/internal/module-info.java.ft new file mode 100644 index 000000000000..53a24634e970 --- /dev/null +++ b/resources-en/src/fileTemplates/internal/module-info.java.ft @@ -0,0 +1,3 @@ +#parse("File Header.java") +module ${MODULE_NAME} { +} \ No newline at end of file diff --git a/resources-en/src/fileTemplates/internal/module-info.java.html b/resources-en/src/fileTemplates/internal/module-info.java.html new file mode 100644 index 000000000000..6604cdb93969 --- /dev/null +++ b/resources-en/src/fileTemplates/internal/module-info.java.html @@ -0,0 +1,86 @@ + +
+| This is a built-in template used each time you create a new module-info.java file, by
+ selecting New | module-info.java from the popup menu in one of the project views. + 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. + It is also possible to specify an arbitrary number of custom variables in the format + ${<VARIABLE_NAME>}. 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. + Using the #parse directive, you can include templates from the Includes + tab, by specifying the full name of the desired template as a parameter in quotation marks. + For example: + #parse("File Header.java") + |
+
| Predefined variables will take the following values: | +||
| + | name of the module in which the new module-info.java file is created | +|
| + | current user system login name | +|
| + | current system date | +|
| + | current system time | +|
| + | current year | +|
| + | current month | +|
| + | first 3 letters of the current month name. Example: Jan, Feb, etc. | +|
| + | full name of the current month. Example: January, February, etc. | +|
| + | current day of the month | +|
| + | current hour | +|
| + | current minute | +|
| + | the name of the current project | +|