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 new file mode 100644 index 000000000000..5d99745168a4 --- /dev/null +++ b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/FormatPostfixTemplate.java @@ -0,0 +1,43 @@ +/* + * Copyright 2000-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.codeInsight.template.postfix.templates; + +import com.intellij.codeInsight.template.postfix.util.PostfixTemplatesUtils; +import com.intellij.openapi.editor.Document; +import com.intellij.openapi.editor.Editor; +import com.intellij.psi.CommonClassNames; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiExpression; +import com.intellij.psi.PsiType; +import org.jetbrains.annotations.NotNull; + +public class FormatPostfixTemplate extends PostfixTemplate { + public FormatPostfixTemplate() { + super("format", "Creates String.format call", "String.format(expr);"); + } + + @Override + public void expand(@NotNull PsiElement context, @NotNull Editor editor) { + PostfixTemplatesUtils.createStatement(context, editor, "String.format(", ", )", -2); + } + + @Override + public boolean isApplicable(@NotNull PsiElement context, @NotNull Document copyDocument, int newOffset) { + PsiExpression expr = getTopmostExpression(context); + PsiType type = expr != null ? expr.getType() : null; + return expr != null && type != null && CommonClassNames.JAVA_LANG_STRING.equals(type.getCanonicalText()); + } +} diff --git a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/NonVoidPostfixTemplate.java b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/NonVoidPostfixTemplate.java new file mode 100644 index 000000000000..906a943e15e6 --- /dev/null +++ b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/NonVoidPostfixTemplate.java @@ -0,0 +1,34 @@ +/* + * Copyright 2000-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.codeInsight.template.postfix.templates; + +import com.intellij.codeInsight.template.postfix.util.PostfixTemplatesUtils; +import com.intellij.openapi.editor.Document; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiExpression; +import org.jetbrains.annotations.NotNull; + +abstract public class NonVoidPostfixTemplate extends PostfixTemplate { + protected NonVoidPostfixTemplate(@NotNull String name, @NotNull String description, @NotNull String example) { + super(name, description, example); + } + + @Override + public boolean isApplicable(@NotNull PsiElement context, @NotNull Document copyDocument, int newOffset) { + PsiExpression expr = getTopmostExpression(context); + return expr != null && PostfixTemplatesUtils.isNonVoid(expr.getType()); + } +} diff --git a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ReturnStatementPostfixTemplate.java b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ReturnStatementPostfixTemplate.java index e15f6c4940c3..277a6f2c0d30 100644 --- a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ReturnStatementPostfixTemplate.java +++ b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ReturnStatementPostfixTemplate.java @@ -15,24 +15,15 @@ */ package com.intellij.codeInsight.template.postfix.templates; -import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.Editor; import com.intellij.psi.*; import org.jetbrains.annotations.NotNull; -public class ReturnStatementPostfixTemplate extends PostfixTemplate { +public class ReturnStatementPostfixTemplate extends NonVoidPostfixTemplate { public ReturnStatementPostfixTemplate() { super("return", "Returns value from containing method", "return expr;"); } - @Override - public boolean isApplicable(@NotNull PsiElement context, @NotNull Document copyDocument, int newOffset) { - PsiExpression expr = getTopmostExpression(context); - if (expr == null) return false; - PsiType type = expr.getType(); - return type != null && !PsiType.VOID.equals(type); - } - @Override public void expand(@NotNull PsiElement context, @NotNull Editor editor) { PsiExpression expr = getTopmostExpression(context); diff --git a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/SoutPostfixTemplate.java b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/SoutPostfixTemplate.java new file mode 100644 index 000000000000..4f0667f5af5e --- /dev/null +++ b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/SoutPostfixTemplate.java @@ -0,0 +1,32 @@ +/* + * Copyright 2000-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.codeInsight.template.postfix.templates; + +import com.intellij.codeInsight.template.postfix.util.PostfixTemplatesUtils; +import com.intellij.openapi.editor.Editor; +import com.intellij.psi.PsiElement; +import org.jetbrains.annotations.NotNull; + +public class SoutPostfixTemplate extends NonVoidPostfixTemplate { + public SoutPostfixTemplate() { + super("sout", "Creates System.out.println call", "System.out.println(expr);"); + } + + @Override + public void expand(@NotNull PsiElement context, @NotNull Editor editor) { + PostfixTemplatesUtils.createStatement(context, editor, "System.out.println(", ")"); + } +} diff --git a/java/java-impl/src/com/intellij/codeInsight/template/postfix/util/PostfixTemplatesUtils.java b/java/java-impl/src/com/intellij/codeInsight/template/postfix/util/PostfixTemplatesUtils.java index 09cf4fb19112..370c986c5f48 100644 --- a/java/java-impl/src/com/intellij/codeInsight/template/postfix/util/PostfixTemplatesUtils.java +++ b/java/java-impl/src/com/intellij/codeInsight/template/postfix/util/PostfixTemplatesUtils.java @@ -37,13 +37,21 @@ public abstract class PostfixTemplatesUtils { } public static void createSimpleStatement(@NotNull PsiElement context, @NotNull Editor editor, @NotNull String text) { + createStatement(context, editor, text + " ", ""); + } + + public static void createStatement(@NotNull PsiElement context, @NotNull Editor editor, @NotNull String prefix, @NotNull String suffix) { + createStatement(context, editor, prefix, suffix, 0); + } + + public static void createStatement(@NotNull PsiElement context, @NotNull Editor editor, @NotNull String prefix, @NotNull String suffix, int offset) { PsiExpression expr = PostfixTemplate.getTopmostExpression(context); PsiElement parent = expr != null ? expr.getParent() : null; assert parent instanceof PsiStatement; PsiElementFactory factory = JavaPsiFacade.getInstance(context.getProject()).getElementFactory(); - PsiStatement assertStatement = factory.createStatementFromText(text + " " + expr.getText() + ";", parent); - PsiElement replace = parent.replace(assertStatement); - editor.getCaretModel().moveToOffset(replace.getTextRange().getEndOffset()); + PsiStatement statement = factory.createStatementFromText(prefix + expr.getText() + suffix + ";", parent); + PsiElement replace = parent.replace(statement); + editor.getCaretModel().moveToOffset(replace.getTextRange().getEndOffset() + offset); } @Contract("null -> false") @@ -71,6 +79,11 @@ public abstract class PostfixTemplatesUtils { return type != null && (PsiType.BOOLEAN.equals(type) || PsiType.BOOLEAN.equals(PsiPrimitiveType.getUnboxedType(type))); } + @Contract("null -> false") + public static boolean isNonVoid(@Nullable PsiType type) { + return type != null && !PsiType.VOID.equals(type); + } + @Contract("null -> false") public static boolean isNumber(@Nullable PsiType type) { if (type == null) { diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/format/notString.java b/java/java-tests/testData/codeInsight/template/postfix/templates/format/notString.java new file mode 100644 index 000000000000..f95b6df27253 --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/format/notString.java @@ -0,0 +1,7 @@ +package templates; + +public class Foo { + void m(boolean b, int value) { + b.format + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/format/notString_after.java b/java/java-tests/testData/codeInsight/template/postfix/templates/format/notString_after.java new file mode 100644 index 000000000000..e828f4da8931 --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/format/notString_after.java @@ -0,0 +1,7 @@ +package templates; + +public class Foo { + void m(boolean b, int value) { + b.format + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/format/string.java b/java/java-tests/testData/codeInsight/template/postfix/templates/format/string.java new file mode 100644 index 000000000000..0cd8302e1b83 --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/format/string.java @@ -0,0 +1,7 @@ +package templates; + +public class Foo { + void m(boolean b, int value) { + "m()".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 new file mode 100644 index 000000000000..a0b3172a42b8 --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/format/string_after.java @@ -0,0 +1,7 @@ +package templates; + +public class Foo { + void m(boolean b, int value) { + String.format("m()", ); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/sout/simple.java b/java/java-tests/testData/codeInsight/template/postfix/templates/sout/simple.java new file mode 100644 index 000000000000..80da2b8ad620 --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/sout/simple.java @@ -0,0 +1,7 @@ +package templates; + +public class Foo { + void m(boolean b, int value) { + b.sout + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/sout/simple_after.java b/java/java-tests/testData/codeInsight/template/postfix/templates/sout/simple_after.java new file mode 100644 index 000000000000..3b0a8dd051ad --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/sout/simple_after.java @@ -0,0 +1,7 @@ +package templates; + +public class Foo { + void m(boolean b, int value) { + System.out.println(b); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/sout/void.java b/java/java-tests/testData/codeInsight/template/postfix/templates/sout/void.java new file mode 100644 index 000000000000..b99a577e98b9 --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/sout/void.java @@ -0,0 +1,7 @@ +package templates; + +public class Foo { + void m(boolean b, int value) { + m().sout + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/sout/void_after.java b/java/java-tests/testData/codeInsight/template/postfix/templates/sout/void_after.java new file mode 100644 index 000000000000..4e08abc92f85 --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/sout/void_after.java @@ -0,0 +1,7 @@ +package templates; + +public class Foo { + void m(boolean b, int value) { + m().sout + } +} \ 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 new file mode 100644 index 000000000000..fc336e077aa8 --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/codeInsight/template/postfix/templates/FormatPostfixTemplateTest.java @@ -0,0 +1,27 @@ +/* + * Copyright 2000-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.codeInsight.template.postfix.templates; + +import org.jetbrains.annotations.NotNull; + +public class FormatPostfixTemplateTest extends PostfixTemplateTestCase { + @NotNull + @Override + protected String getSuffix() { return "format"; } + + public void testString() { doTest(); } + public void testNotString() { doTest(); } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/template/postfix/templates/SoutPostfixTemplateTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/template/postfix/templates/SoutPostfixTemplateTest.java new file mode 100644 index 000000000000..d3bcc74d4684 --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/codeInsight/template/postfix/templates/SoutPostfixTemplateTest.java @@ -0,0 +1,27 @@ +/* + * Copyright 2000-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.codeInsight.template.postfix.templates; + +import org.jetbrains.annotations.NotNull; + +public class SoutPostfixTemplateTest extends PostfixTemplateTestCase { + @NotNull + @Override + protected String getSuffix() { return "sout"; } + + public void testSimple() { doTest(); } + public void testVoid() { doTest(); } +} \ No newline at end of file diff --git a/resources/src/META-INF/PostfixTemplates.xml b/resources/src/META-INF/PostfixTemplates.xml index 367c9f8cc0e4..91505d6149d1 100644 --- a/resources/src/META-INF/PostfixTemplates.xml +++ b/resources/src/META-INF/PostfixTemplates.xml @@ -23,6 +23,8 @@ + +