diff --git a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/FormatPostfixTemplate.java b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/FormatPostfixTemplate.java index 688f5476135d..5b88af016709 100644 --- a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/FormatPostfixTemplate.java +++ b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/FormatPostfixTemplate.java @@ -23,7 +23,7 @@ import com.intellij.psi.PsiType; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import static com.intellij.codeInsight.template.postfix.util.JavaPostfixTemplatesUtils.selectorTopmost; +import static com.intellij.codeInsight.template.postfix.util.JavaPostfixTemplatesUtils.selectorAllExpressionsWithCurrentOffset; public class FormatPostfixTemplate extends StringBasedPostfixTemplate { private static final Condition IS_STRING = new Condition() { @@ -39,13 +39,18 @@ public class FormatPostfixTemplate extends StringBasedPostfixTemplate { public FormatPostfixTemplate() { - super("format", "String.format(expr)", selectorTopmost(IS_STRING)); + super("format", "String.format(expr)", selectorAllExpressionsWithCurrentOffset(IS_STRING)); } @Nullable @Override public String getTemplateString(@NotNull PsiElement element) { - return "String.format($expr$, $END$);"; + return "String.format($expr$, $END$)"; + } + + @Override + protected boolean shouldRemoveParent() { + return false; } } diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/format/expression.java b/java/java-tests/testData/codeInsight/template/postfix/templates/format/expression.java new file mode 100644 index 000000000000..a56771c3799e --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/format/expression.java @@ -0,0 +1,9 @@ +package templates; + +import java.lang.Exception; + +public class Foo { + void m(boolean b, int value) { + throw new Exception("".format); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/format/expressionNotCompleted.java b/java/java-tests/testData/codeInsight/template/postfix/templates/format/expressionNotCompleted.java new file mode 100644 index 000000000000..96fdbb685d41 --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/format/expressionNotCompleted.java @@ -0,0 +1,9 @@ +package templates; + +import java.lang.Exception; + +public class Foo { + void m(boolean b, int value) { + throw new Exception("".format + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/format/expressionNotCompleted_after.java b/java/java-tests/testData/codeInsight/template/postfix/templates/format/expressionNotCompleted_after.java new file mode 100644 index 000000000000..ba58e6a53784 --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/format/expressionNotCompleted_after.java @@ -0,0 +1,9 @@ +package templates; + +import java.lang.Exception; + +public class Foo { + void m(boolean b, int value) { + throw new Exception(String.format("", ) + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/format/expression_after.java b/java/java-tests/testData/codeInsight/template/postfix/templates/format/expression_after.java new file mode 100644 index 000000000000..398bb1cb985d --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/format/expression_after.java @@ -0,0 +1,9 @@ +package templates; + +import java.lang.Exception; + +public class Foo { + void m(boolean b, int value) { + throw new Exception(String.format("", )); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/format/string_after.java b/java/java-tests/testData/codeInsight/template/postfix/templates/format/string_after.java index a0b3172a42b8..d4585afe2057 100644 --- a/java/java-tests/testData/codeInsight/template/postfix/templates/format/string_after.java +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/format/string_after.java @@ -2,6 +2,6 @@ package templates; public class Foo { void m(boolean b, int value) { - String.format("m()", ); + String.format("m()", ) } } \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/template/postfix/templates/FormatPostfixTemplateTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/template/postfix/templates/FormatPostfixTemplateTest.java index 24e98054da34..36b0f5175b4f 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/template/postfix/templates/FormatPostfixTemplateTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/template/postfix/templates/FormatPostfixTemplateTest.java @@ -31,4 +31,12 @@ public class FormatPostfixTemplateTest extends PostfixTemplateTestCase { public void testNotString() { doTest(); } + + public void testExpression() { + doTest(); + } + + public void testExpressionNotCompleted() { + doTest(); + } } \ No newline at end of file