Skip to content

Commit cd73010

Browse files
committed
First implementation
1 parent 3da9d82 commit cd73010

18 files changed

Lines changed: 498 additions & 66 deletions

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ task wrapper(type: Wrapper) {
7878
version '3.0pre5'
7979

8080
intellij {
81-
version = '2017.3'
81+
version = '2017.3.1'
8282
downloadSources = true
8383
pluginName = 'Mathematica-IntelliJ-Plugin'
8484
updateSinceUntilBuild = false

config/mit.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) $today.year Patrick Scheibe
2+
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 all
11+
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 THE
19+
SOFTWARE.

resources/META-INF/plugin.xml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,8 @@
3939

4040
<sdkType id="Mathematica Sdk" implementation="de.halirutan.mathematica.sdk.MathematicaSdkType"/>
4141
<moduleType id="MATHEMATICA_MODULE" implementationClass="de.halirutan.mathematica.module.MathematicaModuleType"/>
42-
<!--<moduleBuilder builderClass="de.halirutan.mathematica.module.MathematicaEmptyModule"/>-->
43-
<!--<moduleBuilder builderClass="de.halirutan.mathematica.module.MathematicaBasicModule"/>-->
44-
<!--<moduleBuilder builderClass="de.halirutan.mathematica.module.MathematicaApplicationModule"/>-->
45-
<moduleConfigurationEditorProvider implementation="de.halirutan.mathematica.module.MathematicaModuleConfigurationEditor"/>
42+
<moduleExtension implementation="de.halirutan.mathematica.module.MathematicaLanguageLevelModuleExtensionImpl"/>
43+
<moduleConfigurationEditorProvider implementation="de.halirutan.mathematica.module.ui.MathematicaModuleConfigurationEditor"/>
4644
<projectTemplatesFactory implementation="de.halirutan.mathematica.module.MathematicaProjectTemplatesFactory"/>
4745

4846
<lang.syntaxHighlighterFactory language="Mathematica" implementationClass="de.halirutan.mathematica.codeinsight.highlighting.MathematicaSyntaxHighlighterFactory"/>

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ import de.halirutan.mathematica.codeinsight.inspections.symbol.UnresolvedSymbolI
3131
* @author patrick (7/8/14)
3232
*/
3333
class MathematicaInspectionProvider : InspectionToolProvider {
34-
override fun getInspectionClasses(): Array<Class<*>> {
35-
return arrayOf(
36-
UnresolvedSymbolInspection::class.java,
37-
ConsistentCompoundExpressionInFile::class.java,
38-
ImplicitTimesThroughLinebreak::class.java,
39-
UnsupportedVersion::class.java
40-
)
41-
}
34+
override fun getInspectionClasses(): Array<Class<*>> {
35+
return arrayOf(
36+
UnresolvedSymbolInspection::class.java,
37+
ConsistentCompoundExpressionInFile::class.java,
38+
ImplicitTimesThroughLinebreak::class.java,
39+
UnsupportedVersion::class.java
40+
)
41+
}
4242
}

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

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,20 @@
2626
import com.intellij.codeHighlighting.HighlightDisplayLevel;
2727
import com.intellij.codeInspection.LocalInspectionToolSession;
2828
import com.intellij.codeInspection.ProblemsHolder;
29+
import com.intellij.openapi.module.Module;
30+
import com.intellij.openapi.module.ModuleManager;
31+
import com.intellij.openapi.module.ModuleType;
2932
import com.intellij.openapi.projectRoots.Sdk;
33+
import com.intellij.openapi.roots.ModuleFileIndex;
34+
import com.intellij.openapi.roots.ModuleRootManager;
3035
import com.intellij.openapi.roots.ProjectRootManager;
3136
import com.intellij.openapi.ui.ComboBox;
3237
import com.intellij.openapi.ui.VerticalFlowLayout;
3338
import com.intellij.openapi.util.TextRange;
39+
import com.intellij.openapi.vfs.VirtualFile;
3440
import com.intellij.psi.PsiElement;
3541
import com.intellij.psi.PsiElementVisitor;
42+
import com.intellij.psi.PsiFile;
3643
import de.halirutan.mathematica.codeinsight.completion.util.SymbolVersionProvider;
3744
import de.halirutan.mathematica.codeinsight.inspections.AbstractInspection;
3845
import de.halirutan.mathematica.file.MathematicaFileType;
@@ -41,6 +48,8 @@
4148
import de.halirutan.mathematica.lang.psi.api.Symbol;
4249
import de.halirutan.mathematica.lang.psi.api.lists.Association;
4350
import de.halirutan.mathematica.lang.psi.impl.LightBuiltInSymbol;
51+
import de.halirutan.mathematica.module.MathematicaLanguageLevelModuleExtensionImpl;
52+
import de.halirutan.mathematica.module.MathematicaModuleType;
4453
import de.halirutan.mathematica.sdk.MathematicaLanguageLevel;
4554
import de.halirutan.mathematica.sdk.MathematicaSdkType;
4655
import org.jetbrains.annotations.Nls;
@@ -64,6 +73,7 @@ public class UnsupportedVersion extends AbstractInspection {
6473

6574
@SuppressWarnings({"InstanceVariableNamingConvention", "WeakerAccess"})
6675
public boolean useSDKLanguageLevelOrHighest = true;
76+
public boolean useModuleLanguageLevelOrHighest = true;
6777

6878
/**
6979
* Sets the correct text for the info label in the inspection settings page
@@ -82,6 +92,7 @@ private void setLabelTextToVersion(JLabel label) {
8292
@Override
8393
public JComponent createOptionsPanel() {
8494
final JPanel mainPanel = new JPanel(new VerticalFlowLayout(VerticalFlowLayout.TOP));
95+
final JCheckBox useModuleCheckbox = new JCheckBox("Language Level of Module has priority");
8596
final JCheckBox useSDKCheckbox = new JCheckBox("Use Project SDK Language Level");
8697
final JLabel infoLabel = new JLabel();
8798
//noinspection Since15
@@ -102,6 +113,11 @@ public JComponent createOptionsPanel() {
102113
}
103114
});
104115

116+
useModuleCheckbox.setSelected(useModuleLanguageLevelOrHighest);
117+
useModuleCheckbox.addActionListener(e ->
118+
useModuleLanguageLevelOrHighest = useModuleCheckbox.isSelected()
119+
);
120+
105121
useSDKCheckbox.setSelected(useSDKLanguageLevelOrHighest);
106122
useSDKCheckbox.addActionListener(e -> {
107123
useSDKLanguageLevelOrHighest = useSDKCheckbox.isSelected();
@@ -114,7 +130,7 @@ public JComponent createOptionsPanel() {
114130

115131
setLabelTextToVersion(infoLabel);
116132
versionComboBox.setVisible(!useSDKLanguageLevelOrHighest);
117-
133+
mainPanel.add(useModuleCheckbox);
118134
mainPanel.add(infoLabel);
119135
mainPanel.add(useSDKCheckbox);
120136
mainPanel.add(versionComboBox);
@@ -152,6 +168,27 @@ public HighlightDisplayLevel getDefaultLevel() {
152168
@Override
153169
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly, @NotNull LocalInspectionToolSession session) {
154170
if (session.getFile().getFileType() instanceof MathematicaFileType) {
171+
172+
// TODO: There must be a simpler way to find the module for a source file
173+
if (useModuleLanguageLevelOrHighest) {
174+
final PsiFile file = session.getFile();
175+
final ModuleManager instance = ModuleManager.getInstance(file.getProject());
176+
for (Module module : instance.getModules()) {
177+
if (ModuleType.is(module, MathematicaModuleType.getInstance())) {
178+
final ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module);
179+
final ModuleFileIndex fileIndex = moduleRootManager.getFileIndex();
180+
final VirtualFile virtualFile = file.getVirtualFile();
181+
if (fileIndex.isInContent(virtualFile)) {
182+
final MathematicaLanguageLevelModuleExtensionImpl languageLevelModuleExtension =
183+
moduleRootManager.getModuleExtension(MathematicaLanguageLevelModuleExtensionImpl.class);
184+
if (languageLevelModuleExtension.getMathematicaLanguageLevel() != null) {
185+
return new WrongVersionVisitor(holder, languageLevelModuleExtension.getMathematicaLanguageLevel());
186+
}
187+
}
188+
}
189+
}
190+
}
191+
155192
if (useSDKLanguageLevelOrHighest) {
156193
final ProjectRootManager manager = ProjectRootManager.getInstance(holder.getProject());
157194
final Sdk projectSdk = manager.getProjectSdk();
@@ -170,12 +207,12 @@ public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, fina
170207
private static class WrongVersionVisitor extends MathematicaVisitor {
171208

172209
private final HashMap<String, Double> mySymbolVersions = SymbolVersionProvider.getSymbolNames();
173-
private MathematicaLanguageLevel myLanguageLevel;
174210
private final ProblemsHolder myHolder;
211+
private MathematicaLanguageLevel myLanguageLevel;
175212

176213
WrongVersionVisitor(final ProblemsHolder holder, final MathematicaLanguageLevel usedLanguageVersion) {
177214
this.myHolder = holder;
178-
myLanguageLevel = usedLanguageVersion;
215+
myLanguageLevel = usedLanguageVersion;
179216
}
180217

181218
private void registerProblem(final PsiElement element, final String message) {
@@ -188,11 +225,13 @@ private void registerProblem(final PsiElement element, final String message) {
188225
@Override
189226
public void visitFunctionCall(FunctionCall functionCall) {
190227
final PsiElement head = functionCall.getHead();
191-
if ("Association".equals(head.getText()) && myLanguageLevel.getVersionNumber() < 10 ) {
192-
registerProblem(functionCall, message("bugs.unsupported.version.association", myLanguageLevel.getPresentableText()));
228+
if ("Association".equals(head.getText()) && myLanguageLevel.getVersionNumber() < 10) {
229+
registerProblem(functionCall,
230+
message("bugs.unsupported.version.association", myLanguageLevel.getPresentableText()));
193231
}
194232

195-
if (functionCall.hasHead("With") && myLanguageLevel.getVersionNumber() < 10.3 && functionCall.getArguments().size() > 3) {
233+
if (functionCall.hasHead("With") && myLanguageLevel.getVersionNumber() < 10.3 &&
234+
functionCall.getArguments().size() > 3) {
196235
registerProblem(functionCall, message("bugs.unsupported.version.with", myLanguageLevel.getPresentableText()));
197236

198237
}
@@ -201,7 +240,8 @@ public void visitFunctionCall(FunctionCall functionCall) {
201240
@Override
202241
public void visitAssociation(Association association) {
203242
if (myLanguageLevel.getVersionNumber() < 10) {
204-
registerProblem(association, message("bugs.unsupported.version.association", myLanguageLevel.getPresentableText()));
243+
registerProblem(association,
244+
message("bugs.unsupported.version.association", myLanguageLevel.getPresentableText()));
205245
}
206246
}
207247

@@ -214,12 +254,14 @@ public void visitSymbol(Symbol symbol) {
214254

215255
final PsiElement resolve = symbol.resolve();
216256
if (resolve instanceof LightBuiltInSymbol) {
217-
String nameWithContext = symbol.getMathematicaContext().equals("") ? "System`" + symbol.getSymbolName() : symbol.getFullSymbolName();
257+
String nameWithContext =
258+
symbol.getMathematicaContext().equals("") ? "System`" + symbol.getSymbolName() : symbol.getFullSymbolName();
218259

219260
if (mySymbolVersions.containsKey(nameWithContext)) {
220261
double version = mySymbolVersions.get(nameWithContext);
221262
if (version > myLanguageLevel.getVersionNumber()) {
222-
registerProblem(symbol, "Mathematica " + version + " required. You are using " + myLanguageLevel.getPresentableText());
263+
registerProblem(symbol,
264+
"Mathematica " + version + " required. You are using " + myLanguageLevel.getPresentableText());
223265
}
224266
}
225267
}

src/de/halirutan/mathematica/module/MathematicaGeneralModuleSettingsEditor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.intellij.openapi.roots.ModifiableRootModel;
2727
import com.intellij.openapi.roots.ui.configuration.CommonContentEntriesEditor;
2828
import com.intellij.openapi.roots.ui.configuration.ModuleConfigurationState;
29+
import de.halirutan.mathematica.module.ui.MathematicaLanguageLevelCombo;
2930
import org.jetbrains.jps.model.java.JavaResourceRootType;
3031
import org.jetbrains.jps.model.java.JavaSourceRootType;
3132

src/de/halirutan/mathematica/module/MathematicaLanguageLevelComboBox.java

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package de.halirutan.mathematica.module;
2+
3+
import de.halirutan.mathematica.sdk.MathematicaLanguageLevel;
4+
5+
import javax.annotation.Nullable;
6+
7+
/**
8+
* @author patrick (21.12.17).
9+
*/
10+
public interface MathematicaLanguageLevelModuleExtension {
11+
@Nullable
12+
MathematicaLanguageLevel getMathematicaLanguageLevel();
13+
14+
void setMathematicaLanguageLevel(MathematicaLanguageLevel languageLevel);
15+
16+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
* Copyright (c) 2017 Patrick Scheibe
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
23+
package de.halirutan.mathematica.module;
24+
25+
import com.intellij.openapi.components.BaseState;
26+
import com.intellij.openapi.components.PersistentStateComponentWithModificationTracker;
27+
import com.intellij.openapi.diagnostic.Logger;
28+
import com.intellij.openapi.module.Module;
29+
import com.intellij.openapi.roots.ModuleExtension;
30+
import com.intellij.openapi.roots.ModuleRootManager;
31+
import com.intellij.util.xmlb.annotations.Property;
32+
import de.halirutan.mathematica.sdk.MathematicaLanguageLevel;
33+
import org.jetbrains.annotations.Nullable;
34+
35+
/**
36+
* @author patrick (13.12.17).
37+
*/
38+
public class MathematicaLanguageLevelModuleExtensionImpl extends ModuleExtension implements MathematicaLanguageLevelModuleExtension,
39+
PersistentStateComponentWithModificationTracker<MathematicaLanguageLevelModuleExtensionImpl.State> {
40+
41+
private static final Logger LOG = Logger.getInstance(MathematicaLanguageLevelModuleExtensionImpl.class);
42+
43+
private boolean myWritable;
44+
private Module myModule;
45+
private MathematicaLanguageLevelModuleExtensionImpl mySource;
46+
private State myState;
47+
48+
public MathematicaLanguageLevelModuleExtensionImpl(Module module) {
49+
myWritable = false;
50+
myModule = module;
51+
mySource = null;
52+
myState = new State();
53+
}
54+
55+
public MathematicaLanguageLevelModuleExtensionImpl(MathematicaLanguageLevelModuleExtensionImpl source, boolean writable) {
56+
this.myWritable = writable;
57+
myModule = source.myModule;
58+
mySource = source;
59+
myState = source.myState;
60+
}
61+
62+
@Nullable
63+
public static MathematicaLanguageLevelModuleExtensionImpl getInstance(final Module module) {
64+
return ModuleRootManager.getInstance(module).getModuleExtension(MathematicaLanguageLevelModuleExtensionImpl.class);
65+
}
66+
67+
@Override
68+
public long getStateModificationCount() {
69+
return myState.getModificationCount();
70+
}
71+
72+
@Override
73+
public ModuleExtension getModifiableModel(boolean writable) {
74+
return new MathematicaLanguageLevelModuleExtensionImpl(this, writable);
75+
}
76+
77+
@Override
78+
public void commit() {
79+
if (isChanged()) {
80+
mySource.myState = myState;
81+
}
82+
}
83+
84+
@Override
85+
public boolean isChanged() {
86+
return mySource != null && !mySource.myState.equals(myState);
87+
}
88+
89+
@Nullable
90+
@Override
91+
public State getState() {
92+
return myState;
93+
}
94+
95+
@Override
96+
public void loadState(State state) {
97+
myState = state;
98+
}
99+
100+
@Override
101+
public void dispose() {
102+
myModule = null;
103+
myState = null;
104+
}
105+
106+
@Nullable
107+
@Override
108+
public MathematicaLanguageLevel getMathematicaLanguageLevel() {
109+
return MathematicaLanguageLevel.fromDouble(myState.versionNumber);
110+
}
111+
112+
@Override
113+
public void setMathematicaLanguageLevel(MathematicaLanguageLevel languageLevel) {
114+
LOG.assertTrue(myWritable, "Writable model can be retrieved from writable ModifiableRootModel");
115+
myState.versionNumber = languageLevel.getVersionNumber();
116+
}
117+
118+
static class State extends BaseState {
119+
@Property
120+
double versionNumber;
121+
}
122+
}

src/de/halirutan/mathematica/module/MathematicaModifiedSettingsStep.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.intellij.ide.util.projectWizard.SdkSettingsStep;
2525
import com.intellij.ide.util.projectWizard.SettingsStep;
2626
import com.intellij.openapi.projectRoots.Sdk;
27+
import de.halirutan.mathematica.module.ui.MathematicaLanguageLevelComboBox;
2728
import de.halirutan.mathematica.sdk.MathematicaLanguageLevel;
2829
import de.halirutan.mathematica.sdk.MathematicaSdkType;
2930
import org.jetbrains.annotations.NotNull;

0 commit comments

Comments
 (0)