Skip to content

Commit 7ca49fa

Browse files
committed
First tests for resolving
Better resolveProcessor for local definitions (unfinished)
1 parent 11bdfb3 commit 7ca49fa

20 files changed

Lines changed: 4220 additions & 138 deletions

.idea/inspectionProfiles/Project_Default.xml

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/de/halirutan/mathematica/codeinsight/highlighting/MathematicaHighlightingAnnotator.java

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
package de.halirutan.mathematica.codeinsight.highlighting;
2323

24-
import com.intellij.codeInspection.ProblemHighlightType;
2524
import com.intellij.lang.ASTNode;
2625
import com.intellij.lang.annotation.Annotation;
2726
import com.intellij.lang.annotation.AnnotationHolder;
@@ -42,9 +41,7 @@
4241
import de.halirutan.mathematica.lang.psi.api.function.Function;
4342
import de.halirutan.mathematica.lang.psi.api.slots.Slot;
4443
import de.halirutan.mathematica.lang.psi.api.slots.SlotExpression;
45-
import de.halirutan.mathematica.lang.psi.util.LocalizationConstruct;
4644
import de.halirutan.mathematica.lang.psi.util.LocalizationConstruct.MScope;
47-
import de.halirutan.mathematica.lang.resolve.SymbolResolveResult;
4845
import org.jetbrains.annotations.NotNull;
4946

5047
import java.util.Set;
@@ -64,8 +61,7 @@ public class MathematicaHighlightingAnnotator extends MathematicaVisitor impleme
6461
private static void setHighlighting(@NotNull PsiElement element, @NotNull AnnotationHolder holder, @NotNull TextAttributesKey key) {
6562
final Annotation annotation = holder.createInfoAnnotation(element, null);
6663
annotation.setTextAttributes(key);
67-
annotation.setHighlightType(ProblemHighlightType.ERROR);
68-
annotation.setNeedsUpdateOnTyping(false);
64+
annotation.setNeedsUpdateOnTyping(true);
6965
}
7066

7167
private static void setHighlightingStrict(@NotNull PsiElement element, @NotNull AnnotationHolder holder, @NotNull TextAttributesKey key) {
@@ -88,33 +84,32 @@ public void annotate(@NotNull PsiElement element, @NotNull final AnnotationHolde
8884

8985
@Override
9086
public void visitSymbol(final Symbol symbol) {
91-
final SymbolResolveResult symbolResolveResult = symbol.advancedResolve();
92-
if (symbolResolveResult != null) {
93-
final MScope scope = symbolResolveResult.getLocalization();
94-
switch (scope) {
95-
case FILE:
96-
setHighlighting(symbol, myHolder, MathematicaSyntaxHighlighterColors.IDENTIFIER);
97-
break;
98-
case NULL:
99-
setHighlighting(symbol, myHolder, CodeInsightColors.WRONG_REFERENCES_ATTRIBUTES);
100-
break;
101-
case BUILT_IN:
102-
setHighlighting(symbol, myHolder, MathematicaSyntaxHighlighterColors.BUILTIN_FUNCTION);
103-
break;
104-
case MODULE:
105-
setHighlighting(symbol, myHolder, MathematicaSyntaxHighlighterColors.MODULE_LOCALIZED);
106-
break;
107-
case BLOCK:
108-
setHighlighting(symbol, myHolder, MathematicaSyntaxHighlighterColors.BLOCK_LOCALIZED);
109-
break;
110-
case SETDELAYEDPATTERN:
111-
setHighlighting(symbol, myHolder, MathematicaSyntaxHighlighterColors.PATTERN);
112-
break;
113-
default:
114-
setHighlighting(symbol, myHolder, MathematicaSyntaxHighlighterColors.MODULE_LOCALIZED);
115-
break;
116-
}
87+
symbol.resolve();
88+
final MScope scope = symbol.getLocalizationConstruct();
89+
switch (scope) {
90+
case FILE:
91+
setHighlighting(symbol, myHolder, MathematicaSyntaxHighlighterColors.IDENTIFIER);
92+
break;
93+
case NULL:
94+
setHighlighting(symbol, myHolder, CodeInsightColors.WRONG_REFERENCES_ATTRIBUTES);
95+
break;
96+
case BUILT_IN:
97+
setHighlighting(symbol, myHolder, MathematicaSyntaxHighlighterColors.BUILTIN_FUNCTION);
98+
break;
99+
case MODULE:
100+
setHighlighting(symbol, myHolder, MathematicaSyntaxHighlighterColors.MODULE_LOCALIZED);
101+
break;
102+
case BLOCK:
103+
setHighlighting(symbol, myHolder, MathematicaSyntaxHighlighterColors.BLOCK_LOCALIZED);
104+
break;
105+
case SETDELAYEDPATTERN:
106+
setHighlighting(symbol, myHolder, MathematicaSyntaxHighlighterColors.PATTERN);
107+
break;
108+
default:
109+
setHighlighting(symbol, myHolder, MathematicaSyntaxHighlighterColors.MODULE_LOCALIZED);
110+
break;
117111
}
112+
118113
}
119114

120115
@Override

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,35 +40,36 @@ public interface FunctionCall extends PsiElement {
4040
*
4141
* @return true if it is a scoping construct.
4242
*/
43-
public boolean isScopingConstruct();
43+
boolean isScopingConstruct();
4444

45-
/**
46-
* Extracts the PsiElement which represents the Head of a function call.
47-
*
48-
* @return The head
49-
*/
50-
public PsiElement getHead();
45+
46+
/**
47+
* Extracts the PsiElement which represents the Head of a function call.
48+
*
49+
* @return The head
50+
*/
51+
PsiElement getHead();
5152

5253
/**
5354
* Tests whether the function call has a head the matches the argument.
5455
*
5556
* @param head The head which should be tested.
5657
* @return True, if head matches the Head of the function call.
5758
*/
58-
public boolean matchesHead(String head);
59+
boolean matchesHead(String head);
5960

6061

61-
public boolean hasHead(@NotNull final String otherHead);
62+
boolean hasHead(@NotNull final String otherHead);
6263

63-
public boolean hasHead(@NotNull final String[] otherHeads);
64+
boolean hasHead(@NotNull final String[] otherHeads);
6465

6566

6667
/**
6768
* Returns the type of scoping construct, if the function call is e.g. <code >Module[..]</code>
6869
*
6970
* @return The scoping construct or MScope.NULL if it is no scoping construct.
7071
*/
71-
public MScope getScopingConstruct();
72+
MScope getScopingConstruct();
7273

7374

7475
/**
@@ -79,4 +80,6 @@ public interface FunctionCall extends PsiElement {
7980
*/
8081
@Nullable
8182
PsiElement getArgument(int n);
83+
84+
PsiElement[] getArguments();
8285
}

src/de/halirutan/mathematica/lang/psi/api/Symbol.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.intellij.psi.PsiNameIdentifierOwner;
2626
import com.intellij.psi.PsiReference;
2727
import de.halirutan.mathematica.lang.psi.util.LocalizationConstruct;
28+
import de.halirutan.mathematica.lang.psi.util.LocalizationConstruct.MScope;
2829
import de.halirutan.mathematica.lang.resolve.SymbolResolveResult;
2930

3031
/**
@@ -60,9 +61,11 @@ public interface Symbol extends PsiNameIdentifierOwner, PsiReference {
6061
*
6162
* @return type of localization
6263
*/
63-
LocalizationConstruct.MScope getLocalizationConstruct();
64+
MScope getLocalizationConstruct();
6465

6566
PsiElement[] getElementsReferencingToMe();
6667

6768
SymbolResolveResult advancedResolve();
69+
70+
boolean isSelfReference();
6871
}

src/de/halirutan/mathematica/lang/psi/impl/CompoundExpressionImpl.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ public CompoundExpressionImpl(ASTNode node) {
4040

4141
@Override
4242
public boolean processDeclarations(@NotNull PsiScopeProcessor processor, @NotNull ResolveState state, PsiElement lastParent, @NotNull PsiElement place) {
43-
PsiElement children[] = getChildren();
44-
for (PsiElement child : children) {
45-
if (child.equals(lastParent)) {
46-
continue;
47-
}
48-
if (!child.processDeclarations(processor, state, this, place)) return false;
49-
}
43+
// PsiElement children[] = getChildren();
44+
// for (PsiElement child : children) {
45+
// if (child.equals(lastParent)) {
46+
// continue;
47+
// }
48+
// if (!child.processDeclarations(processor, state, this, place)) return false;
49+
// }
5050
return true;
5151
}
5252

src/de/halirutan/mathematica/lang/psi/impl/ExpressionImpl.java

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

2424
import com.intellij.extapi.psi.ASTWrapperPsiElement;
2525
import com.intellij.lang.ASTNode;
26-
import com.intellij.psi.PsiElement;
2726
import de.halirutan.mathematica.lang.psi.api.Expression;
2827
import org.jetbrains.annotations.NotNull;
2928

@@ -48,12 +47,12 @@ public String toString() {
4847

4948
@Override
5049
public void subtreeChanged() {
51-
final PsiElement[] children = getChildren();
52-
for (PsiElement child : children) {
53-
if (child instanceof ExpressionImpl) {
54-
((ExpressionImpl) child).subtreeChanged();
55-
}
56-
}
50+
// final PsiElement[] children = getChildren();
51+
// for (PsiElement child : children) {
52+
// if (child instanceof ExpressionImpl) {
53+
// ((ExpressionImpl) child).subtreeChanged();
54+
// }
55+
// }
5756

5857
}
5958

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

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -22,44 +22,44 @@
2222
package de.halirutan.mathematica.lang.psi.impl;
2323

2424
import com.intellij.lang.ASTNode;
25-
import com.intellij.openapi.util.Key;
2625
import com.intellij.psi.PsiElement;
2726
import com.intellij.psi.PsiElementVisitor;
2827
import com.intellij.psi.ResolveState;
2928
import com.intellij.psi.scope.PsiScopeProcessor;
3029
import de.halirutan.mathematica.lang.psi.MathematicaVisitor;
3130
import de.halirutan.mathematica.lang.psi.api.FunctionCall;
32-
import de.halirutan.mathematica.lang.psi.api.Symbol;
3331
import de.halirutan.mathematica.lang.psi.util.LocalizationConstruct;
3432
import de.halirutan.mathematica.lang.psi.util.LocalizationConstruct.MScope;
33+
import de.halirutan.mathematica.lang.resolve.processors.SymbolResolveHint;
3534
import org.jetbrains.annotations.NotNull;
3635

3736
public class FunctionCallImpl extends ExpressionImpl implements FunctionCall {
3837

39-
private final Key<Object> myScopeKey = Key.create("SCOPING_CONSTRUCT");
40-
private boolean myIsUpToDate;
38+
// private final Key<Object> myScopeKey = Key.create("SCOPING_CONSTRUCT");
39+
// private boolean myIsUpToDate;
4140
private String myHead;
41+
private MScope myLocalizationConstruct = MScope.NULL;
4242

4343

4444
public FunctionCallImpl(@NotNull ASTNode node) {
4545
super(node);
46-
myIsUpToDate = false;
46+
// myIsUpToDate = false;
4747
myHead = node.getFirstChildNode().getText();
48+
myLocalizationConstruct = LocalizationConstruct.getType(myHead);
4849
}
4950

5051
@Override
5152
public boolean processDeclarations(@NotNull PsiScopeProcessor processor, @NotNull ResolveState state, PsiElement lastParent, @NotNull PsiElement place) {
52-
final PsiElement head = getFirstChild();
53-
if (head instanceof Symbol) {
53+
if (isScopingConstruct()) {
5454
// In a tree-up-walk, we only consider declarations of Module, Block, .. when we come from inside this Module.
5555
// Therefore, we need to check whether our last position was inside and if not, we don't consider the declarations
5656
// of this.
57-
if (lastParent.getParent() != this) {
57+
if (lastParent == this.getHead() || lastParent.getParent() != this) {
5858
return true;
5959
}
60-
final String symbolName = ((Symbol) head).getSymbolName();
61-
cacheScopingConstruct(symbolName);
6260
if (isScopingConstruct()) {
61+
state = state.put(SymbolResolveHint.RESOLVE_CONTEXT, this);
62+
state = state.put(SymbolResolveHint.LAST_PARENT, lastParent);
6363
return processor.execute(this, state);
6464
}
6565
}
@@ -102,11 +102,16 @@ public PsiElement getArgument(int n) {
102102
}
103103

104104
@Override
105-
public void subtreeChanged() {
106-
super.subtreeChanged();
107-
myIsUpToDate = false;
105+
public PsiElement[] getArguments() {
106+
return getChildren();
108107
}
109108

109+
// @Override
110+
// public void subtreeChanged() {
111+
// super.subtreeChanged();
112+
// myIsUpToDate = false;
113+
// }
114+
110115
/**
111116
* Extracts the head of the function call and looks whether it is in the list {@link #SCOPING_CONSTRUCTS}. This can
112117
* lead to various false negatives. E.g. <code >(Block)[{..},..]</code> returns false, although after <em
@@ -118,35 +123,24 @@ public void subtreeChanged() {
118123

119124
@Override
120125
public boolean isScopingConstruct() {
121-
122-
if (!myIsUpToDate) {
123-
cacheScopingConstruct();
124-
}
125-
MScope type = (LocalizationConstruct.MScope) getUserData(myScopeKey);
126-
return type != null && !type.equals(MScope.NULL);
127-
}
128-
129-
public LocalizationConstruct.MScope getScopingConstruct() {
130-
if (!myIsUpToDate) {
131-
cacheScopingConstruct();
132-
}
133-
return (LocalizationConstruct.MScope) getUserData(myScopeKey);
126+
return myLocalizationConstruct != MScope.NULL;
134127
}
135128

136-
private void cacheScopingConstruct() {
137-
if (myIsUpToDate) return;
138-
PsiElement head = getFirstChild();
139-
if (head instanceof Symbol) {
140-
cacheScopingConstruct(((Symbol) head).getSymbolName());
141-
} else {
142-
putUserData(myScopeKey, MScope.NULL);
143-
}
144-
myIsUpToDate = true;
145-
}
146-
147-
private void cacheScopingConstruct(String functionName) {
148-
putUserData(myScopeKey, LocalizationConstruct.getType(functionName));
129+
@Override
130+
public MScope getScopingConstruct() {
131+
return myLocalizationConstruct;
149132
}
133+
//
134+
// private void cacheScopingConstruct() {
135+
// if (myIsUpToDate) return;
136+
// PsiElement head = getFirstChild();
137+
// if (head instanceof Symbol) {
138+
// cacheScopingConstruct(((Symbol) head).getSymbolName());
139+
// } else {
140+
// putUserData(myScopeKey, MScope.NULL);
141+
// }
142+
// myIsUpToDate = true;
143+
// }
150144

151145
@Override
152146
public void accept(@NotNull PsiElementVisitor visitor) {

src/de/halirutan/mathematica/lang/psi/impl/MathematicaPsiFileImpl.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ public FileType getFileType() {
4949

5050
@Override
5151
public boolean processDeclarations(@NotNull PsiScopeProcessor processor, @NotNull ResolveState state, PsiElement lastParent, @NotNull PsiElement place) {
52-
PsiElement children[] = getChildren();
53-
for (PsiElement child : children) {
54-
if (child.equals(lastParent)) {
55-
continue;
56-
}
57-
if (!child.processDeclarations(processor, state, this, place)) return false;
58-
}
52+
// PsiElement children[] = getChildren();
53+
// for (PsiElement child : children) {
54+
// if (child.equals(lastParent)) {
55+
// continue;
56+
// }
57+
// if (!child.processDeclarations(processor, state, this, place)) return false;
58+
// }
5959
return true;
6060
}
6161

0 commit comments

Comments
 (0)