Bug 545417 - [12] New Code templates for Switch case and Expression
Change-Id: I1b8cb0819e35deafd92c91b65a2fbca04c0b511e
Signed-off-by: Kalyan Prasad Tatavarthi <kalyan_prasad@in.ibm.com>
diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/util/JavaModelUtil.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/util/JavaModelUtil.java
index 7c103c7..3173056 100644
--- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/util/JavaModelUtil.java
+++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/util/JavaModelUtil.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2018 IBM Corporation and others.
+ * Copyright (c) 2000, 2019 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
@@ -885,6 +885,10 @@
public static boolean is11OrHigher(IJavaProject project) {
return is11OrHigher(getSourceCompliance(project));
}
+
+ public static boolean is12OrHigher(IJavaProject project) {
+ return is12OrHigher(getSourceCompliance(project));
+ }
private static String getSourceCompliance(IJavaProject project) {
return project != null ? project.getOption(JavaCore.COMPILER_SOURCE, true) : JavaCore.getOption(JavaCore.COMPILER_SOURCE);
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/template/contentassist/TemplateEngine.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/template/contentassist/TemplateEngine.java
index c9a8554..93c3885 100644
--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/template/contentassist/TemplateEngine.java
+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/template/contentassist/TemplateEngine.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2016 IBM Corporation and others.
+ * Copyright (c) 2000, 2019 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
@@ -39,10 +39,12 @@
import org.eclipse.jface.text.templates.TemplateContextType;
import org.eclipse.jdt.core.ICompilationUnit;
+import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.internal.corext.template.java.CompilationUnitContext;
import org.eclipse.jdt.internal.corext.template.java.CompilationUnitContextType;
import org.eclipse.jdt.internal.corext.template.java.SWTContextType;
+import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
import org.eclipse.jdt.internal.ui.JavaPlugin;
import org.eclipse.jdt.internal.ui.JavaPluginImages;
@@ -53,6 +55,9 @@
private static final Pattern $_LINE_SELECTION_PATTERN= Pattern.compile("\\$\\{(.*:)?" + GlobalTemplateVariables.LineSelection.NAME + "(\\(.*\\))?\\}"); //$NON-NLS-1$ //$NON-NLS-2$
private static final Pattern $_WORD_SELECTION_PATTERN= Pattern.compile("\\$\\{(.*:)?" + GlobalTemplateVariables.WordSelection.NAME + "(\\(.*\\))?\\}"); //$NON-NLS-1$ //$NON-NLS-2$
+
+ private static String Switch_Name = "switch"; //$NON-NLS-1$
+ private static String Switch_Default = "switch case statement"; //$NON-NLS-1$
/** The context type. */
private TemplateContextType fContextType;
@@ -130,11 +135,11 @@
IRegion region= new Region(start, end - start);
Template[] templates= JavaPlugin.getDefault().getTemplateStore().getTemplates();
-
+ boolean needsCheck= !isJava12OrHigherProject(compilationUnit);
if (selection.y == 0) {
for (int i= 0; i != templates.length; i++) {
Template template= templates[i];
- if (context.canEvaluate(template)) {
+ if (canEvaluate(context, template, needsCheck)) {
fProposals.add(new TemplateProposal(template, context, region, getImage()));
}
}
@@ -147,7 +152,7 @@
for (int i= 0; i != templates.length; i++) {
Template template= templates[i];
- if (context.canEvaluate(template))
+ if (canEvaluate(context, template, needsCheck))
{
Matcher wordSelectionMatcher= $_WORD_SELECTION_PATTERN.matcher(template.getPattern());
Matcher lineSelectionMatcher= $_LINE_SELECTION_PATTERN.matcher(template.getPattern());
@@ -174,6 +179,34 @@
return JavaPluginImages.get(JavaPluginImages.IMG_OBJS_TEMPLATE);
}
+ private boolean isJava12OrHigherProject(ICompilationUnit compUnit) {
+ if (compUnit != null) {
+ IJavaProject javaProject= compUnit.getJavaProject();
+ return JavaModelUtil.is12OrHigher(javaProject);
+ }
+ return false;
+ }
+
+ private boolean isTemplateAllowed(Template template) {
+ if (Switch_Name.equals(template.getName())) {
+ if (Switch_Default.equals(template.getDescription())) {
+ return true;
+ }
+ return false;
+ }
+ return true;
+ }
+
+ private boolean canEvaluate(CompilationUnitContext context, Template template, boolean needsCheck) {
+ if (!needsCheck) {
+ return context.canEvaluate(template);
+ }
+ if (isTemplateAllowed(template)) {
+ return context.canEvaluate(template);
+ }
+ return false;
+ }
+
/**
* Returns <code>true</code> if one line is completely selected or if multiple lines are selected.
* Being completely selected means that all characters except the new line characters are