Skip to content

Commit 3c4b135

Browse files
committed
Created an annotator that highlights Mathematica symbols that are not available in the used Mathematica Sdk version
1 parent 50a8950 commit 3c4b135

16 files changed

Lines changed: 545 additions & 59 deletions

resources/META-INF/plugin.xml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<id>de.halirutan.mathematica</id>
33
<name>Mathematica Support</name>
44
<category>Custom Language</category>
5-
<version>1.3.5</version>
5+
<version>2.0.0</version>
66
<idea-version since-build="135.1230"/>
77
<vendor email="patrick@halirutan.de" url="http://mathematicaplugin.halirutan.de">Patrick Scheibe</vendor>
88
<depends>com.intellij.modules.lang</depends>
@@ -77,12 +77,15 @@
7777

7878
<moduleType id="MATHEMATICA_MODULE"
7979
implementationClass="de.halirutan.mathematica.module.MathematicaModuleType"/>
80+
<moduleConfigurationEditorProvider
81+
implementation="de.halirutan.mathematica.module.MathematicaModuleConfigurationEditor"/>
82+
8083
<projectTemplatesFactory implementation="de.halirutan.mathematica.module.MathematicaProjectTemplatesFactory"/>
8184

8285

8386
<!-- Highlighting and annotating of code, StructureView and documentation lookup-->
8487

85-
<lang.syntaxHighlighterFactory key="Mathematica"
88+
<lang.syntaxHighlighterFactory language="Mathematica"
8689
implementationClass="de.halirutan.mathematica.codeinsight.highlighting.MathematicaSyntaxHighlighterFactory"/>
8790
<lang.psiStructureViewFactory language="Mathematica"
8891
implementationClass="de.halirutan.mathematica.codeinsight.structureview.MathematicaStructureViewFactory"/>
@@ -151,6 +154,7 @@
151154
<lang.formatter language="Mathematica"
152155
implementationClass="de.halirutan.mathematica.codeinsight.formatter.MathematicaFormattingModelBuilder"/>
153156

157+
154158
<additionalTextAttributes scheme="Darcula" file="colors/MathematicaDarcula.xml"/>
155159
<additionalTextAttributes scheme="Default" file="colors/MathematicaDefault.xml"/>
156160
<lang.refactoringSupport language="Mathematica"

resources/de/halirutan/mathematica/MathematicaBundle.properties

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@
2121

2222
language.name=Mathematica
2323
language.description=The Mathematica/Wolfram language
24+
language.level.11=Mathematica Version 11.0
25+
language.level.10.4=Mathematica Version 10.4
26+
language.level.10.3=Mathematica Version 10.3
27+
language.level.10.2=Mathematica Version 10.2
28+
language.level.10.1=Mathematica Version 10.1
29+
language.level.10=Mathematica Version 10.0
30+
language.level.9=Mathematica Version 9
31+
language.level.8=Mathematica Version 8
2432
module.type.description=Creates an empty project or module for custom layouts.
2533
module.type.name=Empty Module
2634
project.template.mathematica=Mathematica

resources/de/halirutan/mathematica/MathematicaInspectionBundle.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,5 @@ consistent.compound.expression.in.file.message=Missing semicolon
2828
bugs.implicit.times.through.linebreak.name=Multiplication through linebreak
2929
bugs.implicit.times.through.linebreak.description=This linebreak is interpreted as multiplication. When two complete expressions are separated by a linebreak, then Mathematica interprets this as a multiplication. This is probably not intended.
3030
bugs.implicit.times.through.linebreak.message=Missing comma or semicolon
31+
bugs.unsupported.version.name=Unsupported version for function
32+
bugs.unsupported.version.description=The function or symbol is part of a later version of Mathematica.\nYou language setting indicates that you are writing code for an earlier Mathematica version.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
*/
3333
public class SymbolInformationProvider {
3434

35-
private final static String ourSymbolInformationFile = "/de/halirutan/mathematica/codeinsight/completion/symbolInformationV10_3_1";
35+
private final static String ourSymbolInformationFile = "/de/halirutan/mathematica/codeinsight/completion/symbolInformationV11_0_1";
3636
private final static HashMap<String, SymbolInformation> ourSymbols;
3737

3838
private SymbolInformationProvider() {}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright (c) 2016 Patrick Scheibe
3+
* Permission is hereby granted, free of charge, to any person obtaining a copy
4+
* of this software and associated documentation files (the "Software"), to deal
5+
* in the Software without restriction, including without limitation the rights
6+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
* copies of the Software, and to permit persons to whom the Software is
8+
* furnished to do so, subject to the following conditions:
9+
*
10+
* The above copyright notice and this permission notice shall be included in
11+
* all copies or substantial portions of the Software.
12+
*
13+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
* THE SOFTWARE.
20+
*/
21+
22+
package de.halirutan.mathematica.codeinsight.completion;
23+
24+
import java.util.Enumeration;
25+
import java.util.HashMap;
26+
import java.util.ResourceBundle;
27+
28+
/**
29+
* Loads and extracts all information of Mathematica built-in symbols that are used for completion and intelligent
30+
* code insight.
31+
* @author hal (4/3/13)
32+
*/
33+
public class SymbolVersionProvider {
34+
35+
private final static String ourSymbolVersionFile = "/de/halirutan/mathematica/codeinsight/completion/symbolVersions";
36+
private final static HashMap<String, Double> ourSymbols;
37+
38+
private SymbolVersionProvider() {}
39+
40+
static {
41+
42+
ResourceBundle info = ResourceBundle.getBundle(ourSymbolVersionFile);
43+
ourSymbols = new HashMap<String, Double>(6000);
44+
45+
Enumeration<String> names = info.getKeys();
46+
while (names.hasMoreElements()) {
47+
String name = names.nextElement();
48+
double version = Double.parseDouble(info.getString(name));
49+
ourSymbols.put(name, version);
50+
}
51+
52+
}
53+
54+
public static HashMap<String, Double> getSymbolNames() {
55+
return ourSymbols;
56+
}
57+
58+
public static String getName(String nameWithContext) {
59+
String context = nameWithContext.split("`")[0] + "`";
60+
return nameWithContext.substring(context.length());
61+
}
62+
63+
}

src/de/halirutan/mathematica/codeinsight/inspections/MathematicaInspectionProvider.java

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

2424
import com.intellij.codeInspection.InspectionToolProvider;
2525
import de.halirutan.mathematica.codeinsight.inspections.bugs.ImplicitTimesThroughLinebreak;
26+
import de.halirutan.mathematica.codeinsight.inspections.bugs.UnsupportedVersion;
2627
import de.halirutan.mathematica.codeinsight.inspections.codestyle.ConsistentCompoundExpressionInFile;
2728

2829
/**
@@ -33,7 +34,8 @@ public class MathematicaInspectionProvider implements InspectionToolProvider {
3334
public Class[] getInspectionClasses() {
3435
return new Class[]{
3536
ConsistentCompoundExpressionInFile.class,
36-
ImplicitTimesThroughLinebreak.class
37+
ImplicitTimesThroughLinebreak.class,
38+
UnsupportedVersion.class
3739
};
3840

3941
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Copyright (c) 2016 Patrick Scheibe
3+
* Permission is hereby granted, free of charge, to any person obtaining a copy
4+
* of this software and associated documentation files (the "Software"), to deal
5+
* in the Software without restriction, including without limitation the rights
6+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
* copies of the Software, and to permit persons to whom the Software is
8+
* furnished to do so, subject to the following conditions:
9+
*
10+
* The above copyright notice and this permission notice shall be included in
11+
* all copies or substantial portions of the Software.
12+
*
13+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
* THE SOFTWARE.
20+
*/
21+
22+
package de.halirutan.mathematica.codeinsight.inspections.bugs;
23+
24+
import com.intellij.codeInspection.LocalInspectionToolSession;
25+
import com.intellij.codeInspection.ProblemsHolder;
26+
import com.intellij.openapi.projectRoots.Sdk;
27+
import com.intellij.openapi.roots.ProjectRootManager;
28+
import com.intellij.openapi.util.TextRange;
29+
import com.intellij.psi.PsiElement;
30+
import com.intellij.psi.PsiElementVisitor;
31+
import de.halirutan.mathematica.codeinsight.completion.SymbolVersionProvider;
32+
import de.halirutan.mathematica.codeinsight.inspections.AbstractInspection;
33+
import de.halirutan.mathematica.codeinsight.inspections.MathematicaInspectionBundle;
34+
import de.halirutan.mathematica.filetypes.MathematicaFileType;
35+
import de.halirutan.mathematica.module.MathematicaLanguageLevel;
36+
import de.halirutan.mathematica.parsing.psi.MathematicaVisitor;
37+
import de.halirutan.mathematica.parsing.psi.api.Symbol;
38+
import de.halirutan.mathematica.sdk.MathematicaSdkType;
39+
import org.jetbrains.annotations.Nls;
40+
import org.jetbrains.annotations.NotNull;
41+
import org.jetbrains.annotations.Nullable;
42+
43+
import java.util.HashMap;
44+
45+
/**
46+
* Provides warnings when you are using Mathematica symbols that are introduces later then the version you are using.
47+
*
48+
* @author halirutan
49+
*/
50+
public class UnsupportedVersion extends AbstractInspection {
51+
52+
@Nls
53+
@NotNull
54+
@Override
55+
public String getDisplayName() {
56+
return MathematicaInspectionBundle.message("bugs.unsupported.version.name");
57+
}
58+
59+
@Nullable
60+
@Override
61+
public String getStaticDescription() {
62+
return MathematicaInspectionBundle.message("bugs.unsupported.version.description");
63+
}
64+
65+
@Nls
66+
@NotNull
67+
@Override
68+
public String getGroupDisplayName() {
69+
return MathematicaInspectionBundle.message("group.bugs");
70+
}
71+
72+
@NotNull
73+
@Override
74+
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly, @NotNull LocalInspectionToolSession session) {
75+
if(session.getFile().getFileType() instanceof MathematicaFileType) {
76+
return new WrongVersionVisitor(holder);
77+
} else return PsiElementVisitor.EMPTY_VISITOR;
78+
}
79+
80+
81+
82+
private static class WrongVersionVisitor extends MathematicaVisitor {
83+
84+
85+
private HashMap<String, Double> mySymbolVersions = SymbolVersionProvider.getSymbolNames();
86+
private MathematicaLanguageLevel myLanguageLevel = MathematicaLanguageLevel.HIGHEST;
87+
private final ProblemsHolder myHolder;
88+
89+
WrongVersionVisitor(final ProblemsHolder holder) {
90+
this.myHolder = holder;
91+
final Sdk projectSdk = ProjectRootManager.getInstance(myHolder.getProject()).getProjectSdk();
92+
if (projectSdk != null && projectSdk.getSdkType() instanceof MathematicaSdkType) {
93+
myLanguageLevel = MathematicaLanguageLevel.createFromSdk(projectSdk);
94+
}
95+
96+
}
97+
98+
private void registerProblem(final PsiElement element, final String message) {
99+
myHolder.registerProblem(
100+
element,
101+
TextRange.from(0,element.getTextLength()),
102+
message);
103+
}
104+
105+
@Override
106+
public void visitSymbol(Symbol symbol) {
107+
final String symbolName = symbol.getSymbolName();
108+
if (Character.isLowerCase(symbolName.charAt(0))) {
109+
return;
110+
}
111+
112+
String nameWithContext = symbol.getMathematicaContext() + symbolName;
113+
if (mySymbolVersions.containsKey(nameWithContext)) {
114+
double version = mySymbolVersions.get(nameWithContext);
115+
if (version > myLanguageLevel.getVersionNumber()) {
116+
registerProblem(symbol, "Mathematica " + version + " required. Your project SDK is " + myLanguageLevel.getPresentableText());
117+
}
118+
}
119+
120+
121+
}
122+
}
123+
}
124+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (c) 2016 Patrick Scheibe
3+
* Permission is hereby granted, free of charge, to any person obtaining a copy
4+
* of this software and associated documentation files (the "Software"), to deal
5+
* in the Software without restriction, including without limitation the rights
6+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
* copies of the Software, and to permit persons to whom the Software is
8+
* furnished to do so, subject to the following conditions:
9+
*
10+
* The above copyright notice and this permission notice shall be included in
11+
* all copies or substantial portions of the Software.
12+
*
13+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
* THE SOFTWARE.
20+
*/
21+
22+
package de.halirutan.mathematica.module;
23+
24+
import com.intellij.openapi.module.Module;
25+
import com.intellij.openapi.roots.ContentEntry;
26+
import com.intellij.openapi.roots.ModifiableRootModel;
27+
import com.intellij.openapi.roots.ui.configuration.CommonContentEntriesEditor;
28+
import com.intellij.openapi.roots.ui.configuration.ModuleConfigurationState;
29+
import org.jetbrains.jps.model.java.JavaResourceRootType;
30+
import org.jetbrains.jps.model.java.JavaSourceRootType;
31+
32+
import javax.swing.*;
33+
import java.awt.*;
34+
35+
/**
36+
* @author patrick (21.11.16).
37+
*/
38+
public class MathematicaGeneralModuleSettingsEditor extends CommonContentEntriesEditor {
39+
public MathematicaGeneralModuleSettingsEditor(String moduleName, ModuleConfigurationState state) {
40+
super(moduleName, state, JavaSourceRootType.SOURCE,JavaSourceRootType.TEST_SOURCE, JavaResourceRootType.RESOURCE);
41+
}
42+
43+
@Override
44+
protected void addContentEntryPanels(ContentEntry[] contentEntriesArray) {
45+
super.addContentEntryPanels(contentEntriesArray);
46+
}
47+
48+
@Override
49+
protected void addAdditionalSettingsToPanel(JPanel mainPanel) {
50+
final Module module = getModule();
51+
final ModifiableRootModel model = getModel();
52+
mainPanel.add(new MathematicaLanguageLevelCombo(model), BorderLayout.NORTH);
53+
54+
}
55+
}

0 commit comments

Comments
 (0)