Skip to content

Commit 15b0094

Browse files
committed
Speedimprovements of reference resolving
Fixed GotoRelated provider
1 parent 351f89f commit 15b0094

9 files changed

Lines changed: 53 additions & 46 deletions

File tree

.idea/runConfigurations/Mathematica_Plugin.xml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,4 @@ task wrapper(type: Wrapper) {
5656
gradleVersion = '3.5'
5757
}
5858

59-
version '2.4.2'
59+
version '2.4.3'

src/de/halirutan/mathematica/codeinsight/navigation/MathematicaGotoRelatedProvider.java

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,8 @@ public List<? extends GotoRelatedItem> getItems(@NotNull PsiElement psiElement)
6767
PsiElement resolve = ref.resolve();
6868
if (resolve != null) {
6969
if (resolve instanceof Symbol) {
70-
final PsiReference[] resolveReferences = resolve.getReferences();
71-
for (PsiReference reference : resolveReferences) {
72-
final PsiElement usageElement = reference.getElement();
70+
final PsiElement[] resolveReferences = ((Symbol) resolve).getElementsReferencingToMe();
71+
for (PsiElement usageElement : resolveReferences) {
7372
if (usageElement instanceof Symbol && usageElement.isValid()) {
7473
// What follows is that want to collect code around the found element.
7574
// I will collect neighbouring PsiElements but not more than 20 characters to the right and
@@ -80,29 +79,12 @@ public List<? extends GotoRelatedItem> getItems(@NotNull PsiElement psiElement)
8079
final int lineEndOffset = document.getLineEndOffset(lineNumber);
8180
assert lineStartOffset <= lineEndOffset;
8281

83-
PsiElement displayStart = usageElement;
84-
PsiElement displayEnd = usageElement;
85-
while (displayStart.getPrevSibling() != null) {
86-
PsiElement tmpStart = displayStart.getPrevSibling();
87-
if (usageElement.getTextOffset() - tmpStart.getTextOffset() > 20
88-
|| tmpStart.getTextOffset() < lineStartOffset) break;
82+
String testToShow = document.getText(TextRange.create(lineStartOffset, lineEndOffset)).trim();
83+
testToShow = testToShow.length()>80 ? testToShow.substring(0,80) :testToShow;
8984

90-
displayStart = tmpStart;
91-
}
92-
93-
while (displayEnd.getNextSibling() != null) {
94-
PsiElement tmpEnd = displayEnd.getNextSibling();
95-
if (tmpEnd.getTextOffset() - (usageElement.getTextOffset() + usageElement.getTextLength()) > 20
96-
|| tmpEnd.getTextOffset() + tmpEnd.getTextLength() > lineEndOffset) break;
97-
98-
displayEnd = tmpEnd;
99-
}
100-
101-
final String lineText = document.getText(
102-
TextRange.create(displayStart.getTextOffset(), displayEnd.getTextOffset() + displayEnd.getTextLength()));
10385
final GotoSymbolItem item = new GotoSymbolItem(
10486
usageElement,
105-
lineText,
87+
testToShow,
10688
"(" + (lineNumber+1) + " in " + ((Symbol) resolve).getLocalizationConstruct() +")", lineNumber);
10789
declarations.add(item);
10890

src/de/halirutan/mathematica/parsing/psi/MathematicaReferenceContributor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ public class MathematicaReferenceContributor extends PsiReferenceContributor {
3939
*/
4040
@Override
4141
public void registerReferenceProviders(@NotNull PsiReferenceRegistrar registrar) {
42-
registrar.registerReferenceProvider(PlatformPatterns.psiElement(Symbol.class),
43-
new MathematicaSymbolReferenceProvider());
42+
// registrar.registerReferenceProvider(PlatformPatterns.psiElement(Symbol.class),
43+
// new MathematicaSymbolReferenceProvider());
4444
registrar.registerReferenceProvider(PlatformPatterns.psiElement(MString.class),
4545
new MathematicaStringReferenceProvider());
4646
}

src/de/halirutan/mathematica/parsing/psi/api/FunctionCall.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import com.intellij.psi.PsiElement;
2525
import de.halirutan.mathematica.parsing.psi.util.LocalizationConstruct.ConstructType;
26+
import org.jetbrains.annotations.NotNull;
2627
import org.jetbrains.annotations.Nullable;
2728

2829
/**
@@ -51,13 +52,17 @@ public interface FunctionCall extends PsiElement {
5152
/**
5253
* Tests whether the function call has a head the matches the argument.
5354
*
54-
* @param head
55-
* The head which should be tested.
55+
* @param head The head which should be tested.
5656
* @return True, if head matches the Head of the function call.
5757
*/
5858
public boolean matchesHead(String head);
5959

6060

61+
public boolean hasHead(@NotNull final String otherHead);
62+
63+
public boolean hasHead(@NotNull final String[] otherHeads);
64+
65+
6166
/**
6267
* Returns the type of scoping construct, if the function call is e.g. <code >Module[..]</code>
6368
*
@@ -69,8 +74,7 @@ public interface FunctionCall extends PsiElement {
6974
/**
7075
* Returns the n'th argument of a function call <code >f[arg1, arg2, ...]</code> or null, if it does not exist.
7176
*
72-
* @param n
73-
* Argument number, where 0 is the first argument.
77+
* @param n Argument number, where 0 is the first argument.
7478
* @return The PsiElement of the argument or null if it does not exist.
7579
*/
7680
@Nullable

src/de/halirutan/mathematica/parsing/psi/impl/FunctionCallImpl.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,13 @@ public class FunctionCallImpl extends ExpressionImpl implements FunctionCall {
3838

3939
private final Key<Object> myScopeKey = Key.create("SCOPING_CONSTRUCT");
4040
private boolean myIsUpToDate;
41+
private String myHead;
4142

4243

4344
public FunctionCallImpl(@NotNull ASTNode node) {
4445
super(node);
4546
myIsUpToDate = false;
47+
myHead = node.getFirstChildNode().getText();
4648
}
4749

4850
@Override
@@ -71,9 +73,18 @@ public PsiElement getHead() {
7173

7274
@Override
7375
public boolean matchesHead(final String head) {
74-
PsiElement myHead = getHead();
75-
if (myHead != null) {
76-
return myHead.getText().matches(head);
76+
return myHead != null && head != null && myHead.matches(head);
77+
}
78+
79+
public boolean hasHead(@NotNull final String otherHead) {
80+
return myHead.equals(otherHead);
81+
}
82+
83+
public boolean hasHead(@NotNull final String[] heads) {
84+
for (String head : heads) {
85+
if (head.equals(myHead)) {
86+
return true;
87+
}
7788
}
7889
return false;
7990
}

src/de/halirutan/mathematica/parsing/psi/impl/SymbolImpl.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public void addElementReferencingToMe(Symbol reference) {
168168

169169
@Override
170170
public PsiElement[] getElementsReferencingToMe() {
171-
if (myReferringElements.isEmpty()) return new PsiElement[0];
171+
if (myReferringElements.isEmpty()) return PsiElement.EMPTY_ARRAY;
172172
return myReferringElements.toArray(new Symbol[myReferringElements.size()]);
173173
}
174174

@@ -181,10 +181,10 @@ public void accept(@NotNull PsiElementVisitor visitor) {
181181
}
182182
}
183183

184-
@NotNull
185-
@Override
186-
public PsiReference[] getReferences() {
187-
return ReferenceProvidersRegistry.getReferencesFromProviders(this);
188-
}
184+
// @NotNull
185+
// @Override
186+
// public PsiReference[] getReferences() {
187+
// return ReferenceProvidersRegistry.getReferencesFromProviders(this);
188+
// }
189189

190190
}

src/de/halirutan/mathematica/parsing/psi/impl/assignment/SetDefinitionSymbolVisitor.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,13 @@
3838
* @author patrick (7/3/14)
3939
*/
4040
public class SetDefinitionSymbolVisitor extends MathematicaVisitor {
41+
42+
private static final String[] VALUES1 = {"Options", "Attributes", "MessageName", "Default", "Format", "N", "SyntaxInformation"};
43+
private static final String[] VALUES2 = {"Options","Attributes","MessageName","Default","SyntaxInformation"};
44+
private static final String[] PATTERNS = {"HoldPattern","Longest","Shortest","Repeated"};
45+
4146
private static final HashMap<String, SymbolAssignmentType> ourHeadAssignmentMapping;
47+
4248
static {
4349
ourHeadAssignmentMapping = new HashMap<>(6);
4450
ourHeadAssignmentMapping.put("Options", SymbolAssignmentType.OPTIONS_ASSIGNMENT);
@@ -100,14 +106,14 @@ public void visitFunctionCall(FunctionCall functionCall) {
100106
if (head instanceof Symbol) {
101107
// The next set are symbols that are just ignored and we have to check their first argument for a symbol
102108
// which is defined
103-
if (functionCall.matchesHead("HoldPattern|Longest|Shortest|Repeated")) {
109+
if (functionCall.hasHead(PATTERNS)) {
104110
final PsiElement arg1 = functionCall.getArgument(1);
105111
if (arg1 != null) {
106112
arg1.accept(this);
107113
}
108114
}
109115
// check if we have an assignment of the form Options[sym] = {...}
110-
if (functionCall.equals(myStartElement) && functionCall.matchesHead("Options|Attributes|MessageName|Default|Format|N|SyntaxInformation")) {
116+
if (functionCall.equals(myStartElement) && functionCall.hasHead(VALUES1)) {
111117
if (myFoundAssignmentType) {
112118
// we already saw eg Options[..] and this cannot be handled any further
113119
return;
@@ -116,7 +122,7 @@ public void visitFunctionCall(FunctionCall functionCall) {
116122
myFoundAssignmentType = true;
117123
PsiElement arg1 = functionCall.getArgument(1);
118124
if (arg1 != null) {
119-
if (functionCall.matchesHead("Options|Attributes|MessageName|Default|SyntaxInformation")) {
125+
if (functionCall.hasHead(VALUES2)) {
120126
if (arg1 instanceof Symbol) myUnboundSymbols.add((Symbol) arg1);
121127
} else {
122128
//if we have for instance N[e : poly[cp_], pa_] := ... where the argument itself can be a complicated

src/de/halirutan/mathematica/parsing/psi/util/GlobalDefinitionResolveProcessor.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,15 @@ public boolean execute(@NotNull PsiElement element) {
5858
}
5959

6060
if (element instanceof FunctionCall) {
61+
final FunctionCall call = (FunctionCall) element;
6162
final PsiElement lhs = ((FunctionCall) element).getArgument(1);
62-
if (((FunctionCall) element).matchesHead("Set|SetDelayed")) {
63+
if (call.hasHead("Set") || call.hasHead("SetDelayed")) {
6364
return visitSetDefinition(lhs);
64-
} else if (((FunctionCall) element).matchesHead("TagSet|TagSetDelayed")) {
65+
} else if (call.hasHead("TagSet") || call.hasHead("TagSetDelayed")) {
6566
return visitTagSetDefinition(lhs);
66-
} else if (((FunctionCall) element).matchesHead("UpSet|UpSetDelayed")) {
67+
} else if (call.hasHead("UpSet")|| call.hasHead("UpSetDelayed")) {
6768
return visitUpSetDefinition(lhs);
68-
} else if (((FunctionCall) element).matchesHead("SetAttributes|SetOptions") && lhs instanceof Symbol) {
69+
} else if ((call.hasHead("SetAttributes") || call.hasHead("SetOptions")) && lhs instanceof Symbol) {
6970
return visitSymbol((Symbol) lhs);
7071
}
7172
}

0 commit comments

Comments
 (0)