Postfix completion: do not apply null, notnull and instanceof template on primitive types

This commit is contained in:
Alexander Zolotov
2014-02-20 16:07:51 +04:00
parent 4ddf754b9d
commit 79e95dc617
12 changed files with 104 additions and 6 deletions
@@ -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.guess.GuessManager;
@@ -28,17 +43,17 @@ public class InstanceofExpressionPostfixTemplate extends PostfixTemplate {
@Override
public boolean isApplicable(@NotNull PsiElement context, @NotNull Document copyDocument, int newOffset) {
return getTopmostExpression(context) != null;
return PostfixTemplatesUtils.isNotPrimitiveTypeExpression(getTopmostExpression(context));
}
@Override
public void expand(@NotNull PsiElement context, @NotNull Editor editor) {
PsiExpression expression = getTopmostExpression(context);
if (expression == null) return;
if (!PostfixTemplatesUtils.isNotPrimitiveTypeExpression(expression)) return;
surroundExpression(context.getProject(), editor, expression);
}
private static void surroundExpression(Project project, Editor editor, PsiExpression expr) throws IncorrectOperationException {
private static void surroundExpression(@NotNull Project project, @NotNull Editor editor, @NotNull PsiExpression expr) throws IncorrectOperationException {
assert expr.isValid();
PsiType[] types = GuessManager.getInstance(project).guessTypeToCast(expr);
final boolean parenthesesNeeded = expr instanceof PsiPolyadicExpression ||
@@ -39,13 +39,13 @@ public abstract class NullCheckPostfixTemplate extends PostfixTemplate {
@Override
public boolean isApplicable(@NotNull PsiElement context, @NotNull Document copyDocument, int newOffset) {
return getTopmostExpression(context) != null;
return PostfixTemplatesUtils.isNotPrimitiveTypeExpression(getTopmostExpression(context));
}
@Override
public void expand(@NotNull PsiElement context, @NotNull Editor editor) {
PsiExpression expr = getTopmostExpression(context);
if (expr == null) return;
if (!PostfixTemplatesUtils.isNotPrimitiveTypeExpression(expr)) return;
Project project = expr.getProject();
PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
@@ -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.
@@ -46,6 +46,11 @@ public abstract class PostfixTemplatesUtils {
editor.getCaretModel().moveToOffset(replace.getTextRange().getEndOffset());
}
@Contract("null -> false")
public static boolean isNotPrimitiveTypeExpression(@Nullable PsiExpression expression) {
return expression != null && !(expression.getType() instanceof PsiPrimitiveType);
}
@Contract("null -> false")
public static boolean isIterable(@Nullable PsiType type) {
return type != null && InheritanceUtil.isInheritor(type, CommonClassNames.JAVA_LANG_ITERABLE);
@@ -0,0 +1,5 @@
public class Foo {
void m(int o) {
o.instanceof<caret>
}
}
@@ -0,0 +1,5 @@
public class Foo {
void m(int o) {
o.instanceof <caret>
}
}
@@ -0,0 +1,5 @@
public class Foo {
void m(int o) {
o.notnull<caret>
}
}
@@ -0,0 +1,5 @@
public class Foo {
void m(int o) {
o.notnull <caret>
}
}
@@ -0,0 +1,5 @@
public class Foo {
void m(int o) {
o.null<caret>
}
}
@@ -0,0 +1,5 @@
public class Foo {
void m(int o) {
o.null <caret>
}
}
@@ -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.impl.TemplateManagerImpl;
@@ -7,6 +22,7 @@ import org.jetbrains.annotations.NotNull;
public class InstanceofPostfixTemplateTest extends PostfixTemplateTestCase {
public void testSingleExpression() { doTest(); }
public void testAlias() { doTest(); }
public void testPrimitive() { doTest(); }
public void testSingleExpressionTemplate() {
TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable());
@@ -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 org.jetbrains.annotations.NotNull;
@@ -11,6 +26,7 @@ public class NotNullPostfixTemplateTest extends PostfixTemplateTestCase {
protected String getSuffix() { return "notnull"; }
public void testSimple() { doTest(); }
public void testPrimitive() { doTest(); }
public void testNn() { doTest(); }
public void testSecondStatement() { doTest(); }
}
@@ -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 org.jetbrains.annotations.NotNull;
@@ -11,5 +26,6 @@ public class NullPostfixTemplateTest extends PostfixTemplateTestCase {
protected String getSuffix() { return "null"; }
public void testSimple() { doTest(); }
public void testPrimitive() { doTest(); }
public void testSecondStatement() { doTest(); }
}