Skip to content

Commit

Permalink
Speedimprovements of reference resolving
Browse files Browse the repository at this point in the history
Fixed GotoRelated provider
  • Loading branch information
halirutan committed Jul 1, 2017
1 parent 351f89f commit 15b0094
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 46 deletions.
3 changes: 3 additions & 0 deletions .idea/runConfigurations/Mathematica_Plugin.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ task wrapper(type: Wrapper) {
gradleVersion = '3.5'
}

version '2.4.2'
version '2.4.3'
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,8 @@ public List<? extends GotoRelatedItem> getItems(@NotNull PsiElement psiElement)
PsiElement resolve = ref.resolve();
if (resolve != null) {
if (resolve instanceof Symbol) {
final PsiReference[] resolveReferences = resolve.getReferences();
for (PsiReference reference : resolveReferences) {
final PsiElement usageElement = reference.getElement();
final PsiElement[] resolveReferences = ((Symbol) resolve).getElementsReferencingToMe();
for (PsiElement usageElement : resolveReferences) {
if (usageElement instanceof Symbol && usageElement.isValid()) {
// What follows is that want to collect code around the found element.
// I will collect neighbouring PsiElements but not more than 20 characters to the right and
Expand All @@ -80,29 +79,12 @@ public List<? extends GotoRelatedItem> getItems(@NotNull PsiElement psiElement)
final int lineEndOffset = document.getLineEndOffset(lineNumber);
assert lineStartOffset <= lineEndOffset;

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

displayStart = tmpStart;
}

while (displayEnd.getNextSibling() != null) {
PsiElement tmpEnd = displayEnd.getNextSibling();
if (tmpEnd.getTextOffset() - (usageElement.getTextOffset() + usageElement.getTextLength()) > 20
|| tmpEnd.getTextOffset() + tmpEnd.getTextLength() > lineEndOffset) break;

displayEnd = tmpEnd;
}

final String lineText = document.getText(
TextRange.create(displayStart.getTextOffset(), displayEnd.getTextOffset() + displayEnd.getTextLength()));
final GotoSymbolItem item = new GotoSymbolItem(
usageElement,
lineText,
testToShow,
"(" + (lineNumber+1) + " in " + ((Symbol) resolve).getLocalizationConstruct() +")", lineNumber);
declarations.add(item);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public class MathematicaReferenceContributor extends PsiReferenceContributor {
*/
@Override
public void registerReferenceProviders(@NotNull PsiReferenceRegistrar registrar) {
registrar.registerReferenceProvider(PlatformPatterns.psiElement(Symbol.class),
new MathematicaSymbolReferenceProvider());
// registrar.registerReferenceProvider(PlatformPatterns.psiElement(Symbol.class),
// new MathematicaSymbolReferenceProvider());
registrar.registerReferenceProvider(PlatformPatterns.psiElement(MString.class),
new MathematicaStringReferenceProvider());
}
Expand Down
12 changes: 8 additions & 4 deletions src/de/halirutan/mathematica/parsing/psi/api/FunctionCall.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import com.intellij.psi.PsiElement;
import de.halirutan.mathematica.parsing.psi.util.LocalizationConstruct.ConstructType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
Expand Down Expand Up @@ -51,13 +52,17 @@ public interface FunctionCall extends PsiElement {
/**
* Tests whether the function call has a head the matches the argument.
*
* @param head
* The head which should be tested.
* @param head The head which should be tested.
* @return True, if head matches the Head of the function call.
*/
public boolean matchesHead(String head);


public boolean hasHead(@NotNull final String otherHead);

public boolean hasHead(@NotNull final String[] otherHeads);


/**
* Returns the type of scoping construct, if the function call is e.g. <code >Module[..]</code>
*
Expand All @@ -69,8 +74,7 @@ public interface FunctionCall extends PsiElement {
/**
* Returns the n'th argument of a function call <code >f[arg1, arg2, ...]</code> or null, if it does not exist.
*
* @param n
* Argument number, where 0 is the first argument.
* @param n Argument number, where 0 is the first argument.
* @return The PsiElement of the argument or null if it does not exist.
*/
@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ public class FunctionCallImpl extends ExpressionImpl implements FunctionCall {

private final Key<Object> myScopeKey = Key.create("SCOPING_CONSTRUCT");
private boolean myIsUpToDate;
private String myHead;


public FunctionCallImpl(@NotNull ASTNode node) {
super(node);
myIsUpToDate = false;
myHead = node.getFirstChildNode().getText();
}

@Override
Expand Down Expand Up @@ -71,9 +73,18 @@ public PsiElement getHead() {

@Override
public boolean matchesHead(final String head) {
PsiElement myHead = getHead();
if (myHead != null) {
return myHead.getText().matches(head);
return myHead != null && head != null && myHead.matches(head);
}

public boolean hasHead(@NotNull final String otherHead) {
return myHead.equals(otherHead);
}

public boolean hasHead(@NotNull final String[] heads) {
for (String head : heads) {
if (head.equals(myHead)) {
return true;
}
}
return false;
}
Expand Down
12 changes: 6 additions & 6 deletions src/de/halirutan/mathematica/parsing/psi/impl/SymbolImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public void addElementReferencingToMe(Symbol reference) {

@Override
public PsiElement[] getElementsReferencingToMe() {
if (myReferringElements.isEmpty()) return new PsiElement[0];
if (myReferringElements.isEmpty()) return PsiElement.EMPTY_ARRAY;
return myReferringElements.toArray(new Symbol[myReferringElements.size()]);
}

Expand All @@ -181,10 +181,10 @@ public void accept(@NotNull PsiElementVisitor visitor) {
}
}

@NotNull
@Override
public PsiReference[] getReferences() {
return ReferenceProvidersRegistry.getReferencesFromProviders(this);
}
// @NotNull
// @Override
// public PsiReference[] getReferences() {
// return ReferenceProvidersRegistry.getReferencesFromProviders(this);
// }

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@
* @author patrick (7/3/14)
*/
public class SetDefinitionSymbolVisitor extends MathematicaVisitor {

private static final String[] VALUES1 = {"Options", "Attributes", "MessageName", "Default", "Format", "N", "SyntaxInformation"};
private static final String[] VALUES2 = {"Options","Attributes","MessageName","Default","SyntaxInformation"};
private static final String[] PATTERNS = {"HoldPattern","Longest","Shortest","Repeated"};

private static final HashMap<String, SymbolAssignmentType> ourHeadAssignmentMapping;

static {
ourHeadAssignmentMapping = new HashMap<>(6);
ourHeadAssignmentMapping.put("Options", SymbolAssignmentType.OPTIONS_ASSIGNMENT);
Expand Down Expand Up @@ -100,14 +106,14 @@ public void visitFunctionCall(FunctionCall functionCall) {
if (head instanceof Symbol) {
// The next set are symbols that are just ignored and we have to check their first argument for a symbol
// which is defined
if (functionCall.matchesHead("HoldPattern|Longest|Shortest|Repeated")) {
if (functionCall.hasHead(PATTERNS)) {
final PsiElement arg1 = functionCall.getArgument(1);
if (arg1 != null) {
arg1.accept(this);
}
}
// check if we have an assignment of the form Options[sym] = {...}
if (functionCall.equals(myStartElement) && functionCall.matchesHead("Options|Attributes|MessageName|Default|Format|N|SyntaxInformation")) {
if (functionCall.equals(myStartElement) && functionCall.hasHead(VALUES1)) {
if (myFoundAssignmentType) {
// we already saw eg Options[..] and this cannot be handled any further
return;
Expand All @@ -116,7 +122,7 @@ public void visitFunctionCall(FunctionCall functionCall) {
myFoundAssignmentType = true;
PsiElement arg1 = functionCall.getArgument(1);
if (arg1 != null) {
if (functionCall.matchesHead("Options|Attributes|MessageName|Default|SyntaxInformation")) {
if (functionCall.hasHead(VALUES2)) {
if (arg1 instanceof Symbol) myUnboundSymbols.add((Symbol) arg1);
} else {
//if we have for instance N[e : poly[cp_], pa_] := ... where the argument itself can be a complicated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,15 @@ public boolean execute(@NotNull PsiElement element) {
}

if (element instanceof FunctionCall) {
final FunctionCall call = (FunctionCall) element;
final PsiElement lhs = ((FunctionCall) element).getArgument(1);
if (((FunctionCall) element).matchesHead("Set|SetDelayed")) {
if (call.hasHead("Set") || call.hasHead("SetDelayed")) {
return visitSetDefinition(lhs);
} else if (((FunctionCall) element).matchesHead("TagSet|TagSetDelayed")) {
} else if (call.hasHead("TagSet") || call.hasHead("TagSetDelayed")) {
return visitTagSetDefinition(lhs);
} else if (((FunctionCall) element).matchesHead("UpSet|UpSetDelayed")) {
} else if (call.hasHead("UpSet")|| call.hasHead("UpSetDelayed")) {
return visitUpSetDefinition(lhs);
} else if (((FunctionCall) element).matchesHead("SetAttributes|SetOptions") && lhs instanceof Symbol) {
} else if ((call.hasHead("SetAttributes") || call.hasHead("SetOptions")) && lhs instanceof Symbol) {
return visitSymbol((Symbol) lhs);
}
}
Expand Down

0 comments on commit 15b0094

Please sign in to comment.