Skip to content

Commit 437c818

Browse files
committed
Reworked symbol names and contexts so that it is now more consistent.
Fixes #52
1 parent 06f58d6 commit 437c818

10 files changed

Lines changed: 77 additions & 76 deletions

File tree

resources/META-INF/plugin.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<name>Mathematica Support</name>
44
<category>Custom Language</category>
55
<version>2.0.14</version>
6-
<idea-version since-build="163.7743.44"/>
6+
<idea-version since-build="163"/>
77
<vendor email="patrick@halirutan.de" url="http://mathematicaplugin.halirutan.de">Patrick Scheibe</vendor>
88
<depends>com.intellij.modules.lang</depends>
99
<description><![CDATA[

src/de/halirutan/mathematica/codeinsight/completion/GlobalDefinitionCompletionProvider.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@
3737
/**
3838
* @author patrick (9/27/13)
3939
*/
40-
public class GlobalDefinitionCompletionProvider extends MathematicaVisitor {
40+
class GlobalDefinitionCompletionProvider extends MathematicaVisitor {
4141

4242
private final Set<String> myCollectedFunctionNames;
4343

4444
public GlobalDefinitionCompletionProvider() {
45-
myCollectedFunctionNames = new HashSet<String>();
45+
myCollectedFunctionNames = new HashSet<>();
4646
}
4747

4848
public void visitFile(PsiFile file) {
@@ -64,14 +64,14 @@ public void visitSet(de.halirutan.mathematica.parsing.psi.api.assignment.Set set
6464
public void visitTagSet(TagSet tagSet) {
6565
final PsiElement tag = tagSet.getFirstChild();
6666
if (tag instanceof Symbol) {
67-
myCollectedFunctionNames.add(((Symbol) tag).getSymbolName());
67+
myCollectedFunctionNames.add(((Symbol) tag).getFullSymbolName());
6868
}
6969
}
7070

7171
public void visitTagSetDelayed(TagSetDelayed tagSetDelayed) {
7272
final PsiElement tag = tagSetDelayed.getFirstChild();
7373
if (tag instanceof Symbol) {
74-
myCollectedFunctionNames.add(((Symbol) tag).getSymbolName());
74+
myCollectedFunctionNames.add(((Symbol) tag).getFullSymbolName());
7575
}
7676
}
7777

@@ -98,7 +98,7 @@ public void visitFunctionCall(final FunctionCall functionCall) {
9898
} else if (head.matches("TagSet|TagSetDelayed|SetAttributes|SetOptions")) {
9999
final PsiElement arg1 = functionCall.getArgument(1);
100100
if (arg1 instanceof Symbol) {
101-
myCollectedFunctionNames.add(((Symbol) arg1).getSymbolName());
101+
myCollectedFunctionNames.add(((Symbol) arg1).getFullSymbolName());
102102
}
103103
}
104104
}
@@ -124,7 +124,7 @@ private void cacheFromUpSetAssignment(final PsiElement upSet) {
124124
private void cacheAssignedSymbols(Set<Symbol> symbolSet) {
125125
if (symbolSet != null) {
126126
for (Symbol symbol : symbolSet) {
127-
myCollectedFunctionNames.add(symbol.getSymbolName());
127+
myCollectedFunctionNames.add(symbol.getFullSymbolName());
128128
}
129129
}
130130
}

src/de/halirutan/mathematica/codeinsight/completion/LocalDefinitionCompletionProvider.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import de.halirutan.mathematica.parsing.psi.api.rules.RuleDelayed;
3535
import de.halirutan.mathematica.parsing.psi.impl.SymbolPsiReference;
3636
import de.halirutan.mathematica.parsing.psi.util.LocalizationConstruct;
37+
import de.halirutan.mathematica.parsing.psi.util.LocalizationConstruct.ConstructType;
3738
import de.halirutan.mathematica.parsing.psi.util.MathematicaPatternVisitor;
3839
import de.halirutan.mathematica.parsing.psi.util.MathematicaPsiUtilities;
3940
import org.jetbrains.annotations.NotNull;
@@ -49,18 +50,18 @@
4950
*
5051
* @author patrick (5/22/13)
5152
*/
52-
public class LocalDefinitionCompletionProvider extends BaseScopeProcessor {
53+
class LocalDefinitionCompletionProvider extends BaseScopeProcessor {
5354

5455
private final List<Symbol> mySymbols = Lists.newLinkedList();
5556
private final Symbol myStartElement;
5657

57-
public LocalDefinitionCompletionProvider(Symbol myStartElement) {
58+
public LocalDefinitionCompletionProvider(Symbol startElement) {
5859
super();
59-
this.myStartElement = myStartElement;
60+
this.myStartElement = startElement;
6061
}
6162

6263
@Override
63-
public boolean execute(@NotNull PsiElement element, ResolveState state) {
64+
public boolean execute(@NotNull PsiElement element, @NotNull ResolveState state) {
6465
if (element instanceof Set || element instanceof SetDelayed || element instanceof TagSetDelayed || element instanceof TagSet) {
6566
MathematicaPatternVisitor patternVisitor = new MathematicaPatternVisitor();
6667
element.accept(patternVisitor);
@@ -70,7 +71,7 @@ public boolean execute(@NotNull PsiElement element, ResolveState state) {
7071
final FunctionCall functionCall = (FunctionCall) element;
7172
if (functionCall.isScopingConstruct()) {
7273
List<Symbol> vars = Lists.newArrayList();
73-
final LocalizationConstruct.ConstructType scopingConstruct = functionCall.getScopingConstruct();
74+
final ConstructType scopingConstruct = functionCall.getScopingConstruct();
7475

7576
if (LocalizationConstruct.isFunctionLike(scopingConstruct)) {
7677
vars = MathematicaPsiUtilities.getLocalFunctionVariables(functionCall);
@@ -116,13 +117,13 @@ public boolean execute(@NotNull PsiElement element, ResolveState state) {
116117
*/
117118
public List<Symbol> getSymbols() {
118119

119-
Collections.sort(mySymbols, new SymbolComparator());
120-
Pattern pattern = Pattern.compile(myStartElement.getSymbolName().substring(0, 1) + ".*");
120+
mySymbols.sort(new SymbolComparator());
121+
Pattern pattern = Pattern.compile(myStartElement.getFullSymbolName().substring(0, 1) + ".*");
121122
Symbol tmp = null;
122123
for (Iterator<Symbol> symbolIterator = mySymbols.iterator(); symbolIterator.hasNext(); ) {
123124
Symbol next = symbolIterator.next();
124125

125-
if (!pattern.matcher(next.getSymbolName()).matches()) {
126+
if (!pattern.matcher(next.getFullSymbolName()).matches()) {
126127
symbolIterator.remove();
127128
continue;
128129
}
@@ -132,7 +133,7 @@ public List<Symbol> getSymbols() {
132133
continue;
133134
}
134135

135-
if (tmp.getSymbolName().equals(next.getSymbolName())) {
136+
if (tmp.getFullSymbolName().equals(next.getFullSymbolName())) {
136137
symbolIterator.remove();
137138
} else {
138139
tmp = next;
@@ -148,7 +149,7 @@ private class SymbolComparator implements Comparator<Symbol> {
148149

149150
@Override
150151
public int compare(Symbol o1, Symbol o2) {
151-
return (o1.getSymbolName().compareTo(o2.getSymbolName()));
152+
return (o1.getFullSymbolName().compareTo(o2.getFullSymbolName()));
152153
}
153154

154155
}

src/de/halirutan/mathematica/codeinsight/completion/VariableNameCompletion.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import com.intellij.codeInsight.completion.*;
2626
import com.intellij.codeInsight.lookup.LookupElementBuilder;
2727
import com.intellij.patterns.PlatformPatterns;
28-
import com.intellij.patterns.PsiElementPattern;
28+
import com.intellij.patterns.PsiElementPattern.Capture;
2929
import com.intellij.psi.PsiElement;
3030
import com.intellij.psi.PsiFile;
3131
import com.intellij.psi.PsiRecursiveElementVisitor;
@@ -34,6 +34,7 @@
3434
import com.intellij.util.ProcessingContext;
3535
import com.intellij.util.containers.hash.HashSet;
3636
import de.halirutan.mathematica.parsing.psi.api.Symbol;
37+
import de.halirutan.mathematica.parsing.psi.impl.SymbolPsiReference;
3738
import org.jetbrains.annotations.NotNull;
3839

3940
import java.util.List;
@@ -51,14 +52,14 @@ public class VariableNameCompletion extends MathematicaCompletionProvider {
5152

5253
@Override
5354
void addTo(CompletionContributor contributor) {
54-
final PsiElementPattern.Capture<PsiElement> symbolPattern = PlatformPatterns.psiElement().withParent(Symbol.class);
55+
final Capture<PsiElement> symbolPattern = PlatformPatterns.psiElement().withParent(Symbol.class);
5556
contributor.extend(CompletionType.BASIC, symbolPattern, this);
5657
}
5758

5859
@Override
5960
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) {
6061
final Symbol callingSymbol = (Symbol) parameters.getPosition().getParent();
61-
final String callingSymbolName = callingSymbol.getSymbolName();
62+
final String callingSymbolName = callingSymbol.getFullSymbolName();
6263

6364
if (!parameters.isExtendedCompletion()) {
6465
final PsiFile containingFile = parameters.getOriginalFile();
@@ -67,7 +68,8 @@ protected void addCompletions(@NotNull CompletionParameters parameters, Processi
6768
GlobalDefinitionCompletionProvider visitor = new GlobalDefinitionCompletionProvider();
6869
containingFile.accept(visitor);
6970
for (String name : visitor.getFunctionsNames()) {
70-
if (!NAMES.contains(name)) {
71+
String possibleBuiltIn = name.contains("`") ? name : "Symbol`" + name;
72+
if (!NAMES.contains(possibleBuiltIn)) {
7173
result.addElement(PrioritizedLookupElement.withPriority(LookupElementBuilder.create(name), GLOBAL_VARIABLE_PRIORITY));
7274
}
7375
}
@@ -80,17 +82,17 @@ protected void addCompletions(@NotNull CompletionParameters parameters, Processi
8082

8183

8284
for (Symbol currentSymbol : variants) {
83-
if (!NAMES.contains(currentSymbol.getSymbolName())) {
85+
if (!SymbolPsiReference.isBuiltInSymbol(currentSymbol)) {
8486
result.addElement(PrioritizedLookupElement.withPriority(LookupElementBuilder.create(currentSymbol), LOCAL_VARIABLE_PRIORITY));
8587
}
8688
}
8789
} else {
88-
final Set<String> allSymbols = new HashSet<String>();
90+
final Set<String> allSymbols = new HashSet<>();
8991
PsiRecursiveElementVisitor visitor = new PsiRecursiveElementVisitor() {
9092
@Override
9193
public void visitElement(PsiElement element) {
92-
if (element instanceof Symbol && !callingSymbolName.equals(((Symbol) element).getSymbolName() + "ZZZ")) {
93-
allSymbols.add(((Symbol) element).getSymbolName());
94+
if (element instanceof Symbol && !callingSymbolName.equals(((Symbol) element).getFullSymbolName() + "ZZZ")) {
95+
allSymbols.add(((Symbol) element).getFullSymbolName());
9496
}
9597
element.acceptChildren(this);
9698
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ public void annotate(@NotNull PsiElement element, @NotNull final AnnotationHolde
8686

8787
@Override
8888
public void visitSymbol(final Symbol symbol) {
89-
if (NAMES.contains(symbol.getMathematicaContext()+symbol.getSymbolName())) {
89+
String possibleGlobalSymbol = symbol.getMathematicaContext().equals("") ?
90+
"System`"+symbol.getSymbolName() : symbol.getFullSymbolName();
91+
if (NAMES.contains(possibleGlobalSymbol)) {
9092
setHighlighting(symbol, myHolder, MathematicaSyntaxHighlighterColors.BUILTIN_FUNCTION);
9193
return;
9294
}

src/de/halirutan/mathematica/codeinsight/inspections/bugs/UnsupportedVersion.java

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public JComponent createOptionsPanel() {
8383
final JCheckBox useSDKCheckbox = new JCheckBox("Use Project SDK Language Level");
8484
final JLabel infoLabel = new JLabel();
8585
//noinspection Since15
86-
final ComboBox<MathematicaLanguageLevel> versionComboBox = new ComboBox<MathematicaLanguageLevel>();
86+
final ComboBox<MathematicaLanguageLevel> versionComboBox = new ComboBox<>();
8787

8888
for (MathematicaLanguageLevel level : MathematicaLanguageLevel.values()) {
8989
//noinspection unchecked
@@ -92,28 +92,22 @@ public JComponent createOptionsPanel() {
9292
versionComboBox.setSelectedItem(languageLevel);
9393
versionComboBox.setEditable(false);
9494
//noinspection unchecked
95-
versionComboBox.addActionListener(new ActionListener() {
96-
@Override
97-
public void actionPerformed(ActionEvent e) {
98-
final MathematicaLanguageLevel selectedItem = (MathematicaLanguageLevel) versionComboBox.getSelectedItem();
99-
if (selectedItem != null) {
100-
languageLevel = selectedItem;
101-
setLabelTextToVersion(infoLabel);
102-
}
95+
versionComboBox.addActionListener(e -> {
96+
final MathematicaLanguageLevel selectedItem = (MathematicaLanguageLevel) versionComboBox.getSelectedItem();
97+
if (selectedItem != null) {
98+
languageLevel = selectedItem;
99+
setLabelTextToVersion(infoLabel);
103100
}
104101
});
105102

106103
useSDKCheckbox.setSelected(useSDKLanguageLevelOrHighest);
107-
useSDKCheckbox.addActionListener(new ActionListener() {
108-
@Override
109-
public void actionPerformed(ActionEvent e) {
110-
useSDKLanguageLevelOrHighest = useSDKCheckbox.isSelected();
111-
versionComboBox.setVisible(!useSDKLanguageLevelOrHighest);
112-
if (!useSDKLanguageLevelOrHighest) {
113-
languageLevel = (MathematicaLanguageLevel) versionComboBox.getSelectedItem();
114-
}
115-
setLabelTextToVersion(infoLabel);
104+
useSDKCheckbox.addActionListener(e -> {
105+
useSDKLanguageLevelOrHighest = useSDKCheckbox.isSelected();
106+
versionComboBox.setVisible(!useSDKLanguageLevelOrHighest);
107+
if (!useSDKLanguageLevelOrHighest) {
108+
languageLevel = (MathematicaLanguageLevel) versionComboBox.getSelectedItem();
116109
}
110+
setLabelTextToVersion(infoLabel);
117111
});
118112

119113
setLabelTextToVersion(infoLabel);
@@ -211,7 +205,9 @@ public void visitSymbol(Symbol symbol) {
211205
return;
212206
}
213207

214-
String nameWithContext = symbol.getMathematicaContext() + symbolName;
208+
String nameWithContext = symbol.getMathematicaContext().equals("") ?
209+
"System`" + symbol.getSymbolName() : symbol.getFullSymbolName();
210+
215211
if (mySymbolVersions.containsKey(nameWithContext)) {
216212
double version = mySymbolVersions.get(nameWithContext);
217213
if (version > myLanguageLevel.getVersionNumber()) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
*/
5151
public class SymbolImpl extends ExpressionImpl implements Symbol {
5252

53-
private final HashSet<Symbol> myReferringElements = new HashSet<Symbol>();
53+
private final HashSet<Symbol> myReferringElements = new HashSet<>();
5454
private boolean myIsUpToDate;
5555
private ConstructType myLocalization;
5656
private Symbol myDefinitionElement;
@@ -84,7 +84,7 @@ public String getName() {
8484
@Override
8585
public String getMathematicaContext() {
8686
String myName = getName();
87-
String context = "System`";
87+
String context = "";
8888
if (myName != null) {
8989
if (myName.contains("`")) {
9090
context = myName.substring(0, myName.lastIndexOf('`') + 1);

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

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ public class SymbolPsiReference extends CachingReference implements PsiReference
6363
@Override
6464
public PsiElement resolveInner() {
6565

66-
if (NAMES.contains(myVariable.getSymbolName())) return myVariable;
66+
if (isBuiltInSymbol(myVariable)) return myVariable;
6767

6868
if (myVariable.cachedResolve()) {
69-
if (myVariable.getSymbolName().equals(myVariable.getResolveElement().getSymbolName()) ||
69+
if (myVariable.getFullSymbolName().equals(myVariable.getResolveElement().getFullSymbolName()) ||
7070
myVariable.getLocalizationConstruct().equals(ConstructType.ANONYMOUSFUNCTION)) {
7171
return myVariable.getResolveElement();
7272
} else {
@@ -109,7 +109,7 @@ public TextRange getRangeInElement() {
109109
@NotNull
110110
@Override
111111
public String getCanonicalText() {
112-
return myVariable.getMathematicaContext() + myVariable.getSymbolName();
112+
return myVariable.getFullSymbolName();
113113
}
114114

115115
@Override
@@ -125,17 +125,6 @@ public PsiElement bindToElement(@NotNull PsiElement element) throws IncorrectOpe
125125
return handleElementRename(element.getText());
126126
}
127127

128-
@Override
129-
public boolean isReferenceTo(PsiElement element) {
130-
if (NAMES.contains(myVariable.getSymbolName())) {
131-
return false;
132-
}
133-
if (element instanceof Symbol && ((Symbol) element).getSymbolName().equals(myVariable.getSymbolName())) {
134-
return super.isReferenceTo(element);
135-
}
136-
return false;
137-
}
138-
139128
@Override
140129
public boolean isSoft() {
141130
return super.isSoft();
@@ -147,4 +136,14 @@ public Object[] getVariants() {
147136
return new Object[0];
148137
}
149138

139+
public static boolean isBuiltInSymbol(PsiElement element) {
140+
if(element instanceof Symbol) {
141+
Symbol symbol = (Symbol) element;
142+
final String name = symbol.getMathematicaContext().equals("") ?
143+
"System`" + symbol.getSymbolName() : symbol.getFullSymbolName();
144+
return NAMES.contains(name);
145+
}
146+
return false;
147+
}
148+
150149
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public boolean execute(@NotNull PsiElement element) {
8080
* @return true if the names are equal
8181
*/
8282
private boolean visitSymbol(final Symbol symbol) {
83-
if (myStartElement.getSymbolName().equals(symbol.getSymbolName())) {
83+
if (myStartElement.getFullSymbolName().equals(symbol.getFullSymbolName())) {
8484
myReferringSymbol = symbol;
8585
return false;
8686
}
@@ -94,7 +94,7 @@ private boolean visitUpSetDefinition(final PsiElement lhs) {
9494
lhs.accept(definitionVisitor);
9595
final java.util.Set<Symbol> definitionSymbols = definitionVisitor.getUnboundSymbols();
9696
for (Symbol next : definitionSymbols) {
97-
if (next.getSymbolName().equals(myStartElement.getSymbolName())) {
97+
if (next.getFullSymbolName().equals(myStartElement.getFullSymbolName())) {
9898
myReferringSymbol = next;
9999
return false;
100100
}
@@ -107,7 +107,7 @@ private boolean visitUpSetDefinition(final PsiElement lhs) {
107107
* TagSet should be trivial. In f /: g[a,b,..,f,..] = .., f is always expected to be a symbol.
108108
*/
109109
private boolean visitTagSetDefinition(final PsiElement defSymbol) {
110-
if (defSymbol instanceof Symbol && ((Symbol) defSymbol).getSymbolName().matches(myStartElement.getSymbolName())) {
110+
if (defSymbol instanceof Symbol && ((Symbol) defSymbol).getFullSymbolName().matches(myStartElement.getFullSymbolName())) {
111111
myReferringSymbol = defSymbol;
112112
return false;
113113
}
@@ -120,7 +120,7 @@ private boolean visitSetDefinition(final PsiElement lhs) {
120120
lhs.accept(definitionVisitor);
121121
final java.util.Set<Symbol> definitionSymbols = definitionVisitor.getUnboundSymbols();
122122
for (Symbol next : definitionSymbols) {
123-
if (next.getSymbolName().equals(myStartElement.getSymbolName())) {
123+
if (next.getFullSymbolName().equals(myStartElement.getFullSymbolName())) {
124124
myReferringSymbol = next;
125125
return false;
126126
}

0 commit comments

Comments
 (0)