mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[platform] "package-info" template references moved to Java module
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2000-2014 JetBrains s.r.o.
|
||||
* 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.
|
||||
@@ -21,29 +21,34 @@ import com.intellij.ide.IdeBundle;
|
||||
import com.intellij.ide.IdeView;
|
||||
import com.intellij.ide.fileTemplates.FileTemplate;
|
||||
import com.intellij.ide.fileTemplates.FileTemplateManager;
|
||||
import com.intellij.ide.fileTemplates.FileTemplateUtil;
|
||||
import com.intellij.ide.fileTemplates.actions.AttributesDefaults;
|
||||
import com.intellij.ide.fileTemplates.actions.CreateFromTemplateActionBase;
|
||||
import com.intellij.openapi.actionSystem.*;
|
||||
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.project.DumbAware;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.roots.ProjectFileIndex;
|
||||
import com.intellij.openapi.roots.ProjectRootManager;
|
||||
import com.intellij.openapi.ui.Messages;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.JavaDirectoryService;
|
||||
import com.intellij.psi.PsiDirectory;
|
||||
import com.intellij.psi.PsiNameHelper;
|
||||
import com.intellij.psi.PsiPackage;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jps.model.java.JavaModuleSourceRootTypes;
|
||||
|
||||
import static com.intellij.ide.fileTemplates.JavaTemplateUtil.INTERNAL_PACKAGE_INFO_TEMPLATE_NAME;
|
||||
|
||||
/**
|
||||
* @author Bas Leijdekkers
|
||||
*/
|
||||
public class CreatePackageInfoAction extends CreateFromTemplateActionBase implements DumbAware {
|
||||
|
||||
public CreatePackageInfoAction() {
|
||||
super(IdeBundle.message("action.create.new.package-info.title"),
|
||||
IdeBundle.message("action.create.new.package-info.description"), AllIcons.FileTypes.Java);
|
||||
super(IdeBundle.message("action.create.new.package-info.title"), IdeBundle.message("action.create.new.package-info.description"), AllIcons.FileTypes.Java);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@@ -112,11 +117,11 @@ public class CreatePackageInfoAction extends CreateFromTemplateActionBase implem
|
||||
@Nullable
|
||||
@Override
|
||||
public AttributesDefaults getAttributesDefaults(DataContext dataContext) {
|
||||
return new AttributesDefaults(FileTemplateUtil.INTERNAL_PACKAGE_INFO_TEMPLATE_NAME).withFixedName(true);
|
||||
return new AttributesDefaults(INTERNAL_PACKAGE_INFO_TEMPLATE_NAME).withFixedName(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected FileTemplate getTemplate(Project project, PsiDirectory dir) {
|
||||
return FileTemplateManager.getInstance(project).getInternalTemplate(FileTemplateUtil.INTERNAL_PACKAGE_INFO_TEMPLATE_NAME);
|
||||
return FileTemplateManager.getInstance(project).getInternalTemplate(INTERNAL_PACKAGE_INFO_TEMPLATE_NAME);
|
||||
}
|
||||
}
|
||||
}
|
||||
+21
-7
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2000-2013 JetBrains s.r.o.
|
||||
* 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.
|
||||
@@ -24,6 +24,7 @@ import com.intellij.pom.java.LanguageLevel;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.CodeStyleManager;
|
||||
import com.intellij.psi.impl.file.JavaDirectoryServiceImpl;
|
||||
import com.intellij.util.ArrayUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@@ -94,20 +95,27 @@ public class JavaCreateFromTemplateHandler implements CreateFromTemplateHandler
|
||||
}
|
||||
}
|
||||
|
||||
public boolean handlesTemplate(final FileTemplate template) {
|
||||
@Override
|
||||
public boolean handlesTemplate(@NotNull FileTemplate template) {
|
||||
FileType fileType = FileTypeManagerEx.getInstanceEx().getFileTypeByExtension(template.getExtension());
|
||||
return fileType.equals(StdFileTypes.JAVA) && !FileTemplateUtil.INTERNAL_PACKAGE_INFO_TEMPLATE_NAME.equals(template.getName());
|
||||
return fileType.equals(StdFileTypes.JAVA) && ArrayUtil.contains(template.getName(), JavaTemplateUtil.INTERNAL_CLASS_TEMPLATES);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public PsiElement createFromTemplate(final Project project, final PsiDirectory directory, final String fileName, FileTemplate template,
|
||||
String templateText, @NotNull Map<String, Object> props) throws IncorrectOperationException {
|
||||
@Override
|
||||
public PsiElement createFromTemplate(Project project,
|
||||
PsiDirectory directory,
|
||||
String fileName,
|
||||
FileTemplate template,
|
||||
String templateText,
|
||||
@NotNull Map<String, Object> props) throws IncorrectOperationException {
|
||||
String extension = template.getExtension();
|
||||
PsiElement result = createClassOrInterface(project, directory, templateText, template.isReformatCode(), extension);
|
||||
hackAwayEmptyPackage((PsiJavaFile)result.getContainingFile(), template, props);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canCreate(final PsiDirectory[] dirs) {
|
||||
for (PsiDirectory dir : dirs) {
|
||||
if (canCreate(dir)) return true;
|
||||
@@ -128,12 +136,18 @@ public class JavaCreateFromTemplateHandler implements CreateFromTemplateHandler
|
||||
@Override
|
||||
public void prepareProperties(Map<String, Object> props) {
|
||||
String packageName = (String)props.get(FileTemplate.ATTRIBUTE_PACKAGE_NAME);
|
||||
if(packageName == null || packageName.length() == 0){
|
||||
if (packageName == null || packageName.length() == 0) {
|
||||
props.put(FileTemplate.ATTRIBUTE_PACKAGE_NAME, FileTemplate.ATTRIBUTE_PACKAGE_NAME);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String commandName(@NotNull FileTemplate template) {
|
||||
return IdeBundle.message("command.create.class.from.template");
|
||||
}
|
||||
|
||||
public static boolean canCreate(PsiDirectory dir) {
|
||||
return JavaDirectoryService.getInstance().getPackage(dir) != null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2000-2015 JetBrains s.r.o.
|
||||
* 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.
|
||||
@@ -16,17 +16,10 @@
|
||||
package com.intellij.ide.fileTemplates;
|
||||
|
||||
import com.intellij.psi.PsiDirectory;
|
||||
import com.intellij.util.ArrayUtil;
|
||||
|
||||
public class JavaInternalTemplatesHandler extends JavaCreateFromTemplateHandler {
|
||||
|
||||
@Override
|
||||
public boolean handlesTemplate(FileTemplate template) {
|
||||
return ArrayUtil.contains(template.getName(), JavaTemplateUtil.INTERNAL_JAVA_TEMPLATES);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canCreate(PsiDirectory[] dirs) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -39,9 +39,11 @@ public class JavaTemplateUtil {
|
||||
public static final String INTERNAL_ANNOTATION_TYPE_TEMPLATE_NAME = "AnnotationType";
|
||||
public static final String INTERNAL_ENUM_TEMPLATE_NAME = "Enum";
|
||||
|
||||
public static final String[] INTERNAL_JAVA_TEMPLATES = {
|
||||
public static final String[] INTERNAL_CLASS_TEMPLATES = {
|
||||
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";
|
||||
|
||||
private JavaTemplateUtil() { }
|
||||
|
||||
public static void setClassAndMethodNameProperties (@NotNull Properties properties, @NotNull PsiClass aClass, @NotNull PsiMethod method) {
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package com.intellij.ide.fileTemplates;
|
||||
|
||||
import com.intellij.ide.IdeBundle;
|
||||
import com.intellij.openapi.extensions.ExtensionPointName;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.PsiDirectory;
|
||||
@@ -47,4 +48,9 @@ public interface CreateFromTemplateHandler {
|
||||
String getErrorMessage();
|
||||
|
||||
void prepareProperties(Map<String, Object> props);
|
||||
|
||||
@NotNull
|
||||
default String commandName(@NotNull FileTemplate template) {
|
||||
return IdeBundle.message("command.create.file.from.template");
|
||||
}
|
||||
}
|
||||
@@ -24,7 +24,6 @@ import com.intellij.openapi.extensions.Extensions;
|
||||
import com.intellij.openapi.fileTypes.FileType;
|
||||
import com.intellij.openapi.fileTypes.FileTypeManager;
|
||||
import com.intellij.openapi.fileTypes.FileTypes;
|
||||
import com.intellij.openapi.fileTypes.StdFileTypes;
|
||||
import com.intellij.openapi.fileTypes.ex.FileTypeManagerEx;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.project.ProjectManager;
|
||||
@@ -60,8 +59,6 @@ import java.util.regex.Pattern;
|
||||
* @author MYakovlev
|
||||
*/
|
||||
public class FileTemplateUtil {
|
||||
public static final String INTERNAL_PACKAGE_INFO_TEMPLATE_NAME = "package-info";
|
||||
|
||||
private static final Logger LOG = Logger.getInstance("#com.intellij.ide.fileTemplates.FileTemplateUtil");
|
||||
private static final CreateFromTemplateHandler DEFAULT_HANDLER = new DefaultCreateFromTemplateHandler();
|
||||
|
||||
@@ -340,8 +337,7 @@ public class FileTemplateUtil {
|
||||
commandException[0] = ex;
|
||||
}
|
||||
}),
|
||||
IdeBundle.message(template.isTemplateOfType(StdFileTypes.JAVA) && !"package-info".equals(template.getName()) ?
|
||||
"command.create.class.from.template" : "command.create.file.from.template"),
|
||||
handler.commandName(template),
|
||||
null);
|
||||
|
||||
if (commandException[0] != null) {
|
||||
@@ -371,7 +367,6 @@ public class FileTemplateUtil {
|
||||
return methodText.replaceAll("\n", "\n" + StringUtil.repeatSymbol(' ', indent));
|
||||
}
|
||||
|
||||
|
||||
public static boolean canCreateFromTemplate(PsiDirectory[] dirs, FileTemplate template) {
|
||||
FileType fileType = FileTypeManagerEx.getInstanceEx().getFileTypeByExtension(template.getExtension());
|
||||
if (fileType.equals(FileTypes.UNKNOWN)) return false;
|
||||
@@ -422,7 +417,7 @@ public class FileTemplateUtil {
|
||||
|
||||
private static String templateToRegex(String text, TIntObjectHashMap<String> offsetToProperty, Project project) {
|
||||
List<Object> properties = ContainerUtil.newArrayList(FileTemplateManager.getInstance(project).getDefaultProperties().keySet());
|
||||
properties.add("PACKAGE_NAME");
|
||||
properties.add(FileTemplate.ATTRIBUTE_PACKAGE_NAME);
|
||||
|
||||
String regex = escapeRegexChars(text);
|
||||
// first group is a whole file header
|
||||
|
||||
Reference in New Issue
Block a user