From 20f36c672ba0b5c127d023a2dfbd6f8551774225 Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Wed, 17 Aug 2016 13:45:46 +0300 Subject: [PATCH] [java] adds "Create module-info.java" action --- .../ide/actions/CreateModuleInfoAction.java | 83 ++++++++++++++++++ .../ide/fileTemplates/JavaTemplateUtil.java | 1 + .../src/messages/IdeBundle.properties | 2 + .../internal/module-info.java.ft | 3 + .../internal/module-info.java.html | 86 +++++++++++++++++++ resources/src/META-INF/IdeaPlugin.xml | 1 + resources/src/idea/JavaActions.xml | 6 +- 7 files changed, 180 insertions(+), 2 deletions(-) create mode 100644 java/java-impl/src/com/intellij/ide/actions/CreateModuleInfoAction.java create mode 100644 resources-en/src/fileTemplates/internal/module-info.java.ft create mode 100644 resources-en/src/fileTemplates/internal/module-info.java.html 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:
${MODULE_NAME} name of the module in which the new module-info.java file is created
${USER} current user system login name
${DATE} current system date
${TIME} current system time
${YEAR} current year
${MONTH} current month
${MONTH_NAME_SHORT} first 3 letters of the current month name. Example: Jan, Feb, etc.
${MONTH_NAME_FULL} full name of the current month. Example: January, February, etc.
${DAY} current day of the month
${HOUR} current hour
${MINUTE} current minute
${PROJECT_NAME} the name of the current project
+ + \ No newline at end of file diff --git a/resources/src/META-INF/IdeaPlugin.xml b/resources/src/META-INF/IdeaPlugin.xml index b78b5503056c..25ab612d2883 100644 --- a/resources/src/META-INF/IdeaPlugin.xml +++ b/resources/src/META-INF/IdeaPlugin.xml @@ -1400,6 +1400,7 @@ + diff --git a/resources/src/idea/JavaActions.xml b/resources/src/idea/JavaActions.xml index 2884754a0ba1..4b67c1e64d2c 100644 --- a/resources/src/idea/JavaActions.xml +++ b/resources/src/idea/JavaActions.xml @@ -25,9 +25,11 @@ - + + + - +