don't offer to replace with MessageFormat.format() when concatenation is constant (IDEA-18184 & IDEA-165981)

This commit is contained in:
Bas Leijdekkers
2017-01-10 13:18:54 +01:00
parent 03ba17b426
commit 4a89674b9f
5 changed files with 72 additions and 4 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
* Copyright 2000-2017 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.
@@ -80,7 +80,7 @@ public class ConcatenationToMessageFormatAction implements IntentionAction {
argumentList.add(arrayArg);
}
call = (PsiMethodCallExpression) JavaCodeStyleManager.getInstance(project).shortenClassReferences(call);
call = (PsiMethodCallExpression) CodeStyleManager.getInstance(element.getManager().getProject()).reformat(call);
call = (PsiMethodCallExpression) CodeStyleManager.getInstance(project).reformat(call);
concatenation.replace(call);
}
@@ -88,8 +88,8 @@ public class ConcatenationToMessageFormatAction implements IntentionAction {
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
if (PsiUtil.getLanguageLevel(file).compareTo(LanguageLevel.JDK_1_4) < 0) return false;
final PsiElement element = findElementAtCaret(editor, file);
PsiPolyadicExpression binaryExpression = getEnclosingLiteralConcatenation(element);
return binaryExpression != null && !AnnotationUtil.isInsideAnnotation(binaryExpression);
final PsiPolyadicExpression concatenation = getEnclosingLiteralConcatenation(element);
return concatenation != null && !AnnotationUtil.isInsideAnnotation(concatenation) && !PsiUtil.isConstantExpression(concatenation);
}
@Nullable

View File

@@ -0,0 +1,6 @@
class Constant {
void f() {
String s = "a" + <caret>"b";
}
}

View File

@@ -0,0 +1,6 @@
class Simple {
void f(int i) {
String s = java.text.MessageFormat.format("a{0}b", i);
}
}

View File

@@ -0,0 +1,6 @@
class Simple {
void f(int i) {
String s = "a" + i + <caret>"b";
}
}

View File

@@ -0,0 +1,50 @@
/*
* Copyright 2000-2017 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.intention;
import com.intellij.JavaTestUtil;
import com.intellij.testFramework.fixtures.CodeInsightTestUtil;
import com.intellij.testFramework.fixtures.JavaCodeInsightFixtureTestCase;
/**
* @author Bas Leijdekkers
*/
public class ConcatenationToMessageFormatTest extends JavaCodeInsightFixtureTestCase {
@Override
protected String getTestDataPath() {
return JavaTestUtil.getJavaTestDataPath() + "/codeInsight/concatenationToMessageFormat/";
}
public void testConstant() {
assertTestNotAvailable();
}
public void testSimple() {
doTest();
}
private void doTest() {
final String name = getTestName(true);
CodeInsightTestUtil
.doIntentionTest(myFixture, "Replace '+' with 'java.text.MessageFormat.format()'", name + ".java", name + ".after.java");
}
private void assertTestNotAvailable() {
myFixture.configureByFile(getTestName(true) + ".java");
assertEmpty(myFixture.filterAvailableIntentions("Replace '+' with 'java.text.MessageFormat.format()'"));
}
}