diff options
| author | Dani Megert | 2016-02-26 10:59:48 +0000 |
|---|---|---|
| committer | Dani Megert | 2016-02-26 10:59:48 +0000 |
| commit | 9fcab2e1cab25e550ad26f0ad972752664d4a265 (patch) | |
| tree | 6ee12da90f2d7b6a71bd38ec36fa3c5642dad9fa | |
| parent | 72b6d18666c696d17438d410be02d0da7b68d692 (diff) | |
| download | eclipse.jdt.ui-9fcab2e1cab25e550ad26f0ad972752664d4a265.tar.gz eclipse.jdt.ui-9fcab2e1cab25e550ad26f0ad972752664d4a265.tar.xz eclipse.jdt.ui-9fcab2e1cab25e550ad26f0ad972752664d4a265.zip | |
Fixed bug 488542: [templates] Surround With does not show templates that specify a default for the selection variable
2 files changed, 17 insertions, 9 deletions
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/QuickTemplateProcessor.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/QuickTemplateProcessor.java index 3205cf6de5..08f9d8de09 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/QuickTemplateProcessor.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/QuickTemplateProcessor.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2015 IBM Corporation and others. + * Copyright (c) 2000, 2016 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -14,6 +14,8 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Comparator; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import com.ibm.icu.text.Collator; @@ -70,8 +72,8 @@ import org.eclipse.jdt.internal.ui.text.template.contentassist.TemplateProposal; */ public class QuickTemplateProcessor implements IQuickAssistProcessor { - private static final String $_LINE_SELECTION= "${" + GlobalTemplateVariables.LineSelection.NAME + "}"; //$NON-NLS-1$ //$NON-NLS-2$ - private static final String $_WORD_SELECTION= "${" + GlobalTemplateVariables.WordSelection.NAME + "}"; //$NON-NLS-1$ //$NON-NLS-2$ + 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$ public QuickTemplateProcessor() { } @@ -218,11 +220,13 @@ public class QuickTemplateProcessor implements IQuickAssistProcessor { private boolean canEvaluate(TemplateContext context, Template template) { String contextId= context.getContextType().getId(); + Matcher wordSelectionMatcher= $_WORD_SELECTION_PATTERN.matcher(template.getPattern()); + Matcher lineSelectionMatcher= $_LINE_SELECTION_PATTERN.matcher(template.getPattern()); if (JavaDocContextType.ID.equals(contextId)) { - if (!template.matches("", contextId) || template.getPattern().indexOf($_LINE_SELECTION) == -1 && template.getPattern().indexOf($_WORD_SELECTION) == -1) //$NON-NLS-1$ + if (!template.matches("", contextId) || !lineSelectionMatcher.find() && !wordSelectionMatcher.find()) //$NON-NLS-1$ return false; } else { - if (template.matches("", JavaDocContextType.ID) || template.getPattern().indexOf($_LINE_SELECTION) == -1) //$NON-NLS-1$ + if (template.matches("", JavaDocContextType.ID) || !lineSelectionMatcher.find()) //$NON-NLS-1$ return false; } TemplateContextType contextType= JavaPlugin.getDefault().getTemplateContextRegistry().getContextType(template.getContextTypeId()); diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/template/contentassist/SurroundWithTemplateProposal.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/template/contentassist/SurroundWithTemplateProposal.java index 716d8bd42e..21671f1704 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/template/contentassist/SurroundWithTemplateProposal.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/template/contentassist/SurroundWithTemplateProposal.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2015 IBM Corporation and others. + * Copyright (c) 2000, 2016 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -13,6 +13,8 @@ package org.eclipse.jdt.internal.ui.text.template.contentassist; import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.Point; @@ -62,7 +64,7 @@ public class SurroundWithTemplateProposal extends TemplateProposal { private static class SurroundWithTemplate extends SurroundWith { - private static final String $_LINE_SELECTION= "${" + GlobalTemplateVariables.LineSelection.NAME + "}"; //$NON-NLS-1$ //$NON-NLS-2$ + private static final Pattern $_LINE_SELECTION_PATTERN= Pattern.compile("\\$\\{(.*:)?" + GlobalTemplateVariables.LineSelection.NAME + "(\\(.*\\))?\\}"); //$NON-NLS-1$ //$NON-NLS-2$ private final Template fTemplate; private final IJavaProject fCurrentProject; @@ -87,12 +89,14 @@ public class SurroundWithTemplateProposal extends TemplateProposal { final String templateVariableRegEx= "\\$\\{[^\\}]*\\}"; //$NON-NLS-1$ String template= fTemplate.getPattern(); - int currentPosition= template.indexOf($_LINE_SELECTION); + Matcher lineSelectionMatcher= $_LINE_SELECTION_PATTERN.matcher(template); + int currentPosition= lineSelectionMatcher.find() ? lineSelectionMatcher.start() : -1; int insertionPosition= -1; while (currentPosition != -1) { insertionPosition= currentPosition; template= template.replaceFirst(templateVariableRegEx, ""); //$NON-NLS-1$ - currentPosition= template.indexOf($_LINE_SELECTION); + lineSelectionMatcher= $_LINE_SELECTION_PATTERN.matcher(template); + currentPosition= lineSelectionMatcher.find() ? lineSelectionMatcher.start() : -1; } template= template.replaceAll(templateVariableRegEx, ""); //$NON-NLS-1$ |
