diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/editable/sameKeyBoolean.java b/java/java-tests/testData/codeInsight/template/postfix/templates/editable/sameKeyBoolean.java new file mode 100644 index 000000000000..326143ce4dc8 --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/editable/sameKeyBoolean.java @@ -0,0 +1,5 @@ +public class Foo { + void m(boolean x) { + x.sameKey + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/editable/sameKeyBoolean_after.java b/java/java-tests/testData/codeInsight/template/postfix/templates/editable/sameKeyBoolean_after.java new file mode 100644 index 000000000000..3c6a3ffd5616 --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/editable/sameKeyBoolean_after.java @@ -0,0 +1,5 @@ +public class Foo { + void m(boolean x) { + Boolean.toString(x); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/editable/sameKeyInteger.java b/java/java-tests/testData/codeInsight/template/postfix/templates/editable/sameKeyInteger.java new file mode 100644 index 000000000000..7933e79d4a57 --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/editable/sameKeyInteger.java @@ -0,0 +1,5 @@ +public class Foo { + void m(int x) { + x.sameKey + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/editable/sameKeyInteger_after.java b/java/java-tests/testData/codeInsight/template/postfix/templates/editable/sameKeyInteger_after.java new file mode 100644 index 000000000000..27dcf61cfaba --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/editable/sameKeyInteger_after.java @@ -0,0 +1,5 @@ +public class Foo { + void m(int x) { + Integer.toString(x); + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/template/postfix/templates/SameKeyPostfixTemplatesTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/template/postfix/templates/SameKeyPostfixTemplatesTest.java new file mode 100644 index 000000000000..b871f0a27060 --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/template/postfix/templates/SameKeyPostfixTemplatesTest.java @@ -0,0 +1,46 @@ +// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +package com.intellij.java.codeInsight.template.postfix.templates; + +import com.intellij.codeInsight.template.postfix.settings.PostfixTemplateStorage; +import com.intellij.codeInsight.template.postfix.templates.JavaPostfixTemplateProvider; +import com.intellij.codeInsight.template.postfix.templates.PostfixTemplate; +import com.intellij.codeInsight.template.postfix.templates.editable.JavaEditablePostfixTemplate; +import com.intellij.codeInsight.template.postfix.templates.editable.JavaPostfixTemplateExpressionCondition; +import com.intellij.pom.java.LanguageLevel; +import com.intellij.util.containers.ContainerUtil; +import org.jetbrains.annotations.NotNull; + +import static java.util.Arrays.asList; + +public class SameKeyPostfixTemplatesTest extends PostfixTemplateTestCase { + private static final JavaPostfixTemplateProvider PROVIDER = new JavaPostfixTemplateProvider(); + + @Override + public void setUp() throws Exception { + super.setUp(); + + PostfixTemplate template1 = new JavaEditablePostfixTemplate( + "myId1", "sameKey", "Boolean.toString($EXPR$);$END$", "", + ContainerUtil.set(new JavaPostfixTemplateExpressionCondition.JavaPostfixTemplateBooleanExpressionCondition()), + LanguageLevel.JDK_1_8, true, PROVIDER); + PostfixTemplate template2 = new JavaEditablePostfixTemplate( + "myId2", "sameKey", "Integer.toString($EXPR$);$END$", "", + ContainerUtil.set(new JavaPostfixTemplateExpressionCondition.JavaPostfixTemplateNumberExpressionCondition()), + LanguageLevel.JDK_1_8, true, PROVIDER); + PostfixTemplateStorage.getInstance().setTemplates(PROVIDER, asList(template1, template2)); + } + + public void testSameKeyInteger() { + doTest(); + } + + public void testSameKeyBoolean() { + doTest(); + } + + @NotNull + @Override + protected String getSuffix() { + return "editable"; + } +} diff --git a/platform/lang-impl/src/com/intellij/codeInsight/template/postfix/templates/PostfixLiveTemplate.java b/platform/lang-impl/src/com/intellij/codeInsight/template/postfix/templates/PostfixLiveTemplate.java index 74d60ad9f8fc..4fdca537bf02 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/template/postfix/templates/PostfixLiveTemplate.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/template/postfix/templates/PostfixLiveTemplate.java @@ -113,8 +113,9 @@ public class PostfixLiveTemplate extends CustomLiveTemplateBase { ApplicationManager.getApplication().assertIsDispatchThread(); Editor editor = callback.getEditor(); + PsiFile file = callback.getContext().getContainingFile(); for (PostfixTemplateProvider provider : LanguagePostfixTemplate.LANG_EP.allForLanguage(getLanguage(callback))) { - PostfixTemplate postfixTemplate = findTemplate(provider, key); + PostfixTemplate postfixTemplate = findApplicableTemplate(provider, key, editor, file); if (postfixTemplate != null) { expandTemplate(key, callback, editor, provider, postfixTemplate); return; @@ -136,7 +137,7 @@ public class PostfixLiveTemplate extends CustomLiveTemplateBase { ApplicationManager.getApplication().assertIsDispatchThread(); FeatureUsageTracker.getInstance().triggerFeatureUsed("editing.completion.postfix"); final PsiFile file = callback.getContext().getContainingFile(); - if (isApplicableTemplate(provider, key, file, editor)) { + if (isApplicableTemplate(provider, key, file, editor, postfixTemplate)) { int offset = deleteTemplateKey(file, editor, key); try { provider.preExpand(file, editor); @@ -304,7 +305,15 @@ public class PostfixLiveTemplate extends CustomLiveTemplateBase { @NotNull String key, @NotNull PsiFile file, @NotNull Editor editor) { - return createIsApplicationTemplateFunction(provider, key, file, editor).value(findTemplate(provider, key)); + return findApplicableTemplate(provider, key, editor, file) != null; + } + + private static boolean isApplicableTemplate(@NotNull PostfixTemplateProvider provider, + @NotNull String key, + @NotNull PsiFile file, + @NotNull Editor editor, + @Nullable PostfixTemplate template) { + return createIsApplicationTemplateFunction(provider, key, file, editor).value(template); } @NotNull @@ -317,9 +326,12 @@ public class PostfixLiveTemplate extends CustomLiveTemplateBase { } @Nullable - private static PostfixTemplate findTemplate(@NotNull PostfixTemplateProvider provider, @Nullable String key) { + private static PostfixTemplate findApplicableTemplate(@NotNull PostfixTemplateProvider provider, + @Nullable String key, + @NotNull Editor editor, + @NotNull PsiFile file) { for (PostfixTemplate template : PostfixTemplatesUtils.getAvailableTemplates(provider)) { - if (template.getKey().equals(key)) { + if (template.getKey().equals(key) && isApplicableTemplate(provider, key, file, editor, template)) { return template; } }