From 05ccb7216627a3412e058d3435060306ff3438ff Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Fri, 21 Aug 2020 09:09:28 +0200 Subject: [PATCH] i18n fix: fix choice format parameters ordering (IDEA-248709) GitOrigin-RevId: 84fa26fcbd459bcf263df0adf224afebb6bdeb31 --- ...beforeGeneratedChoicePatternWithConcatenation.java | 5 +++++ .../java/codeInsight/daemon/quickFix/I18nizeTest.java | 11 +++++++++++ .../intellij/codeInspection/i18n/JavaI18nUtil.java | 8 ++------ 3 files changed, 18 insertions(+), 6 deletions(-) create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/i18nize/beforeGeneratedChoicePatternWithConcatenation.java diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/i18nize/beforeGeneratedChoicePatternWithConcatenation.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/i18nize/beforeGeneratedChoicePatternWithConcatenation.java new file mode 100644 index 000000000000..8fa53c1e4650 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/i18nize/beforeGeneratedChoicePatternWithConcatenation.java @@ -0,0 +1,5 @@ +class MyTest { + void f(String lang, boolean prefix, String article){ + String s = "Not a valid " + lang + " identifier part in " + (prefix ? article + " prefix" : "suffix"); + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/quickFix/I18nizeTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/quickFix/I18nizeTest.java index 165bb5eea397..b6a133bd3317 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/quickFix/I18nizeTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/quickFix/I18nizeTest.java @@ -108,4 +108,15 @@ public class I18nizeTest extends LightJavaCodeInsightTestCase { assertSize(1, args); assertEquals("prefix ? 0 : 1", args.get(0).getSourcePsi().getText()); } + + public void testGeneratedChoicePatternWithConcatenation() { + configureByFile(getBasePath() + "/before" + getTestName(false) + "." + "java"); + UInjectionHost enclosingStringLiteral = I18nizeAction.getEnclosingStringLiteral(getFile(), getEditor()); + UStringConcatenationsFacade concatenation = UStringConcatenationsFacade.createFromTopConcatenation(enclosingStringLiteral); + assertNotNull(concatenation); + ArrayList args = new ArrayList<>(); + Assert.assertEquals("Not a valid {0} identifier part in {2, choice, 0#{1} prefix|1#suffix}", JavaI18nUtil.buildUnescapedFormatString(concatenation, args, getProject())); + assertSize(3, args); + assertEquals("prefix ? 0 : 1", args.get(2).getSourcePsi().getText()); + } } diff --git a/plugins/java-i18n/src/com/intellij/codeInspection/i18n/JavaI18nUtil.java b/plugins/java-i18n/src/com/intellij/codeInspection/i18n/JavaI18nUtil.java index b81847f66224..96082c015d96 100644 --- a/plugins/java-i18n/src/com/intellij/codeInspection/i18n/JavaI18nUtil.java +++ b/plugins/java-i18n/src/com/intellij/codeInspection/i18n/JavaI18nUtil.java @@ -452,7 +452,6 @@ public final class JavaI18nUtil extends I18nUtil { @NotNull Project project, boolean nested) { StringBuilder result = new StringBuilder(); - int elIndex = 0; boolean noEscapingRequired = !nested && SequencesKt.all(cf.getUastOperands(), expression -> expression instanceof ULiteralExpression); for (UExpression expression : SequencesKt.asIterable(cf.getUastOperands())) { while (expression instanceof UParenthesizedExpression) { @@ -470,11 +469,8 @@ public final class JavaI18nUtil extends I18nUtil { } } } - else if (addChoicePattern(expression, formatParameters, project, result)) { - elIndex = formatParameters.size(); - } - else { - result.append("{").append(elIndex++).append("}"); + else if (!addChoicePattern(expression, formatParameters, project, result)) { + result.append("{").append(formatParameters.size()).append("}"); formatParameters.add(expression); } }