mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
PsiTreeAnchorizer: wrap the anchor to prevent instanceof confusion
This commit is contained in:
+33
-6
@@ -17,6 +17,7 @@ package com.intellij.ide.projectView.impl.nodes;
|
||||
|
||||
import com.intellij.ide.util.treeView.TreeAnchorizer;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Computable;
|
||||
import com.intellij.openapi.util.Key;
|
||||
import com.intellij.psi.PsiElement;
|
||||
@@ -29,7 +30,7 @@ import org.jetbrains.annotations.Nullable;
|
||||
*/
|
||||
public class PsiTreeAnchorizer extends TreeAnchorizer {
|
||||
|
||||
private static final Key<SmartPsiElementPointer> PSI_ANCHORIZER_POINTER = Key.create("PSI_ANCHORIZER_ANCHOR");
|
||||
private static final Key<SmartPointerWrapper> PSI_ANCHORIZER_POINTER = Key.create("PSI_ANCHORIZER_POINTER");
|
||||
|
||||
@Override
|
||||
public Object createAnchor(Object element) {
|
||||
@@ -39,13 +40,14 @@ public class PsiTreeAnchorizer extends TreeAnchorizer {
|
||||
return ApplicationManager.getApplication().runReadAction(new Computable<Object>() {
|
||||
@Override
|
||||
public Object compute() {
|
||||
SmartPsiElementPointer pointer = psiElement.getUserData(PSI_ANCHORIZER_POINTER);
|
||||
SmartPointerWrapper pointer = psiElement.getUserData(PSI_ANCHORIZER_POINTER);
|
||||
if (!psiElement.isValid()) {
|
||||
return pointer != null ? pointer : psiElement;
|
||||
}
|
||||
|
||||
if (pointer == null || pointer.getElement() != psiElement) {
|
||||
pointer = SmartPointerManager.getInstance(psiElement.getProject()).createSmartPsiElementPointer(psiElement);
|
||||
if (pointer == null || pointer.myPointer.getElement() != psiElement) {
|
||||
Project project = psiElement.getProject();
|
||||
pointer = new SmartPointerWrapper(SmartPointerManager.getInstance(project).createSmartPsiElementPointer(psiElement));
|
||||
psiElement.putUserData(PSI_ANCHORIZER_POINTER, pointer);
|
||||
}
|
||||
return pointer;
|
||||
@@ -57,10 +59,35 @@ public class PsiTreeAnchorizer extends TreeAnchorizer {
|
||||
@Override
|
||||
@Nullable
|
||||
public Object retrieveElement(Object pointer) {
|
||||
if (pointer instanceof SmartPsiElementPointer) {
|
||||
return ((SmartPsiElementPointer)pointer).getElement();
|
||||
if (pointer instanceof SmartPointerWrapper) {
|
||||
return ((SmartPointerWrapper)pointer).myPointer.getElement();
|
||||
}
|
||||
|
||||
return super.retrieveElement(pointer);
|
||||
}
|
||||
|
||||
private static class SmartPointerWrapper {
|
||||
private final SmartPsiElementPointer myPointer;
|
||||
|
||||
private SmartPointerWrapper(SmartPsiElementPointer pointer) {
|
||||
myPointer = pointer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof SmartPointerWrapper)) return false;
|
||||
|
||||
SmartPointerWrapper wrapper = (SmartPointerWrapper)o;
|
||||
|
||||
if (!myPointer.equals(wrapper.myPointer)) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return myPointer.hashCode();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user