IDEA-138603 "From File Template" is not always available

This commit is contained in:
Dmitry Avdeev
2015-04-02 17:35:38 +03:00
parent 740bb5d684
commit 306692ff9e
8 changed files with 95 additions and 2 deletions
@@ -109,6 +109,9 @@ public class JavaCreateFromTemplateHandler implements CreateFromTemplateHandler
}
public boolean canCreate(final PsiDirectory[] dirs) {
for (PsiDirectory dir : dirs) {
if (canCreate(dir)) return true;
}
return false;
}
@@ -0,0 +1,32 @@
/*
* Copyright 2000-2015 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.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;
}
}
@@ -32,11 +32,15 @@ public class JavaTemplateUtil {
@NonNls public static final String TEMPLATE_I18NIZED_EXPRESSION = "I18nized Expression.java";
@NonNls public static final String TEMPLATE_I18NIZED_CONCATENATION = "I18nized Concatenation.java";
@NonNls public static final String TEMPLATE_I18NIZED_JSP_EXPRESSION = "I18nized JSP Expression.jsp";
@NonNls public static final String INTERNAL_CLASS_TEMPLATE_NAME = "Class";
@NonNls public static final String INTERNAL_INTERFACE_TEMPLATE_NAME = "Interface";
@NonNls public static final String INTERNAL_ANNOTATION_TYPE_TEMPLATE_NAME = "AnnotationType";
@NonNls public static final String INTERNAL_ENUM_TEMPLATE_NAME = "Enum";
public static final String[] INTERNAL_JAVA_TEMPLATES = new String[]{INTERNAL_CLASS_TEMPLATE_NAME,
INTERNAL_INTERFACE_TEMPLATE_NAME, INTERNAL_ANNOTATION_TYPE_TEMPLATE_NAME, INTERNAL_ENUM_TEMPLATE_NAME};
private JavaTemplateUtil() {
}
@@ -0,0 +1,46 @@
/*
* Copyright 2000-2015 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.fileTemplates;
import com.intellij.ide.fileTemplates.actions.CreateFromTemplateAction;
import com.intellij.ide.fileTemplates.actions.CreateFromTemplateGroup;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.util.Condition;
import com.intellij.testFramework.TestActionEvent;
import com.intellij.testFramework.TestDataProvider;
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
import com.intellij.util.containers.ContainerUtil;
public class JavaFileTemplatesTest extends LightCodeInsightFixtureTestCase {
public void testCreateFromTemplateGroup() throws Exception {
myFixture.configureByText("foo.java", "");
AnAction[] children = new CreateFromTemplateGroup().getChildren(new TestActionEvent(new TestDataProvider(getProject())));
assertNull(ContainerUtil.find(children, new Condition<AnAction>() {
@Override
public boolean value(AnAction action) {
return action instanceof CreateFromTemplateAction && ((CreateFromTemplateAction)action).getTemplate().getName().equals("Class");
}
}));
assertNotNull(ContainerUtil.find(children, new Condition<AnAction>() {
@Override
public boolean value(AnAction action) {
return action instanceof CreateFromTemplateAction && ((CreateFromTemplateAction)action).getTemplate().getName().equals("Singleton");
}
}));
}
}
@@ -22,6 +22,7 @@ import com.intellij.ide.util.EditorHelper;
import com.intellij.injected.editor.EditorWindow;
import com.intellij.injected.editor.InjectedCaret;
import com.intellij.lang.Language;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Caret;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.ex.EditorEx;
@@ -114,7 +115,7 @@ public class TextEditorPsiDataProvider implements EditorDataProvider {
if (IDE_VIEW.is(dataId)) {
final PsiFile psiFile = project == null ? null : PsiManager.getInstance(project).findFile(file);
final PsiDirectory psiDirectory = psiFile != null ? psiFile.getParent() : null;
if (psiDirectory != null && psiDirectory.isPhysical()) {
if (psiDirectory != null && (psiDirectory.isPhysical() || ApplicationManager.getApplication().isUnitTestMode())) {
return new IdeView() {
@Override
@@ -37,6 +37,10 @@ public class TestActionEvent extends AnActionEvent {
super(null, DataManager.getInstance().getDataContext(), "", presentation, ActionManager.getInstance(), 0);
}
public TestActionEvent(DataContext context) {
super(null, context, "", new Presentation(), ActionManager.getInstance(), 0);
}
public TestActionEvent() {
super(null, DataManager.getInstance().getDataContext(), "", new Presentation(), ActionManager.getInstance(), 0);
}
@@ -16,6 +16,7 @@
package com.intellij.testFramework;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.DataProvider;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.editor.Editor;
@@ -32,7 +33,7 @@ import javax.swing.*;
/**
* @author peter
*/
public class TestDataProvider implements DataProvider {
public class TestDataProvider implements DataProvider, DataContext {
private final Project myProject;
public TestDataProvider(@NotNull Project project) {
+2
View File
@@ -1273,7 +1273,9 @@
<projectService serviceInterface="com.intellij.ide.todo.nodes.TodoTreeHelper"
serviceImplementation="com.intellij.ide.todo.nodes.TodoJavaTreeHelper"/>
<createFromTemplateHandler implementation="com.intellij.ide.fileTemplates.JavaInternalTemplatesHandler"/>
<createFromTemplateHandler implementation="com.intellij.ide.fileTemplates.JavaCreateFromTemplateHandler"/>
<defaultTemplatePropertiesProvider implementation="com.intellij.ide.fileTemplates.TemplatePackagePropertyProvider"/>
<favoriteNodeProvider implementation="com.intellij.ide.favoritesTreeView.PsiClassFavoriteNodeProvider"/>
<favoriteNodeProvider implementation="com.intellij.ide.favoritesTreeView.PsiFieldFavoriteNodeProvider"/>