2323
2424import com .intellij .psi .PsiElement ;
2525import com .intellij .psi .PsiReference ;
26+ import com .intellij .psi .util .PsiTreeUtil ;
2627import com .intellij .refactoring .rename .RenamePsiElementProcessor ;
28+ import de .halirutan .mathematica .parsing .psi .MathematicaRecursiveVisitor ;
2729import de .halirutan .mathematica .parsing .psi .api .Symbol ;
2830import de .halirutan .mathematica .parsing .psi .api .string .MString ;
2931import org .jetbrains .annotations .NotNull ;
3032
3133import java .util .Collection ;
34+ import java .util .HashSet ;
35+ import java .util .Objects ;
3236
33- /** This class shouldn't be necessary since usually Idea does a great job of renaming if you have
37+ /**
38+ * This class shouldn't be necessary since usually Idea does a great job of renaming if you have
3439 * your symbol references working correctly. Unfortunately there is a bug that was introduces by some
3540 * optimisation {@see https://youtrack.jetbrains.com/issue/IDEA-165760}.
36- *
41+ * <p>
3742 * Therefore, this class just adds some references that were missed by Idea. This will be removed as soon
3843 * as the bug is fixed.
44+ *
3945 * @author patrick (24.12.16).
4046 */
4147public class MathematicaPsiRenameProcessor extends RenamePsiElementProcessor {
@@ -48,13 +54,40 @@ public boolean canProcessElement(@NotNull PsiElement element) {
4854 @ Override
4955 public Collection <PsiReference > findReferences (PsiElement element ) {
5056 final Collection <PsiReference > references = super .findReferences (element );
51- final PsiReference [] myReferences = element .getReferences ();
52- for (PsiReference ref : myReferences ) {
53- if (!references .contains (ref )) {
54- references .add (ref );
57+ PsiReference elementRef = element .getReference ();
58+ PsiElement definitionElement ;
59+ if (elementRef != null ) {
60+ definitionElement = elementRef .resolve ();
61+ if (definitionElement instanceof Symbol ) {
62+ SymbolCollector collector = new SymbolCollector ((Symbol ) definitionElement );
63+ element .getContainingFile ().accept (collector );
64+ references .addAll (collector .myReferences );
5565 }
5666 }
5767 return references ;
5868 }
5969
70+ private class SymbolCollector extends MathematicaRecursiveVisitor {
71+ Collection <PsiReference > myReferences ;
72+ Symbol myDefinitionElement ;
73+
74+ SymbolCollector (Symbol definitionElement ) {
75+ this .myDefinitionElement = definitionElement ;
76+ myReferences = new HashSet <>();
77+ }
78+
79+ @ Override
80+ public void visitSymbol (Symbol symbol ) {
81+ if (symbol .getSymbolName ().equals (myDefinitionElement .getSymbolName ())) {
82+ PsiReference reference = symbol .getReference ();
83+ if (reference != null ) {
84+ PsiElement resolve = reference .resolve ();
85+ if (resolve != null && resolve .equals (myDefinitionElement )) {
86+ myReferences .add (reference );
87+ }
88+ }
89+ }
90+ }
91+ }
92+
6093}
0 commit comments