From 8aa8ca5de25cad44c94a0b3fee300e8d1dc270cf Mon Sep 17 00:00:00 2001 From: Alexander Zolotov Date: Mon, 13 Jan 2014 15:40:55 +0400 Subject: [PATCH] Postfix templates: fix template for anonymous classes --- .../templates/BooleanPostfixTemplate.java | 20 ++++++++++++---- .../templates/ForIndexedPostfixTemplate.java | 7 +++--- .../templates/ForeachPostfixTemplate.java | 19 ++++++++++++--- .../templates/NullCheckPostfixTemplate.java | 19 ++++++++++++--- .../postfix/templates/PostfixTemplate.java | 6 +++-- .../ReturnStatementPostfixTemplate.java | 17 +++++++++++++- .../SwitchStatementPostfixTemplate.java | 17 +++++++++++++- .../SynchronizedStatementPostfixTemplate.java | 23 ++++++++++++++++--- .../ThrowExceptionPostfixTemplate.java | 21 +++++++++++++---- .../if/boxedBooleanVariable_after.java | 2 +- .../IfStatementPostfixTemplateTest.java | 17 +++++++++++++- 11 files changed, 140 insertions(+), 28 deletions(-) diff --git a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/BooleanPostfixTemplate.java b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/BooleanPostfixTemplate.java index 0f810fd90dc0..ab6f90eaa752 100644 --- a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/BooleanPostfixTemplate.java +++ b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/BooleanPostfixTemplate.java @@ -1,10 +1,24 @@ +/* + * 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 com.intellij.psi.PsiExpressionStatement; import org.jetbrains.annotations.NotNull; abstract public class BooleanPostfixTemplate extends PostfixTemplate { @@ -15,8 +29,6 @@ abstract public class BooleanPostfixTemplate extends PostfixTemplate { @Override public boolean isApplicable(@NotNull PsiElement context, @NotNull Document copyDocument, int newOffset) { PsiExpression topmostExpression = getTopmostExpression(context); - return topmostExpression != null && - topmostExpression.getParent() instanceof PsiExpressionStatement && - PostfixTemplatesUtils.isBoolean(topmostExpression.getType()); + return topmostExpression != null && PostfixTemplatesUtils.isBoolean(topmostExpression.getType()); } } diff --git a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ForIndexedPostfixTemplate.java b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ForIndexedPostfixTemplate.java index 6946ce8b47da..c353c8d39145 100644 --- a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ForIndexedPostfixTemplate.java +++ b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ForIndexedPostfixTemplate.java @@ -26,7 +26,6 @@ import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Pair; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiExpression; -import com.intellij.psi.PsiExpressionStatement; import com.intellij.psi.PsiType; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -39,9 +38,9 @@ public abstract class ForIndexedPostfixTemplate extends PostfixTemplate { @Override public boolean isApplicable(@NotNull PsiElement context, @NotNull Document copyDocument, int newOffset) { PsiExpression expr = getTopmostExpression(context); - if (expr == null || !(expr.getParent() instanceof PsiExpressionStatement)) return false; - return PostfixTemplatesUtils.isNumber(expr.getType()) || PostfixTemplatesUtils.isArray(expr.getType()) || PostfixTemplatesUtils.isIterable( - expr.getType()); + return expr != null && (PostfixTemplatesUtils.isNumber(expr.getType()) || + PostfixTemplatesUtils.isArray(expr.getType()) || + PostfixTemplatesUtils.isIterable(expr.getType())); } @Override diff --git a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ForeachPostfixTemplate.java b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ForeachPostfixTemplate.java index a82f74a9687f..fa5e2afc6d2d 100644 --- a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ForeachPostfixTemplate.java +++ b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ForeachPostfixTemplate.java @@ -1,3 +1,18 @@ +/* + * 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.Template; @@ -13,7 +28,6 @@ import com.intellij.openapi.editor.Editor; import com.intellij.openapi.project.Project; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiExpression; -import com.intellij.psi.PsiExpressionStatement; import org.jetbrains.annotations.NotNull; public class ForeachPostfixTemplate extends PostfixTemplate { @@ -24,8 +38,7 @@ public class ForeachPostfixTemplate extends PostfixTemplate { @Override public boolean isApplicable(@NotNull PsiElement context, @NotNull Document copyDocument, int newOffset) { PsiExpression expr = getTopmostExpression(context); - if (expr == null || !(expr.getParent() instanceof PsiExpressionStatement)) return false; - return PostfixTemplatesUtils.isArray(expr.getType()) || PostfixTemplatesUtils.isIterable(expr.getType()); + return expr != null && PostfixTemplatesUtils.isArray(expr.getType()) || PostfixTemplatesUtils.isIterable(expr.getType()); } @Override diff --git a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/NullCheckPostfixTemplate.java b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/NullCheckPostfixTemplate.java index f35eeb200316..4d4ac0c4786c 100644 --- a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/NullCheckPostfixTemplate.java +++ b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/NullCheckPostfixTemplate.java @@ -1,3 +1,18 @@ +/* + * 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; @@ -21,9 +36,7 @@ public abstract class NullCheckPostfixTemplate extends PostfixTemplate { @Override public boolean isApplicable(@NotNull PsiElement context, @NotNull Document copyDocument, int newOffset) { - PsiExpression expr = getTopmostExpression(context); - PsiElement parent = expr != null ? expr.getParent() : null; - return parent instanceof PsiExpressionStatement; + return getTopmostExpression(context) != null; } @Override diff --git a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/PostfixTemplate.java b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/PostfixTemplate.java index 8ee2cc08760d..80a68ab26650 100644 --- a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/PostfixTemplate.java +++ b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/PostfixTemplate.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * 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. @@ -21,6 +21,7 @@ import com.intellij.openapi.editor.Editor; import com.intellij.openapi.extensions.ExtensionPointName; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiExpression; +import com.intellij.psi.PsiExpressionStatement; import com.intellij.psi.util.PsiTreeUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -72,7 +73,8 @@ public abstract class PostfixTemplate { @Nullable public static PsiExpression getTopmostExpression(PsiElement context) { - return PsiTreeUtil.getTopmostParentOfType(context, PsiExpression.class); + PsiExpressionStatement statement = PsiTreeUtil.getNonStrictParentOfType(context, PsiExpressionStatement.class); + return statement != null ? PsiTreeUtil.getChildOfType(statement, PsiExpression.class) : null; } public abstract boolean isApplicable(@NotNull PsiElement context, @NotNull Document copyDocument, int newOffset); 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 70e413f8ac5b..e15f6c4940c3 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 @@ -1,3 +1,18 @@ +/* + * 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.openapi.editor.Document; @@ -13,7 +28,7 @@ public class ReturnStatementPostfixTemplate extends PostfixTemplate { @Override public boolean isApplicable(@NotNull PsiElement context, @NotNull Document copyDocument, int newOffset) { PsiExpression expr = getTopmostExpression(context); - if (expr == null || !(expr.getParent() instanceof PsiExpressionStatement)) return false; + if (expr == null) return false; PsiType type = expr.getType(); return type != null && !PsiType.VOID.equals(type); } diff --git a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/SwitchStatementPostfixTemplate.java b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/SwitchStatementPostfixTemplate.java index d878f004a3be..880c0455775b 100644 --- a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/SwitchStatementPostfixTemplate.java +++ b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/SwitchStatementPostfixTemplate.java @@ -1,3 +1,18 @@ +/* + * 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.openapi.application.ApplicationManager; @@ -37,7 +52,7 @@ public class SwitchStatementPostfixTemplate extends StatementPostfixTemplateBase @Override public boolean isApplicable(@NotNull PsiElement context, @NotNull Document copyDocument, int newOffset) { PsiExpression expr = getTopmostExpression(context); - return expr != null && expr.getParent() instanceof PsiExpressionStatement && isSwitchCompatibleType(expr.getType(), context); + return expr != null && isSwitchCompatibleType(expr.getType(), context); } @Override diff --git a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/SynchronizedStatementPostfixTemplate.java b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/SynchronizedStatementPostfixTemplate.java index 801274ebbfce..044dadc81c8c 100644 --- a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/SynchronizedStatementPostfixTemplate.java +++ b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/SynchronizedStatementPostfixTemplate.java @@ -1,8 +1,26 @@ +/* + * 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.openapi.editor.Document; import com.intellij.openapi.editor.Editor; -import com.intellij.psi.*; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiExpression; +import com.intellij.psi.PsiPrimitiveType; +import com.intellij.psi.PsiType; import org.jetbrains.annotations.NotNull; public class SynchronizedStatementPostfixTemplate extends StatementPostfixTemplateBase { @@ -13,9 +31,8 @@ public class SynchronizedStatementPostfixTemplate extends StatementPostfixTempla @Override public boolean isApplicable(@NotNull PsiElement context, @NotNull Document copyDocument, int newOffset) { PsiExpression expression = getTopmostExpression(context); - PsiElement parent = expression != null ? expression.getParent() : null; PsiType type = expression != null ? expression.getType() : null; - return parent instanceof PsiExpressionStatement && type != null && !(type instanceof PsiPrimitiveType); + return type != null && !(type instanceof PsiPrimitiveType); } @Override diff --git a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ThrowExceptionPostfixTemplate.java b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ThrowExceptionPostfixTemplate.java index 5af260b46be1..7c2017ffe290 100644 --- a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ThrowExceptionPostfixTemplate.java +++ b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ThrowExceptionPostfixTemplate.java @@ -1,11 +1,24 @@ +/* + * 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.PsiElement; -import com.intellij.psi.PsiExpression; -import com.intellij.psi.PsiExpressionStatement; import org.jetbrains.annotations.NotNull; public class ThrowExceptionPostfixTemplate extends PostfixTemplate { @@ -15,9 +28,7 @@ public class ThrowExceptionPostfixTemplate extends PostfixTemplate { @Override public boolean isApplicable(@NotNull PsiElement context, @NotNull Document copyDocument, int newOffset) { - PsiExpression expr = getTopmostExpression(context); - PsiElement parent = expr != null ? expr.getParent() : null; - return parent instanceof PsiExpressionStatement; + return getTopmostExpression(context) != null; } @Override diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/if/boxedBooleanVariable_after.java b/java/java-tests/testData/codeInsight/template/postfix/templates/if/boxedBooleanVariable_after.java index d78e60160a52..9a7729930589 100644 --- a/java/java-tests/testData/codeInsight/template/postfix/templates/if/boxedBooleanVariable_after.java +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/if/boxedBooleanVariable_after.java @@ -5,4 +5,4 @@ public class Foo { } return; } -} \ No newline at end of file +} diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/template/postfix/templates/IfStatementPostfixTemplateTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/template/postfix/templates/IfStatementPostfixTemplateTest.java index f0c8ff645862..41f4fa0d70df 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/template/postfix/templates/IfStatementPostfixTemplateTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/template/postfix/templates/IfStatementPostfixTemplateTest.java @@ -1,10 +1,25 @@ +/* + * 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 IfStatementPostfixTemplateTest extends PostfixTemplateTestCase { public void testBooleanVariableBeforeAssignment() { doTest(); } - public void _testBoxedBooleanVariable() { doTest(); } //todo: platform changes if required + public void testBoxedBooleanVariable() { doTest(); } public void testNotBooleanExpression() { doTest(); } public void testUnresolvedVariable() { doTest(); } public void testSeveralConditions() { doTest(); }