mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-19 13:02:30 +07:00
preserve comments: concatenation to message format
compose tests into one
This commit is contained in:
@@ -28,6 +28,7 @@ import com.intellij.psi.util.PsiConcatenationUtil;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.siyeh.ig.psiutils.CommentTracker;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -81,7 +82,7 @@ public class ConcatenationToMessageFormatAction implements IntentionAction {
|
||||
}
|
||||
call = (PsiMethodCallExpression) JavaCodeStyleManager.getInstance(project).shortenClassReferences(call);
|
||||
call = (PsiMethodCallExpression) CodeStyleManager.getInstance(project).reformat(call);
|
||||
concatenation.replace(call);
|
||||
new CommentTracker().replaceAndRestoreComments(concatenation, call);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
class Simple {
|
||||
|
||||
void f(String a, String b) {
|
||||
//a before arg
|
||||
//comment
|
||||
//after literal
|
||||
/*before arg*/
|
||||
String s = java.text.MessageFormat.format("a:{0}b:{1}", a, b); //end comment
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
class Simple {
|
||||
|
||||
void f(String a, String b) {
|
||||
String s = "a:" +//a before arg
|
||||
a <caret>+ //comment
|
||||
"b:"//after literal
|
||||
+ /*before arg*/b; //end comment
|
||||
|
||||
}
|
||||
}
|
||||
@@ -16,12 +16,13 @@
|
||||
package com.intellij.java.codeInsight.intention;
|
||||
|
||||
import com.intellij.JavaTestUtil;
|
||||
import com.intellij.psi.PsiExpression;
|
||||
import com.intellij.psi.util.PsiConcatenationUtil;
|
||||
import com.intellij.testFramework.fixtures.CodeInsightTestUtil;
|
||||
import com.intellij.testFramework.fixtures.JavaCodeInsightFixtureTestCase;
|
||||
|
||||
/**
|
||||
* @author Bas Leijdekkers
|
||||
*/
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class ConcatenationToMessageFormatTest extends JavaCodeInsightFixtureTestCase {
|
||||
|
||||
@Override
|
||||
@@ -37,6 +38,8 @@ public class ConcatenationToMessageFormatTest extends JavaCodeInsightFixtureTest
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testComments() { doTest(); }
|
||||
|
||||
private void doTest() {
|
||||
final String name = getTestName(true);
|
||||
CodeInsightTestUtil
|
||||
@@ -47,4 +50,33 @@ public class ConcatenationToMessageFormatTest extends JavaCodeInsightFixtureTest
|
||||
myFixture.configureByFile(getTestName(true) + ".java");
|
||||
assertEmpty(myFixture.filterAvailableIntentions("Replace '+' with 'java.text.MessageFormat.format()'"));
|
||||
}
|
||||
|
||||
private void doTest(String expressionText, String messageFormatText, String... foundExpressionTexts) {
|
||||
final PsiExpression expression = getElementFactory().createExpressionFromText(expressionText, null);
|
||||
final StringBuilder result = new StringBuilder();
|
||||
final ArrayList<PsiExpression> args = new ArrayList<>();
|
||||
PsiConcatenationUtil.buildFormatString(expression, result, args, false);
|
||||
assertEquals(messageFormatText, result.toString());
|
||||
assertEquals(foundExpressionTexts.length, args.size());
|
||||
for (int i = 0; i < foundExpressionTexts.length; i++) {
|
||||
final String foundExpressionText = foundExpressionTexts[i];
|
||||
assertEquals(foundExpressionText, args.get(i).getText());
|
||||
}
|
||||
}
|
||||
|
||||
public void test1() {
|
||||
doTest("\"aaa 'bbb' '\" + ((java.lang.String)ccc) + \"'\"", "aaa ''bbb'' ''{0}''", "ccc");
|
||||
}
|
||||
|
||||
public void test2() {
|
||||
doTest("1 + 2 + 3 + \"{}'\" + '\\n' + ((java.lang.String)ccc)", "{0}'{}'''\\n{1}", "1 + 2 + 3", "ccc");
|
||||
}
|
||||
|
||||
public void test3() {
|
||||
doTest("\"Test{A = \" + 1 + \", B = \" + 2 + \", C = \" + 3 + \"}\"", "Test'{'A = {0}, B = {1}, C = {2}'}'", "1", "2", "3");
|
||||
}
|
||||
|
||||
public void testNullCast() {
|
||||
doTest("\"abc\" + (String)()", "abc{0}", "(String)()");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,55 +0,0 @@
|
||||
/*
|
||||
* 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.java.codeInspection;
|
||||
|
||||
import com.intellij.psi.JavaPsiFacade;
|
||||
import com.intellij.psi.PsiExpression;
|
||||
import com.intellij.psi.util.PsiConcatenationUtil;
|
||||
import com.intellij.testFramework.LightIdeaTestCase;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class ConcatenationToMessageFormatActionTest extends LightIdeaTestCase {
|
||||
|
||||
public void doTest(String expressionText, String messageFormatText, String... foundExpressionTexts) {
|
||||
final PsiExpression expression = JavaPsiFacade.getElementFactory(getProject()).createExpressionFromText(expressionText, null);
|
||||
final StringBuilder result = new StringBuilder();
|
||||
final ArrayList<PsiExpression> args = new ArrayList<>();
|
||||
PsiConcatenationUtil.buildFormatString(expression, result, args, false);
|
||||
assertEquals(messageFormatText, result.toString());
|
||||
assertEquals(foundExpressionTexts.length, args.size());
|
||||
for (int i = 0; i < foundExpressionTexts.length; i++) {
|
||||
final String foundExpressionText = foundExpressionTexts[i];
|
||||
assertEquals(foundExpressionText, args.get(i).getText());
|
||||
}
|
||||
}
|
||||
|
||||
public void test1() {
|
||||
doTest("\"aaa 'bbb' '\" + ((java.lang.String)ccc) + \"'\"", "aaa ''bbb'' ''{0}''", "ccc");
|
||||
}
|
||||
|
||||
public void test2() {
|
||||
doTest("1 + 2 + 3 + \"{}'\" + '\\n' + ((java.lang.String)ccc)", "{0}'{}'''\\n{1}", "1 + 2 + 3", "ccc");
|
||||
}
|
||||
|
||||
public void test3() {
|
||||
doTest("\"Test{A = \" + 1 + \", B = \" + 2 + \", C = \" + 3 + \"}\"", "Test'{'A = {0}, B = {1}, C = {2}'}'", "1", "2", "3");
|
||||
}
|
||||
|
||||
public void testNullCast() {
|
||||
doTest("\"abc\" + (String)()", "abc{0}", "(String)()");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user