i18n fix: traverse to the expression top level (IDEA-248707)
authorAnna Kozlova <anna.kozlova@jetbrains.com>
Fri, 21 Aug 2020 07:59:54 +0000 (09:59 +0200)
committerintellij-monorepo-bot <intellij-monorepo-bot-no-reply@jetbrains.com>
Fri, 21 Aug 2020 08:03:52 +0000 (08:03 +0000)
if one need to extract subpart of concatenation, it's required to extract a variable; similar to parts of polyadic expression

GitOrigin-RevId: 46cd1a75303b72ea64e9694617fb43f4d3da5982

java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/i18nize/afterNestedConcatenation.java [new file with mode: 0644]
java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/i18nize/beforeNestedConcatenation.java [new file with mode: 0644]
java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/quickFix/I18nizeTest.java
plugins/java-i18n/src/com/intellij/codeInspection/i18n/I18nizeConcatenationQuickFix.java

diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/i18nize/afterNestedConcatenation.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/i18nize/afterNestedConcatenation.java
new file mode 100644 (file)
index 0000000..c5f4b44
--- /dev/null
@@ -0,0 +1,5 @@
+class a {
+  void foo(String s, int i, boolean b) {
+    String desc = i18nizedExpr;
+  }
+}
\ No newline at end of file
diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/i18nize/beforeNestedConcatenation.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/i18nize/beforeNestedConcatenation.java
new file mode 100644 (file)
index 0000000..08fe755
--- /dev/null
@@ -0,0 +1,5 @@
+class a {
+  void foo(String s, int i, boolean b) {
+    String desc = "Our " + s + " has " + (b ? i + " doo<caret>rs" : "many windows");
+  }
+}
\ No newline at end of file
index b6a133bd3317b48f7481293a74e98538c38668b0..dfea299a2340c9d600ce12acc07b3e68cb9c13c8 100644 (file)
@@ -93,6 +93,7 @@ public class I18nizeTest extends LightJavaCodeInsightTestCase {
 
   public void testLongConcat() {doTest();}
   public void testCharacterLiteral() {doTest();}
+  public void testNestedConcatenation() {doTest();}
 
   public void testShortenClassReferences() {
     doTest("p.MyBundle.message(\"key\")");
index 5df3bfd3109b3a7e59627e94512d43457b9b14bc..627869b1bb8f6fe135a47728a7e0a82aaa302189 100644 (file)
@@ -13,9 +13,7 @@ import com.intellij.util.IncorrectOperationException;
 import org.jetbrains.annotations.NonNls;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
-import org.jetbrains.uast.UExpression;
-import org.jetbrains.uast.UPolyadicExpression;
-import org.jetbrains.uast.UastContextKt;
+import org.jetbrains.uast.*;
 import org.jetbrains.uast.expressions.UInjectionHost;
 import org.jetbrains.uast.expressions.UStringConcatenationsFacade;
 import org.jetbrains.uast.generate.UastCodeGenerationPlugin;
@@ -105,10 +103,19 @@ public class I18nizeConcatenationQuickFix extends I18nizeQuickFix {
 
   @Nullable
   public static UPolyadicExpression getEnclosingLiteralConcatenation(final PsiElement psiElement) {
-    UPolyadicExpression uPolyadicExpression = UastContextKt.getUastParentOfType(psiElement, UPolyadicExpression.class);
-    UStringConcatenationsFacade concatenation = UStringConcatenationsFacade.createFromTopConcatenation(
-      uPolyadicExpression
-    );
+    UExpression topExpression = UastContextKt.getUastParentOfType(psiElement, UPolyadicExpression.class);
+    while (topExpression != null) {
+      UElement parent = topExpression.getUastParent();
+      if (parent instanceof UParenthesizedExpression || 
+          parent instanceof UIfExpression || 
+          parent instanceof UPolyadicExpression) {
+        topExpression = (UExpression)parent;
+      }
+      else {
+        break;
+      }
+    }
+    UStringConcatenationsFacade concatenation = UStringConcatenationsFacade.createFromTopConcatenation(topExpression);
     if (concatenation != null) {
       PartiallyKnownString pks = concatenation.asPartiallyKnownString();
       if (pks.getSegments().size() == 1) {