[codeInsight] IDEA-113640 Provide intention to combine System.out.println(String.format(...)) into System.out.printf

This patch fixes the notes from the code review, it inclues:

- renaming method to methodNameReference for variables that contain the name of a method that is being called, i.e. either `println` or `print` or `format`
- using a different overloaded createProblemDescriptor method that does not accept a `TextRange`
- using `getParent` in quick fixes to get the `PsiMethodExpressionCall`
- proper handling arguments with parenthesis

Signed-off-by: Nikita Eshkeev <nikita.eshkeev@jetbrains.com>

GitOrigin-RevId: ea1e335372bf7b1ce3e6c87a3816baa0ab54e11e
This commit is contained in:
Nikita Eshkeev
2020-05-06 12:52:27 +03:00
committed by intellij-monorepo-bot
parent 7f537d3466
commit 299e3d31e1
7 changed files with 190 additions and 52 deletions

View File

@@ -8,13 +8,19 @@ class Main {
static {
String s1 = "test";
String s1n = format("test%n");
String s1n1 = format(("test%n"));
String s2 = "test";
String s21 = "test";
String s2n = format(Locale.US, "test%n");
String s2n1 = format(Locale.US, ((("test%n"))));
String s3 = "test";
String s31 = "test";
String s3l = "test";
String s3n = String.format(Locale.US, "test%n");
String s3l1 = "test";
String s3n1 = String.format(Locale.US, ("test%n"));
System.out.println(/* one */ /* two */ /* three */ "hello, " /* four */);
System.out.println(/* one */ /* two */ /* three */ "hello" /* four */);
System.out.println(/* one */ /* two */ /* three */ ("hello") /* four */);
}
Main() {

View File

@@ -4,7 +4,9 @@ import java.io.PrintStream;
class Main {
static {
System.out.print(/* begin */ "Hello, World!"/* end */);
System.out.print((/* begin */ "Hello, World!"/* end */));
System.out.print(String.format(/* begin */ "Hello, World!%n"/* end */));
System.out.print(String.format((/* begin */ "Hello, World!%n"/* end */)));
}
Main() {

View File

@@ -0,0 +1,45 @@
// "Fix all 'Redundant call to 'String.format()'' problems in file" "true"
import java.io.PrintStream;
import java.util.Locale;
import static java.lang.String.format;
class Main {
static {
System.out.print(/* one */ "hello" /* two */);
System.out.print(/* one */ ("hello") /* two */);
System.out.print(/* one */ ("hello") /* two */);
System.out.print(/* one */ ("hello") /* two */);
System.out.print(/* one */ /* two */ /* three */ ("hello") /* four */);
System.out.print(/* one */ /* two */ /* three */ ("hello") /* four */);
System.out.print(/* one */ /* two */ /* three */ ("hello") /* four */);
System.out.print((format(/* one */ Locale.CANADA /* two */, /* three */ "hello%n" /* four */)));
}
Main() {
System.out.print(/* one */ ("hello") /* two */);
System.out.print(/* one */ ("hello") /* two */);
System.out.print(/* one */ /* two */ /* three */ ("hello") /* four */);
System.out.print(/* one */ /* two */ /* three */ ("hello") /* four */);
}
void f() {
System.out.print(/* one */ ("hello") /* two */);
System.out.print(/* one */ ("hello") /* two */);
System.out.print(/* one */ /* two */ /* three */ ("hello") /* four */);
System.out.print(/* one */ /* two */ /* three */ ("hello") /* four */);
}
void out(PrintStream printer) {
printer.print(/* one */ ("hello") /* two */);
printer.print(/* one */ ("hello") /* two */);
printer.print(/* one */ /* two */ /* three */ ("hello") /* four */);
printer.print(/* one */ /* two */ /* three */ ("hello") /* four */);
}
void caller() {
printf(String.format(/* one */ ("hello") /* two */));
printf(((String.format(/* one */ ("hello") /* two */))));
printf(String.format(/* one */ Locale.CANADA /* two */, /* three */ ("hello") /* four */));
printf((String.format(/* one */ Locale.CANADA /* two */, /* three */ ("hello") /* four */)));
}
static void printf(String value) {}
}

View File

@@ -8,13 +8,19 @@ class Main {
static {
String s1 = f<caret>ormat("test");
String s1n = format("test%n");
String s1n1 = format(("test%n"));
String s2 = format(Locale.US, "test");
String s21 = format(Locale.US, ("test"));
String s2n = format(Locale.US, "test%n");
String s2n1 = format(Locale.US, ((("test%n"))));
String s3 = String.format("test");
String s31 = String.format((("test")));
String s3l = String.format(Locale.US, "test");
String s3n = String.format(Locale.US, "test%n");
String s3l1 = String.format(Locale.US, ("test"));
String s3n1 = String.format(Locale.US, ("test%n"));
System.out.println(String.format(/* one */ Locale.CANADA /* two */, /* three */ "hello, " /* four */));
System.out.println(String.format(/* one */ Locale.CANADA /* two */, /* three */ "hello" /* four */));
System.out.println(String.format(/* one */ Locale.CANADA /* two */, /* three */ ("hello") /* four */));
}
Main() {

View File

@@ -4,7 +4,9 @@ import java.io.PrintStream;
class Main {
static {
System.out.print(String.<caret>format(/* begin */ "Hello, World!"/* end */));
System.out.print(String.format((/* begin */ "Hello, World!"/* end */)));
System.out.print(String.format(/* begin */ "Hello, World!%n"/* end */));
System.out.print(String.format((/* begin */ "Hello, World!%n"/* end */)));
}
Main() {

View File

@@ -0,0 +1,45 @@
// "Fix all 'Redundant call to 'String.format()'' problems in file" "true"
import java.io.PrintStream;
import java.util.Locale;
import static java.lang.String.format;
class Main {
static {
System.out.<caret>printf(String.format(/* one */ "hello" /* two */));
System.out.printf(String.format(/* one */ ("hello") /* two */));
System.out.printf(format(/* one */ ("hello") /* two */);
System.out.printf(((String.format(/* one */ ("hello") /* two */))));
System.out.printf(String.format(/* one */ Locale.CANADA /* two */, /* three */ ("hello") /* four */));
System.out.printf((String.format(/* one */ Locale.CANADA /* two */, /* three */ ("hello") /* four */)));
System.out.printf((format(/* one */ Locale.CANADA /* two */, /* three */ ("hello") /* four */)));
System.out.printf((format(/* one */ Locale.CANADA /* two */, /* three */ "hello%n" /* four */)));
}
Main() {
System.out.printf(String.format(/* one */ ("hello") /* two */));
System.out.printf(((String.format(/* one */ ("hello") /* two */))));
System.out.printf(String.format(/* one */ Locale.CANADA /* two */, /* three */ ("hello") /* four */));
System.out.printf((String.format(/* one */ Locale.CANADA /* two */, /* three */ ("hello") /* four */)));
}
void f() {
System.out.printf(String.format(/* one */ ("hello") /* two */));
System.out.printf(((String.format(/* one */ ("hello") /* two */))));
System.out.printf(String.format(/* one */ Locale.CANADA /* two */, /* three */ ("hello") /* four */));
System.out.printf((String.format(/* one */ Locale.CANADA /* two */, /* three */ ("hello") /* four */)));
}
void out(PrintStream printer) {
printer.printf(String.format(/* one */ ("hello") /* two */));
printer.printf(((String.format(/* one */ ("hello") /* two */))));
printer.printf(String.format(/* one */ Locale.CANADA /* two */, /* three */ ("hello") /* four */));
printer.printf((String.format(/* one */ Locale.CANADA /* two */, /* three */ ("hello") /* four */)));
}
void caller() {
printf(String.format(/* one */ ("hello") /* two */));
printf(((String.format(/* one */ ("hello") /* two */))));
printf(String.format(/* one */ Locale.CANADA /* two */, /* three */ ("hello") /* four */));
printf((String.format(/* one */ Locale.CANADA /* two */, /* three */ ("hello") /* four */)));
}
static void printf(String value) {}
}