mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Java8MapApiInspection: better name generation for lambda argument (IDEA-CR-16359)
This commit is contained in:
+24
-8
@@ -29,7 +29,6 @@ import com.intellij.psi.search.searches.ReferencesSearch;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.siyeh.ig.psiutils.*;
|
||||
import one.util.streamex.IntStreamEx;
|
||||
import one.util.streamex.StreamEx;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.Nls;
|
||||
@@ -383,13 +382,7 @@ public class Java8MapApiInspection extends BaseJavaBatchLocalInspectionTool {
|
||||
refs = StreamEx.of(PsiTreeUtil.collectElementsOfType(value, PsiReferenceExpression.class))
|
||||
.filter(ref -> ref.getQualifierExpression() == null && ref.isReferenceTo(element)).toList();
|
||||
if (!refs.isEmpty()) {
|
||||
String name = ((PsiReferenceExpression)key).getReferenceName();
|
||||
// like "myVariableName" => "mvn"
|
||||
nameCandidate = IntStreamEx.ofChars(name).mapFirst(Character::toUpperCase).filter(Character::isUpperCase).charsToString()
|
||||
.toLowerCase(Locale.ENGLISH);
|
||||
if (nameCandidate.isEmpty()) {
|
||||
nameCandidate = "k";
|
||||
}
|
||||
nameCandidate = getNameCandidate(((PsiReferenceExpression)key).getReferenceName());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -418,6 +411,29 @@ public class Java8MapApiInspection extends BaseJavaBatchLocalInspectionTool {
|
||||
}
|
||||
CodeStyleManager.getInstance(project).reformat(result);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String getNameCandidate(String name) {
|
||||
// Either last uppercase letter (if it's not the last letter) or the first letter, removing leading underscores
|
||||
// token -> t
|
||||
// myAccessToken -> t
|
||||
// SQL -> s
|
||||
// __name -> n
|
||||
// __1 -> k
|
||||
String nameCandidate;
|
||||
name = name.replaceFirst("^[_\\d]+", "");
|
||||
if (name.isEmpty()) return "k";
|
||||
nameCandidate = name.substring(0, 1);
|
||||
for (int pos = name.length() - 1; pos > 0; pos--) {
|
||||
if (Character.isUpperCase(name.charAt(pos))) {
|
||||
if (pos != name.length() - 1) {
|
||||
nameCandidate = name.substring(pos, pos + 1);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return nameCandidate.toLowerCase(Locale.ENGLISH);
|
||||
}
|
||||
}
|
||||
|
||||
class MapCheckCondition {
|
||||
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
// "Replace with 'computeIfAbsent' method call" "true"
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class Main {
|
||||
static class MyItem {
|
||||
String k;
|
||||
int i;
|
||||
|
||||
MyItem(String k, int i) {
|
||||
this.k = k;
|
||||
this.i = i;
|
||||
}
|
||||
}
|
||||
|
||||
public MyItem testMap(Map<String, MyItem> map, String localUser) {
|
||||
MyItem item = map.computeIfAbsent(localUser, u -> new MyItem(u, 1));
|
||||
return item;
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
// "Replace with 'computeIfAbsent' method call" "true"
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class Main {
|
||||
static class MyItem {
|
||||
String k;
|
||||
int i;
|
||||
|
||||
MyItem(String k, int i) {
|
||||
this.k = k;
|
||||
this.i = i;
|
||||
}
|
||||
}
|
||||
|
||||
public MyItem testMap(Map<String, MyItem> map, String ___1) {
|
||||
MyItem item = map.computeIfAbsent(___1, k -> new MyItem(k, 1));
|
||||
return item;
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
// "Replace with 'computeIfAbsent' method call" "true"
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class Main {
|
||||
static class MyItem {
|
||||
String k;
|
||||
int i;
|
||||
|
||||
MyItem(String k, int i) {
|
||||
this.k = k;
|
||||
this.i = i;
|
||||
}
|
||||
}
|
||||
|
||||
public MyItem testMap(Map<String, MyItem> map, String localUser) {
|
||||
MyItem item = map.get(localUser);
|
||||
if(item == nul<caret>l) {
|
||||
map.put(localUser, item = new MyItem(localUser, 1));
|
||||
}
|
||||
return item;
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
// "Replace with 'computeIfAbsent' method call" "true"
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class Main {
|
||||
static class MyItem {
|
||||
String k;
|
||||
int i;
|
||||
|
||||
MyItem(String k, int i) {
|
||||
this.k = k;
|
||||
this.i = i;
|
||||
}
|
||||
}
|
||||
|
||||
public MyItem testMap(Map<String, MyItem> map, String ___1) {
|
||||
MyItem item = map.get(___1);
|
||||
if(item == nul<caret>l) {
|
||||
map.put(___1, item = new MyItem(___1, 1));
|
||||
}
|
||||
return item;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user