Java: don't add unnecessary parentheses when inlining string concatenation into a string concatenation (IDEA-314882)

GitOrigin-RevId: d13d5bf7670009a4d26f09b4838828b5adeb4bde
This commit is contained in:
Bas Leijdekkers
2023-03-08 00:32:43 +00:00
committed by intellij-monorepo-bot
parent 93a9db98dc
commit 6a09b77b61
5 changed files with 27 additions and 9 deletions
@@ -64,12 +64,13 @@ public final class InlineUtil implements CommonJavaInlineUtil {
PsiClass refParent = RefactoringChangeUtil.getThisClass(ref);
final PsiType varType = variable.getType();
initializer = CommonJavaRefactoringUtil.convertInitializerToNormalExpression(initializer, varType);
if (initializer instanceof PsiPolyadicExpression) {
final IElementType operationTokenType = ((PsiPolyadicExpression)initializer).getOperationTokenType();
if (initializer instanceof PsiPolyadicExpression concatenation && parent instanceof PsiPolyadicExpression parentConcatenation) {
final IElementType operationTokenType = concatenation.getOperationTokenType();
if ((operationTokenType == JavaTokenType.PLUS || operationTokenType == JavaTokenType.MINUS) &&
parent instanceof PsiPolyadicExpression && ((PsiPolyadicExpression)parent).getOperationTokenType() == JavaTokenType.PLUS) {
final PsiType type = ((PsiPolyadicExpression)parent).getType();
if (type != null && type.equalsToText(CommonClassNames.JAVA_LANG_STRING)) {
parentConcatenation.getOperationTokenType() == JavaTokenType.PLUS) {
final PsiType type = parentConcatenation.getType();
if (type != null && type.equalsToText(CommonClassNames.JAVA_LANG_STRING) &&
!varType.equalsToText(CommonClassNames.JAVA_LANG_STRING)) {
final PsiElementFactory factory = JavaPsiFacade.getElementFactory(initializer.getProject());
initializer = factory.createExpressionFromText("(" + initializer.getText() + ")", initializer);
}
@@ -5,6 +5,6 @@ import java.util.function.Function;
public class Test {
public static void main(String[] args) {
/* bar */
String s = ("a" +/* who-hoo */ "x") + "foo";
String s = "a" +/* who-hoo */ "x" + "foo";
}
}
@@ -0,0 +1,9 @@
class X {
String string() {
return "heavy" + "⚡️" + "metal";
}
String x() {
return "🤘" + string<caret>() + "🤘";
}
}
@@ -0,0 +1,6 @@
class X {
String x() {
return "🤘" + "heavy" + "⚡️" + "metal" + "🤘";
}
}
@@ -1,4 +1,4 @@
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.java.refactoring.inline;
import com.intellij.JavaTestUtil;
@@ -16,8 +16,6 @@ import com.intellij.testFramework.IdeaTestUtil;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import java.util.List;
public class InlineMethodTest extends LightRefactoringTestCase {
@NotNull
@Override
@@ -347,6 +345,10 @@ public class InlineMethodTest extends LightRefactoringTestCase {
public void testStringPlusOverload() {
doTest();
}
public void testConcatenationInConcatenation() {
doTest();
}
public void testReturnStatementWithoutBraces() {
doTestInlineThisOnly();