From 8d6f72b2ca32954179ad391de2b6f582a7ed008e Mon Sep 17 00:00:00 2001 From: Alexander Zolotov Date: Thu, 6 Mar 2014 21:54:52 +0400 Subject: [PATCH] IDEA-121756 Don't provide postfix completion options in java string literals --- .../ExpressionPostfixTemplateWithChooser.java | 25 +++++++++++++++++-- .../postfix/templates/cast/insideString.java | 5 ++++ .../templates/cast/insideString_after.java | 5 ++++ .../templates/cast/singleArgument.java | 5 ++++ .../templates/cast/singleArgument_after.java | 5 ++++ .../templates/CastPostfixTemplateTest.java | 20 ++++++++++++--- 6 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 java/java-tests/testData/codeInsight/template/postfix/templates/cast/insideString.java create mode 100644 java/java-tests/testData/codeInsight/template/postfix/templates/cast/insideString_after.java create mode 100644 java/java-tests/testData/codeInsight/template/postfix/templates/cast/singleArgument.java create mode 100644 java/java-tests/testData/codeInsight/template/postfix/templates/cast/singleArgument_after.java diff --git a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ExpressionPostfixTemplateWithChooser.java b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ExpressionPostfixTemplateWithChooser.java index 6f09a79e5b60..7efead36faa9 100644 --- a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ExpressionPostfixTemplateWithChooser.java +++ b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ExpressionPostfixTemplateWithChooser.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; @@ -65,8 +80,14 @@ public abstract class ExpressionPostfixTemplateWithChooser extends PostfixTempla } @NotNull - protected List getExpressions(@NotNull PsiElement context, @NotNull Document document, int offset) { - List expressions = IntroduceVariableBase.collectExpressions(context.getContainingFile(), document, offset, false); + protected List getExpressions(@NotNull PsiElement context, @NotNull Document document, final int offset) { + List expressions = ContainerUtil.filter(IntroduceVariableBase.collectExpressions(context.getContainingFile(), document, offset - 1, false), + new Condition() { + @Override + public boolean value(PsiExpression expression) { + return expression.getTextRange().getEndOffset() == offset; + } + }); return ContainerUtil.filter(expressions.isEmpty() ? maybeTopmostExpression(context) : expressions, getTypeCondition()); } diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/cast/insideString.java b/java/java-tests/testData/codeInsight/template/postfix/templates/cast/insideString.java new file mode 100644 index 000000000000..8e2d4d8e59cb --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/cast/insideString.java @@ -0,0 +1,5 @@ +public class Foo { + void m(Object o) { + Integer string = (Integer.parseInt("test.test.cast")); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/cast/insideString_after.java b/java/java-tests/testData/codeInsight/template/postfix/templates/cast/insideString_after.java new file mode 100644 index 000000000000..f51c17698074 --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/cast/insideString_after.java @@ -0,0 +1,5 @@ +public class Foo { + void m(Object o) { + Integer string = (Integer.parseInt("test.test.cast ")); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/cast/singleArgument.java b/java/java-tests/testData/codeInsight/template/postfix/templates/cast/singleArgument.java new file mode 100644 index 000000000000..29bd8830d679 --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/cast/singleArgument.java @@ -0,0 +1,5 @@ +public class Foo { + void m(Object o) { + Integer string = (Integer.parseInt("test.test".cast)); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/cast/singleArgument_after.java b/java/java-tests/testData/codeInsight/template/postfix/templates/cast/singleArgument_after.java new file mode 100644 index 000000000000..61720cad312a --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/cast/singleArgument_after.java @@ -0,0 +1,5 @@ +public class Foo { + void m(Object o) { + Integer string = (Integer.parseInt((() "test.test"))); + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/template/postfix/templates/CastPostfixTemplateTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/template/postfix/templates/CastPostfixTemplateTest.java index dbd4c1ddd20d..b7d4ef358823 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/template/postfix/templates/CastPostfixTemplateTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/template/postfix/templates/CastPostfixTemplateTest.java @@ -1,10 +1,22 @@ +/* + * 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; -/** - * @author ignatov - */ public class CastPostfixTemplateTest extends PostfixTemplateTestCase { @NotNull @Override @@ -12,4 +24,6 @@ public class CastPostfixTemplateTest extends PostfixTemplateTestCase { public void testSingleExpression() { doTest(); } // jdk mock needed public void testVoidExpression() { doTest(); } + public void testSingleArgument() { doTest(); } + public void testInsideString() { doTest(); } }