find occurrences: distinguish <> with different inference (IDEA-66176)

This commit is contained in:
anna
2011-03-11 11:44:42 +01:00
parent ef19124922
commit 1b09d55adc
5 changed files with 44 additions and 2 deletions
@@ -28,6 +28,7 @@ import com.intellij.openapi.util.Computable;
import com.intellij.openapi.util.Condition;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.*;
import com.intellij.psi.impl.source.PsiDiamondTypeElementImpl;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.search.searches.ClassInheritorsSearch;
import com.intellij.psi.tree.IElementType;
@@ -225,7 +226,17 @@ public class CodeInsightUtil {
}
return 1;
}
}, false);
}, new Comparator<PsiElement>() {
@Override
public int compare(PsiElement o1, PsiElement o2) {
if (o1 instanceof PsiDiamondTypeElementImpl && o2 instanceof PsiDiamondTypeElementImpl) {
final PsiDiamondType.DiamondInferenceResult thisInferenceResult = new PsiDiamondType(o1.getManager(), (PsiTypeElement)o1).resolveInferredTypes();
final PsiDiamondType.DiamondInferenceResult otherInferenceResult = new PsiDiamondType(o2.getManager(), (PsiTypeElement)o2).resolveInferredTypes();
return thisInferenceResult.equals(otherInferenceResult) ? 0 : 1;
}
return 0;
}
}, false);
}
public static Editor positionCursor(final Project project, PsiFile targetFile, PsiElement element) {
@@ -0,0 +1,9 @@
import java.util.*;
class Test {
void foo(final ArrayList<String> anObject) {
List<String> ls = anObject;
List<String> lss = anObject;
List<Integer> li = new ArrayList<>();
}
}
@@ -0,0 +1,9 @@
import java.util.*;
class Test {
void foo() {
List<String> ls = <selection>new ArrayList<>()</selection>;
List<String> lss = new ArrayList<>();
List<Integer> li = new ArrayList<>();
}
}
@@ -264,6 +264,10 @@ public class IntroduceParameterTest extends LightCodeInsightTestCase {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_ALL, true, false, true, false);
}
public void testDiamondOccurrences() throws Exception {
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_ALL, true, false, true, false);
}
private void doTestThroughHandler() throws Exception {
configureByFile("/refactoring/introduceParameter/before" + getTestName(false) + ".java");
new IntroduceParameterHandler().invoke(getProject(), myEditor, myFile, new DataContext() {
@@ -44,6 +44,14 @@ public class PsiEquivalenceUtil {
@NotNull PsiElement element2,
@Nullable Comparator<PsiElement> resolvedElementsComparator,
boolean areCommentsSignificant) {
return areElementsEquivalent(element1, element2, resolvedElementsComparator, null, areCommentsSignificant);
}
public static boolean areElementsEquivalent(@NotNull PsiElement element1,
@NotNull PsiElement element2,
@Nullable Comparator<PsiElement> resolvedElementsComparator,
@Nullable Comparator<PsiElement> leafElementsComparator,
boolean areCommentsSignificant) {
if(element1 == element2) return true;
ASTNode node1 = element1.getNode();
ASTNode node2 = element2.getNode();
@@ -57,11 +65,12 @@ public class PsiEquivalenceUtil {
for (int i = 0; i < children1.length; i++) {
PsiElement child1 = children1[i];
PsiElement child2 = children2[i];
if (!areElementsEquivalent(child1, child2, resolvedElementsComparator, areCommentsSignificant)) return false;
if (!areElementsEquivalent(child1, child2, resolvedElementsComparator, leafElementsComparator, areCommentsSignificant)) return false;
}
if (children1.length == 0) {
if (!element1.textMatches(element2)) return false;
if (leafElementsComparator != null && leafElementsComparator.compare(element1, element2) != 0) return false;
}
PsiReference ref1 = element1.getReference();