Skip to content

Commit 351512d

Browse files
committed
Implemented a recursive visitor that checks each occurrence of a symbol for renaming
1 parent 8be2c04 commit 351512d

3 files changed

Lines changed: 41 additions & 7 deletions

File tree

Mathematica-IntelliJ-Plugin.iml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="PLUGIN_MODULE" version="4">
33
<component name="DevKit.ModuleBuildProperties" url="file://$MODULE_DIR$/resources/META-INF/plugin.xml" manifest="file://$MODULE_DIR$/resources/META-INF/plugin.xml" />
4-
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_6" inherit-compiler-output="true">
4+
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8" inherit-compiler-output="true">
55
<content url="file://$MODULE_DIR$">
66
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
77
<sourceFolder url="file://$MODULE_DIR$/resources" type="java-resource" />

resources/META-INF/plugin.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@
168168
<psi.referenceContributor language="Mathematica" implementation="de.halirutan.mathematica.parsing.psi.MathematicaReferenceContributor"/>
169169
<renamePsiElementProcessor implementation="de.halirutan.mathematica.refactoring.MathematicaPsiRenameProcessor"/>
170170

171+
171172
<!--<applicationService serviceInterface="de.halirutan.mathematica.MathematicaSettings" serviceImplementation="de.halirutan.mathematica.MathematicaSettings"/>-->
172173
<!--<applicationConfigurable groupId="language" displayName="Mathematica" id="preferences.Mathematica"-->
173174
<!--instance="de.halirutan.mathematica.MathematicaSettingsConfigurable"/>-->

src/de/halirutan/mathematica/refactoring/MathematicaPsiRenameProcessor.java

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,25 @@
2323

2424
import com.intellij.psi.PsiElement;
2525
import com.intellij.psi.PsiReference;
26+
import com.intellij.psi.util.PsiTreeUtil;
2627
import com.intellij.refactoring.rename.RenamePsiElementProcessor;
28+
import de.halirutan.mathematica.parsing.psi.MathematicaRecursiveVisitor;
2729
import de.halirutan.mathematica.parsing.psi.api.Symbol;
2830
import de.halirutan.mathematica.parsing.psi.api.string.MString;
2931
import org.jetbrains.annotations.NotNull;
3032

3133
import 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
*/
4147
public 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

Comments
 (0)