Java: better name suggestions for boxed types, filter out common bad names and add special cases for common methods

GitOrigin-RevId: 2bf3977d2873bd133ac6acb85df54aece740f428
This commit is contained in:
Bas Leijdekkers
2023-02-13 17:04:12 +01:00
committed by intellij-monorepo-bot
parent f0be15b132
commit 9a44d02318
46 changed files with 192 additions and 134 deletions

View File

@@ -264,6 +264,7 @@ public class JavaCodeStyleManagerImpl extends JavaCodeStyleManager {
_propertyName = namesByExpr != null ? namesByExpr.propertyName : null;
}
filterOutBadNames(names);
addNamesFromStatistics(names, kind, _propertyName, type);
String[] namesArray = ArrayUtilRt.toStringArray(names);
@@ -280,6 +281,11 @@ public class JavaCodeStyleManagerImpl extends JavaCodeStyleManager {
};
}
private static void filterOutBadNames(Set<String> names) {
names.remove("of");
names.remove("to");
}
private static void addNamesFromStatistics(@NotNull Set<? super String> names,
@NotNull VariableKind variableKind,
@Nullable String propertyName,
@@ -332,16 +338,20 @@ public class JavaCodeStyleManagerImpl extends JavaCodeStyleManager {
@NotNull
private Collection<String> doSuggestNamesByType(@NotNull PsiType type, @NotNull final VariableKind variableKind) {
String fromTypeMap = suggestNameFromTypeMap(type, variableKind, getLongTypeName(type));
if (fromTypeMap != null) {
if (fromTypeMap != null && type instanceof PsiPrimitiveType) {
return Collections.singletonList(fromTypeMap);
}
final Collection<String> suggestions = new LinkedHashSet<>();
if (fromTypeMap != null) {
suggestions.add(fromTypeMap);
}
List<String> fromTypeName = suggestNamesFromTypeName(type, variableKind, getTypeName(type));
if (!(type instanceof PsiClassType classType)) {
return fromTypeName;
suggestions.addAll(fromTypeName);
return suggestions;
}
final Collection<String> suggestions = new LinkedHashSet<>();
suggestNamesForCollectionInheritors(classType, suggestions);
suggestFromOptionalContent(variableKind, classType, suggestions);
suggestNamesFromGenericParameters(classType, suggestions);
@@ -366,7 +376,7 @@ public class JavaCodeStyleManagerImpl extends JavaCodeStyleManager {
@Nullable
private static String nameByType(@NotNull String longTypeName, @NotNull VariableKind kind) {
if (kind == VariableKind.PARAMETER || kind == VariableKind.LOCAL_VARIABLE) {
if (kind == VariableKind.PARAMETER) {
return switch (longTypeName) {
case "int", "boolean", "byte", "char", "long" -> longTypeName.substring(0, 1);
case "double", "float" -> "v";
@@ -377,6 +387,20 @@ public class JavaCodeStyleManagerImpl extends JavaCodeStyleManager {
default -> null;
};
}
if (kind == VariableKind.LOCAL_VARIABLE) {
return switch (longTypeName) {
case "int", "boolean", "byte", "char", "long" -> longTypeName.substring(0, 1);
case "double", "float", CommonClassNames.JAVA_LANG_DOUBLE, CommonClassNames.JAVA_LANG_FLOAT -> "v";
case "short", CommonClassNames.JAVA_LANG_SHORT, CommonClassNames.JAVA_LANG_INTEGER -> "i";
case CommonClassNames.JAVA_LANG_LONG -> "l";
case CommonClassNames.JAVA_LANG_BOOLEAN, CommonClassNames.JAVA_LANG_BYTE -> "b";
case CommonClassNames.JAVA_LANG_CHARACTER -> "c";
case CommonClassNames.JAVA_LANG_OBJECT -> "o";
case CommonClassNames.JAVA_LANG_STRING -> "s";
case CommonClassNames.JAVA_LANG_VOID -> "unused";
default -> null;
};
}
return null;
}
@@ -694,7 +718,7 @@ public class JavaCodeStyleManagerImpl extends JavaCodeStyleManager {
}
}
else if (words.length == 1 || useAllMethodNames) {
if (Registry.is("add.past.participle.to.suggested.names") && !"equals".equals(firstWord)) {
if (Registry.is("add.past.participle.to.suggested.names") && !"equals".equals(firstWord) && !"valueOf".equals(methodName)) {
words[0] = PastParticiple.pastParticiple(firstWord);
return new NamesByExprInfo(methodName, words[0], StringUtil.join(words));
}
@@ -1003,6 +1027,9 @@ public class JavaCodeStyleManagerImpl extends JavaCodeStyleManager {
if (wordByPreposition != null && (!correctKeywords || isIdentifier(wordByPreposition))) {
answer.add(wordByPreposition);
}
if (name.equals("hashCode")) {
answer.add("hash");
}
return answer;
}

View File

@@ -3,7 +3,7 @@ class C {
void f() {
String s = "";
if (s instanceof String) {
String s1 = (String) s;
String string = (String) s;
<caret>
}
}

View File

@@ -3,7 +3,7 @@ class C {
void f(Object o, Object f) {
if (o instanceof String) {
Float s = 1f;
String o1 = (String) o;
String string = (String) o;
<caret>
}
}

View File

@@ -3,7 +3,7 @@ class C {
void f(Object o, Object f) {
if (o instanceof String && f instanceof String) {
String s = (String) o;
String f1 = (String) f;
String string = (String) f;
<caret>
}
}

View File

@@ -5,7 +5,7 @@ class C {
void x() {
if (x instanceof String) {
String s1 = (String) x;
String string = (String) x;
<caret>
}
}

View File

@@ -4,6 +4,6 @@ import java.util.*;
public class Main {
public static Runnable get(Optional<String> s) {
return s.<Runnable>map(value -> value::trim).orElse(null);
return s.<Runnable>map(string -> string::trim).orElse(null);
}
}

View File

@@ -82,7 +82,7 @@ class Test {
}
String flatMapWithOrInside() {
Object o1 = null;
Object object = null;
String empty = null;
throw new NoSuchElementException("No value present");
}

View File

@@ -17,7 +17,7 @@ class Test {
}
String removesRedundantAssignment(String in) {
String s1 = null;
String string = null;
String s = null;
if (in == null) throw new NullPointerException();
s = in;

View File

@@ -9,12 +9,12 @@ public class Main {
long count = 0L;
Set<Integer> uniqueValues = new HashSet<>();
boolean first = true;
for (Integer integer : new Integer[]{1, 2, 3, 2, 3}) {
for (Integer i : new Integer[]{1, 2, 3, 2, 3}) {
if (first) {
first = false;
continue;
}
if (uniqueValues.add(integer)) {
if (uniqueValues.add(i)) {
count++;
}
}

View File

@@ -9,8 +9,8 @@ public class Main {
private static List<Integer> test(int[] numbers) {
List<Integer> list = new ArrayList<>();
for (int number : numbers) {
Integer integer = number;
list.add(integer);
Integer i = number;
list.add(i);
}
return list;
}

View File

@@ -10,8 +10,8 @@ public class Main {
List<String> result = new ArrayList<>();
Function<? super String, ? extends String> function = list.size() < 10 ? String::trim : Function.identity();
for (String s : list) {
String s1 = function.apply(s);
result.add(s1);
String string = function.apply(s);
result.add(string);
}
System.out.println(result);
}

View File

@@ -10,8 +10,8 @@ public class Main {
List<Integer> list = new ArrayList<>();
for (int x : input) {
if (x > 0) {
Integer integer = x * 2;
list.add(integer);
Integer i = x * 2;
list.add(i);
}
}
return list;

View File

@@ -12,11 +12,11 @@ public class Main {
private static long testChain(List<? extends String> list) {
long count = 0L;
for (Object o : Arrays.asList(0, null, "1", list)) {
for (Object o1 : Arrays.asList(o)) {
for (Object o2 : Arrays.asList(o1)) {
for (Object o3 : Arrays.asList(o2)) {
for (Object o4 : Arrays.asList(o3)) {
for (Object o5 : Arrays.asList(o4)) {
for (Object object : Arrays.asList(o)) {
for (Object o1 : Arrays.asList(object)) {
for (Object object1 : Arrays.asList(o1)) {
for (Object o2 : Arrays.asList(object1)) {
for (Object object2 : Arrays.asList(o2)) {
count++;
}
}

View File

@@ -27,8 +27,8 @@ public class Main {
Integer acc = 0;
SplittableRandom splittableRandom = new SplittableRandom(1);
for (long count = 100; count > 0; count--) {
Integer integer = 500;
acc = splittableRandom.nextInt(acc, integer);
Integer i = 500;
acc = splittableRandom.nextInt(acc, i);
}
int n1 = acc;
System.out.println(n1);

View File

@@ -66,8 +66,8 @@ public class Main {
TreeMap<Integer, List<Integer>> map = new TreeMap<>(Comparator.reverseOrder());
for (String string : strings) {
Integer len = string.length();
Integer integer = len * 2;
map.computeIfAbsent(string.length(), k -> new ArrayList<>()).add(integer);
Integer i = len * 2;
map.computeIfAbsent(string.length(), k -> new ArrayList<>()).add(i);
}
return map;
}

View File

@@ -8,54 +8,54 @@ import java.util.stream.Stream;
public class InExactVariable {
public void testMap() {
HashMap<String, String> map = new HashMap<>();
for (Integer integer2 : Arrays.asList(1, 2, 3, 4)) {
String of = String.valueOf(integer2);
map.putIfAbsent(of.trim(), of);
for (Integer i1 : Arrays.asList(1, 2, 3, 4)) {
String valueOf = String.valueOf(i1);
map.putIfAbsent(valueOf.trim(), valueOf);
}
Object map1 = map;
HashMap<String, String> map2 = new HashMap<>();
for (Integer integer1 : Arrays.asList(1, 2, 3, 4)) {
String valueOf = String.valueOf(integer1);
map2.putIfAbsent(valueOf.trim(), valueOf);
for (Integer integer : Arrays.asList(1, 2, 3, 4)) {
String string = String.valueOf(integer);
map2.putIfAbsent(string.trim(), string);
}
Map<String, String> map3 = new HashMap<>();
for (Integer integer : Arrays.asList(1, 2, 3, 4)) {
String s = String.valueOf(integer);
for (Integer i : Arrays.asList(1, 2, 3, 4)) {
String s = String.valueOf(i);
map3.putIfAbsent(s.trim(), s);
}
}
public void testList() {
List<String> result1 = new ArrayList<>();
for (Integer integer5 : Arrays.asList(1, 2, 3, 4)) {
String valuedOf = String.valueOf(integer5);
result1.add(valuedOf);
for (Integer integer2 : Arrays.asList(1, 2, 3, 4)) {
String string1 = String.valueOf(integer2);
result1.add(string1);
}
Object list1 = result1;
List<String> result = new ArrayList<>();
for (Integer integer4 : Arrays.asList(1, 2, 3, 4)) {
String valued = String.valueOf(integer4);
result.add(valued);
for (Integer i2 : Arrays.asList(1, 2, 3, 4)) {
String s1 = String.valueOf(i2);
result.add(s1);
}
Iterable<String> list2 = result;
Collection<String> list3 = new ArrayList<>();
for (Integer integer3 : Arrays.asList(1, 2, 3, 4)) {
String value = String.valueOf(integer3);
for (Integer integer1 : Arrays.asList(1, 2, 3, 4)) {
String value = String.valueOf(integer1);
list3.add(value);
}
List<String> list4 = new ArrayList<>();
for (Integer integer2 : Arrays.asList(1, 2, 3, 4)) {
String of = String.valueOf(integer2);
list4.add(of);
for (Integer i1 : Arrays.asList(1, 2, 3, 4)) {
String valueOf = String.valueOf(i1);
list4.add(valueOf);
}
Collection<Object> list5 = new ArrayList<>();
for (Integer integer1 : Arrays.asList(1, 2, 3, 4)) {
String valueOf = String.valueOf(integer1);
list5.add(valueOf);
for (Integer integer : Arrays.asList(1, 2, 3, 4)) {
String string = String.valueOf(integer);
list5.add(string);
}
List<String> list = new ArrayList<>();
for (Integer integer : Arrays.asList(1, 2, 3, 4)) {
String s = String.valueOf(integer);
for (Integer i : Arrays.asList(1, 2, 3, 4)) {
String s = String.valueOf(i);
list.add(s);
}
Collection<?> list6 = list;
@@ -65,45 +65,45 @@ public class InExactVariable {
Map<Boolean, List<String>> map = new HashMap<>();
map.put(false, new ArrayList<>());
map.put(true, new ArrayList<>());
for (Integer integer1 : Arrays.asList(1, 2, 3, 4)) {
String s = String.valueOf(integer1);
for (Integer integer : Arrays.asList(1, 2, 3, 4)) {
String s = String.valueOf(integer);
map.get(s.length() > 1).add(s);
}
Object map1 = map;
Map<Boolean, List<String>> map2 = new HashMap<>();
map2.put(false, new ArrayList<>());
map2.put(true, new ArrayList<>());
for (Integer integer : Arrays.asList(1, 2, 3, 4)) {
String x = String.valueOf(integer);
for (Integer i : Arrays.asList(1, 2, 3, 4)) {
String x = String.valueOf(i);
map2.get(x.length() > 1).add(x);
}
}
public void testGroupingBy() {
TreeMap<Integer, Set<String>> result = new TreeMap<>();
for (Integer integer4 : Arrays.asList(1, 2, 3, 4)) {
String valued = String.valueOf(integer4);
result.computeIfAbsent(valued.length(), k2 -> new HashSet<>()).add(valued);
for (Integer i2 : Arrays.asList(1, 2, 3, 4)) {
String s1 = String.valueOf(i2);
result.computeIfAbsent(s1.length(), k2 -> new HashSet<>()).add(s1);
}
Object map1 = result;
TreeMap<Integer, Set<String>> map2 = new TreeMap<>();
for (Integer integer3 : Arrays.asList(1, 2, 3, 4)) {
String value = String.valueOf(integer3);
for (Integer integer1 : Arrays.asList(1, 2, 3, 4)) {
String value = String.valueOf(integer1);
map2.computeIfAbsent(value.length(), key1 -> new HashSet<>()).add(value);
}
NavigableMap<Integer, Set<String>> map3 = new TreeMap<>();
for (Integer integer2 : Arrays.asList(1, 2, 3, 4)) {
String of = String.valueOf(integer2);
map3.computeIfAbsent(of.length(), k1 -> new HashSet<>()).add(of);
for (Integer i1 : Arrays.asList(1, 2, 3, 4)) {
String valueOf = String.valueOf(i1);
map3.computeIfAbsent(valueOf.length(), k1 -> new HashSet<>()).add(valueOf);
}
SortedMap<Integer, Set<String>> map4 = new TreeMap<>();
for (Integer integer1 : Arrays.asList(1, 2, 3, 4)) {
String valueOf = String.valueOf(integer1);
map4.computeIfAbsent(valueOf.length(), key -> new HashSet<>()).add(valueOf);
for (Integer integer : Arrays.asList(1, 2, 3, 4)) {
String string = String.valueOf(integer);
map4.computeIfAbsent(string.length(), key -> new HashSet<>()).add(string);
}
TreeMap<Integer, Set<String>> map = new TreeMap<>();
for (Integer integer : Arrays.asList(1, 2, 3, 4)) {
String s = String.valueOf(integer);
for (Integer i : Arrays.asList(1, 2, 3, 4)) {
String s = String.valueOf(i);
map.computeIfAbsent(s.length(), k -> new HashSet<>()).add(s);
}
Cloneable map5 = map;

View File

@@ -10,9 +10,9 @@ public class Main {
DoubleSupplier s = () -> {
long sum = 0;
long count = 0;
for (String s1 : list) {
if (s1 != null) {
sum += s1.length();
for (String string : list) {
if (string != null) {
sum += string.length();
count++;
}
}

View File

@@ -6,9 +6,9 @@ import java.util.stream.Stream;
public class Main {
public void test(String... list) {
Runnable s = () -> {
for (String s1 : list) {
if (s1 != null) {
System.out.println(s1);
for (String string : list) {
if (string != null) {
System.out.println(string);
}
}
};

View File

@@ -47,8 +47,8 @@ public class Main {
private static List<String> testNestedUseName() {
List<String> list = new ArrayList<>();
for (int x = 0; x < 20; x++) {
Integer integer = x;
long limit = integer;
Integer i = x;
long limit = i;
for (String str = ""; ; str = "a" + str) {
if (limit-- == 0) break;
list.add(str);

View File

@@ -8,8 +8,8 @@ public class Main {
List<Integer> list = new ArrayList<>();
for (String string : strings) {
Integer len = string.length();
Integer integer = len * 2;
list.add(integer);
Integer i = len * 2;
list.add(i);
}
System.out.println(list);
}

View File

@@ -36,8 +36,8 @@ public class Main {
System.out.println(j);
StringJoiner joiner = new StringJoiner(",");
for (String s1 : list) {
joiner.add(s1);
for (String string : list) {
joiner.add(string);
}
for(String s = joiner.toString(); !s.isEmpty(); s = s.substring(1)) {
System.out.println(s);

View File

@@ -27,17 +27,17 @@ public class Test {
boolean dropping = true;
OUTER:
for (String s : data) {
for (String s1 : Arrays.asList(s, s + s)) {
if (s1.isEmpty()) {
for (String string : Arrays.asList(s, s + s)) {
if (string.isEmpty()) {
break OUTER;
}
if (dropping) {
if (s1.length() < 3) {
if (string.length() < 3) {
continue;
}
dropping = false;
}
xyz.add(s1);
xyz.add(string);
}
}
System.out.println(xyz);

View File

@@ -8,8 +8,8 @@ public class Main {
private static Object[] test(int[] numbers) {
List<Integer> list = new ArrayList<>();
for (int number : numbers) {
Integer integer = number;
list.add(integer);
Integer i = number;
list.add(i);
}
return list.toArray();
}
@@ -27,8 +27,8 @@ public class Main {
private static Number[] testCovariant(int[] numbers) {
List<Integer> list = new ArrayList<>();
for (int number : numbers) {
Integer integer = number;
list.add(integer);
Integer i = number;
list.add(i);
}
return list.toArray(new Integer[0]);
}
@@ -36,8 +36,8 @@ public class Main {
private static Number[] testCovariantLambda(int[] numbers) {
List<Integer> list = new ArrayList<>();
for (int number : numbers) {
Integer integer = number;
list.add(integer);
Integer i = number;
list.add(i);
}
return list.toArray(new Integer[0]);
}

View File

@@ -8,8 +8,8 @@ public class Test {
static void test() {
for (int n = 1; n < 100; n++) {
if (n > 20) {
Integer integer = n;
for (PrimitiveIterator.OfDouble it = new Random(integer).doubles(integer).iterator(); it.hasNext(); ) {
Integer i = n;
for (PrimitiveIterator.OfDouble it = new Random(i).doubles(i).iterator(); it.hasNext(); ) {
double v = it.next();
if (v < 0.01) {
System.out.println(v);

View File

@@ -14,8 +14,8 @@ public class Test {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
for (String s : Arrays.asList("a", "b", "c")) {
String doProcess = doProcess(s);
list.add(doProcess);
String string = doProcess(s);
list.add(string);
}
System.out.println(list);
}

View File

@@ -1,7 +1,7 @@
public class Foo {
void m(Object o) {
if (o instanceof Boolean) {
final Boolean aBoolean = (Boolean) o;
final Boolean b = (Boolean) o;
}
}
}

View File

@@ -1,7 +1,7 @@
public class Foo {
void m(Object o) {
if (o instanceof Boolean) {
Boolean aBoolean = (Boolean) o;
Boolean b = (Boolean) o;
}
}
}

View File

@@ -4,9 +4,9 @@ import java.nio.file.*;
class C {
public boolean isRelativeDirectory() {
String fileName = "foo";
Path of = Path.of(fileName);
if (of.isAbsolute()) return false;
Path path = Path.of(fileName);
if (path.isAbsolute()) return false;
System.out.println(fileName);
return Files.isDirectory(of);
return Files.isDirectory(path);
}
}

View File

@@ -44,7 +44,7 @@ public class CodeStyleManagerTest extends LightJavaCodeInsightFixtureTestCase {
JANUARY
}
}
""", VariableKind.LOCAL_VARIABLE, "some", "s");
""", VariableKind.LOCAL_VARIABLE, "some", "s", "string");
}
public void testSuggestSemanticNameVariableNameParameter() {
@@ -62,6 +62,37 @@ public class CodeStyleManagerTest extends LightJavaCodeInsightFixtureTestCase {
""", VariableKind.PARAMETER, "month2", "month");
}
public void testShortNameForBoxedTypes() {
//noinspection ALL
checkSuggestedNames("""
class X {
void x() {
Integer.<caret>valueOf(1);
}
}
""", VariableKind.LOCAL_VARIABLE, "valueOf", "value", "i", "integer");
}
public void testHashCode() {
checkSuggestedNames("""
class X {
void x(String s) {
s.<caret>hashCode()
}
}
""", VariableKind.LOCAL_VARIABLE, "hashCode", "code", "hash", "i");
}
public void testCompareTo() {
checkSuggestedNames("""
class X {
void x(String s) {
s.<caret>compareTo("");
}
}
""", VariableKind.PARAMETER, "compareTo", "compare", "i");
}
private void checkSuggestedNames(@NotNull @Language("JAVA") String code, @NotNull VariableKind kind, String @NotNull ... expected) {
PsiFile file = myFixture.configureByText("Test.java", code);
PsiElement element = PsiUtilCore.getElementAtOffset(file, myFixture.getCaretOffset());

View File

@@ -108,7 +108,7 @@ public class VariablesCompletionTest extends LightFixtureCompletionTestCase {
@NeedsIndex.ForStandardLibrary(reason = "With empty indices 'Object' may be considered variable name, and instanceof is ok here; Object is not resolved thus not replaced with 'o'")
public void testUnresolvedReference() throws Exception {
configureByFile(FILE_PREFIX + "locals/" + getTestName(false) + ".java");
assertStringItems("o", "psiClass");
assertStringItems("o", "psiClass", "object");
}
public void testFieldNameCompletion1() {
@@ -175,7 +175,7 @@ public class VariablesCompletionTest extends LightFixtureCompletionTestCase {
public void testInitializerMatters() {
myFixture.configureByText(JavaFileType.INSTANCE, "class Foo {{ String f<caret>x = getFoo(); }; String getFoo() {}; }");
complete();
assertStringItems("foo");
assertStringItems("foo", "fString");
}
public void testFieldInitializerMatters() {
@@ -216,7 +216,7 @@ public class VariablesCompletionTest extends LightFixtureCompletionTestCase {
@NeedsIndex.ForStandardLibrary(reason = "On empty indices String is not resolved and replaced with name 's', filtered out in JavaMemberNameCompletionContributor.getOverlappedNameVersions for being short")
public void testConstructorParameterName() {
configure();
assertStringItems("color");
assertStringItems("color", "coString");
}
@NeedsIndex.ForStandardLibrary(reason = "On empty indices String is not resolved and replaced with name 's', filtered out in JavaMemberNameCompletionContributor.getOverlappedNameVersions for being short; P is from PARAMETER_NAME_PREFIX")
@@ -227,7 +227,7 @@ public class VariablesCompletionTest extends LightFixtureCompletionTestCase {
configure();
assertStringItems("pColor");
assertStringItems("pColor", "pCoPString");
}
public void test_finish_with__() {

View File

@@ -19,22 +19,22 @@ public class NameSuggestionsByExpressionTest extends LightJavaCodeInsightFixture
public void testNameSuggestionFromLiteralArgument() {
checkSuggestions("class A {{new Str<caret>ing(\"string with spaces\")}}",
VariableKind.LOCAL_VARIABLE,
new String[]{"stringWithSpaces", "string_with_spaces", "withSpaces", "with_spaces", "spaces", "string", "s"});
"stringWithSpaces", "string_with_spaces", "withSpaces", "with_spaces", "spaces", "string", "s");
}
public void testWordByPreposition() {
checkSuggestions("class A {{getParent<caret>OfType()} String getParentOfType() {return null;}}",
VariableKind.LOCAL_VARIABLE,
new String[]{"getParentOfType", "parentOfType", "ofType", "type", "parent", "s"});
"getParentOfType", "parentOfType", "ofType", "type", "parent", "s", "string");
}
public void testNameByAssignmentContext() {
checkSuggestions("class A {{String bar = \"<caret>\";}}",
VariableKind.PARAMETER,
new String[]{"bar", "s"});
"bar", "s", "string");
}
private void checkSuggestions(String code, VariableKind variableKind, String[] expecteds) {
private void checkSuggestions(String code, VariableKind variableKind, String... expecteds) {
Document fakeDocument = new DocumentImpl(code);
EditorTestUtil.CaretAndSelectionState caretsState = EditorTestUtil.extractCaretAndSelectionMarkers(fakeDocument);
assertTrue("No caret specified", caretsState.hasExplicitCaret());

View File

@@ -1,4 +1,4 @@
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.java.refactoring
import com.intellij.codeInsight.TargetElementUtil
@@ -133,7 +133,7 @@ class Foo {
}
}
""")
assert suggestions == ["cat", "innerCat", "s"]
assert suggestions == ["cat", "innerCat", 'string', "s"]
}
private doTestSuggestionAvailable(String text, String... expectedSuggestions) {

View File

@@ -8,7 +8,7 @@ public class Test {
case Integer y -> y.byteValue();
case String s && x > 0 -> s.length();
case Boolean c -> (Boolean) c ? 42 : 0;
case Character character -> ((BigDecimal) obj).hashCode();
case Character c -> ((BigDecimal) obj).hashCode();
case null, default -> -1;
};
}

View File

@@ -9,7 +9,7 @@ class Test {
case Integer x && x > 0 && x < 10:
System.out.println();
break;
case Float aFloat && variable > 0:
case Float v && variable > 0:
break;
default:
break;

View File

@@ -9,7 +9,7 @@ class Test {
case Integer x && x > 0 && x < 10:
System.out.println();
break;
case Float aFloat && variable > 0:
case Float v && variable > 0:
break;
default:
break;

View File

@@ -12,7 +12,7 @@ public class Test {
case String r && x > 0 -> {
return ((String) obj).length();
}
case Character character -> {
case Character c -> {
return ((BigDecimal) obj).hashCode();
}
case null, default -> {

View File

@@ -5,11 +5,11 @@ import java.math.BigDecimal;
public class Test {
public static void test(Object obj) {
switch (obj) {
case String s1 -> {
String s = s1;
case String string -> {
String s = string;
s = s.repeat(2);
System.out.println(s);
System.out.println(s1);
System.out.println(string);
}
case Integer i -> System.out.println(i * 2);
case null, default -> System.out.println();

View File

@@ -4,7 +4,7 @@ class Test {
void test(@NotNull Object o) {
<caret>switch (o) {
case String s -> System.out.println();
case Integer integer -> System.out.println();
case Integer i -> System.out.println();
default -> {
}
}

View File

@@ -4,7 +4,7 @@ class Test {
void test(@Nullable Object o) {
switch (o) {
case String s -> System.out.println();
case Integer integer, null -> System.out.println();
case Integer i, null -> System.out.println();
default -> {
}
}

View File

@@ -6,7 +6,7 @@ class Test {
case String s:
System.out.println();
break;
case Integer integer:
case Integer i:
System.out.println();
break;
case null:

View File

@@ -6,8 +6,8 @@ class Test {
case String s when !s.isEmpty() -> System.out.println();
case Integer x when x > 0 && x < 10 -> System.out.println();
case Integer x when x == 42 -> System.out.println();
case Integer integer -> System.out.println();
case Float aFloat when variable > 0 -> System.out.println();
case Integer i -> System.out.println();
case Float v when variable > 0 -> System.out.println();
default -> {
}
}

View File

@@ -4,7 +4,7 @@ class Test {
void test(@Nullable Object o) {
switch (o) {
case String s -> System.out.println();
case Integer integer -> System.out.println();
case Integer i -> System.out.println();
case null, default -> {
}
}

View File

@@ -4,7 +4,7 @@ class Test {
void test(@Nullable Object o) {
<caret>switch (o) {
case String s, null -> System.out.println("nullable string");
case Integer integer -> System.out.println("int");
case Integer i -> System.out.println("int");
default -> System.out.println("default");
}
}

View File

@@ -4,7 +4,7 @@ class Test {
void test(@Nullable Object o) {
String r = switch (o) {
case String s && s.length() > 3 -> s.substring(0, 3);
case Integer integer -> "integer";
case Integer i -> "integer";
case null, default -> "default";
};
}

View File

@@ -4,7 +4,7 @@ class Test {
void test(@NotNull Object o) {
switch (o) {
case String s -> System.out.println();
case Integer integer -> System.out.println();
case Integer i -> System.out.println();
default -> {
}
}

View File

@@ -1,4 +1,4 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.siyeh.ig.fixes;
import com.siyeh.InspectionGadgetsBundle;
@@ -97,8 +97,8 @@ public class ExtractParameterAsLocalVariableFixTest extends IGQuickFixesTestCase
"class X {\n" +
" void m() {\n" +
" for (String s : new String[]{\"one\", \"two\", \"three\"}) {\n" +
" String s1 = s;\n" +
" s1 = \"four\";\n" +
" String string = s;\n" +
" string = \"four\";\n" +
" }\n" +
" }\n" +
"}"
@@ -187,9 +187,9 @@ public class ExtractParameterAsLocalVariableFixTest extends IGQuickFixesTestCase
"class Foo {\n" +
" Foo(Object o ) {\n" +
" super();\n" +
" Object o1 = o;\n" +
" if (o1 != null) {\n" +
" o1 = o1.toString();\n" +
" Object object = o;\n" +
" if (object != null) {\n" +
" object = object.toString();\n" +
" }\n" +
" }\n" +
"}");