Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKalyan Prasad Tatavarthi2019-05-16 07:58:28 +0000
committerNoopur Gupta2019-05-21 09:32:03 +0000
commitb7ba888612705256fba8f67a90bde5030465cf83 (patch)
tree2f318e51a3a05a30328ac1c464270b66e562110d
parent9c3a7f04436fbdb6eed1728ec283f98bb828383d (diff)
downloadeclipse.jdt.ui-b7ba888612705256fba8f67a90bde5030465cf83.tar.gz
eclipse.jdt.ui-b7ba888612705256fba8f67a90bde5030465cf83.tar.xz
eclipse.jdt.ui-b7ba888612705256fba8f67a90bde5030465cf83.zip
Bug 545417 - [12] New Code templates for Switch case and ExpressionBETA_JAVA_12
Change-Id: I1b8cb0819e35deafd92c91b65a2fbca04c0b511e Signed-off-by: Kalyan Prasad Tatavarthi <kalyan_prasad@in.ibm.com>
-rw-r--r--org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/util/JavaModelUtil.java6
-rw-r--r--org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/template/contentassist/TemplateEngine.java41
2 files changed, 42 insertions, 5 deletions
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 7c103c7f3d..317305603e 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 final class JavaModelUtil {
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 c9a85547b5..93c3885876 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.Template;
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 @@ public class TemplateEngine {
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 @@ public class TemplateEngine {
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 @@ public class TemplateEngine {
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 @@ public class TemplateEngine {
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

Back to the top