Skip to content

Commit 396401e

Browse files
committed
First implementation
1 parent 3da9d82 commit 396401e

18 files changed

Lines changed: 497 additions & 67 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 & 10 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,8 +130,7 @@ public JComponent createOptionsPanel() {
114130

115131
setLabelTextToVersion(infoLabel);
116132
versionComboBox.setVisible(!useSDKLanguageLevelOrHighest);
117-
118-
mainPanel.add(infoLabel);
133+
mainPanel.add(useModuleCheckbox);
119134
mainPanel.add(useSDKCheckbox);
120135
mainPanel.add(versionComboBox);
121136

@@ -152,6 +167,27 @@ public HighlightDisplayLevel getDefaultLevel() {
152167
@Override
153168
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly, @NotNull LocalInspectionToolSession session) {
154169
if (session.getFile().getFileType() instanceof MathematicaFileType) {
170+
171+
// TODO: There must be a simpler way to find the module for a source file
172+
if (useModuleLanguageLevelOrHighest) {
173+
final PsiFile file = session.getFile();
174+
final ModuleManager instance = ModuleManager.getInstance(file.getProject());
175+
for (Module module : instance.getModules()) {
176+
if (ModuleType.is(module, MathematicaModuleType.getInstance())) {
177+
final ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module);
178+
final ModuleFileIndex fileIndex = moduleRootManager.getFileIndex();
179+
final VirtualFile virtualFile = file.getVirtualFile();
180+
if (fileIndex.isInContent(virtualFile)) {
181+
final MathematicaLanguageLevelModuleExtensionImpl languageLevelModuleExtension =
182+
moduleRootManager.getModuleExtension(MathematicaLanguageLevelModuleExtensionImpl.class);
183+
if (languageLevelModuleExtension.getMathematicaLanguageLevel() != null) {
184+
return new WrongVersionVisitor(holder, languageLevelModuleExtension.getMathematicaLanguageLevel());
185+
}
186+
}
187+
}
188+
}
189+
}
190+
155191
if (useSDKLanguageLevelOrHighest) {
156192
final ProjectRootManager manager = ProjectRootManager.getInstance(holder.getProject());
157193
final Sdk projectSdk = manager.getProjectSdk();
@@ -170,12 +206,12 @@ public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, fina
170206
private static class WrongVersionVisitor extends MathematicaVisitor {
171207

172208
private final HashMap<String, Double> mySymbolVersions = SymbolVersionProvider.getSymbolNames();
173-
private MathematicaLanguageLevel myLanguageLevel;
174209
private final ProblemsHolder myHolder;
210+
private MathematicaLanguageLevel myLanguageLevel;
175211

176212
WrongVersionVisitor(final ProblemsHolder holder, final MathematicaLanguageLevel usedLanguageVersion) {
177213
this.myHolder = holder;
178-
myLanguageLevel = usedLanguageVersion;
214+
myLanguageLevel = usedLanguageVersion;
179215
}
180216

181217
private void registerProblem(final PsiElement element, final String message) {
@@ -188,11 +224,13 @@ private void registerProblem(final PsiElement element, final String message) {
188224
@Override
189225
public void visitFunctionCall(FunctionCall functionCall) {
190226
final PsiElement head = functionCall.getHead();
191-
if ("Association".equals(head.getText()) && myLanguageLevel.getVersionNumber() < 10 ) {
192-
registerProblem(functionCall, message("bugs.unsupported.version.association", myLanguageLevel.getPresentableText()));
227+
if ("Association".equals(head.getText()) && myLanguageLevel.getVersionNumber() < 10) {
228+
registerProblem(functionCall,
229+
message("bugs.unsupported.version.association", myLanguageLevel.getPresentableText()));
193230
}
194231

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

198236
}
@@ -201,7 +239,8 @@ public void visitFunctionCall(FunctionCall functionCall) {
201239
@Override
202240
public void visitAssociation(Association association) {
203241
if (myLanguageLevel.getVersionNumber() < 10) {
204-
registerProblem(association, message("bugs.unsupported.version.association", myLanguageLevel.getPresentableText()));
242+
registerProblem(association,
243+
message("bugs.unsupported.version.association", myLanguageLevel.getPresentableText()));
205244
}
206245
}
207246

@@ -214,12 +253,14 @@ public void visitSymbol(Symbol symbol) {
214253

215254
final PsiElement resolve = symbol.resolve();
216255
if (resolve instanceof LightBuiltInSymbol) {
217-
String nameWithContext = symbol.getMathematicaContext().equals("") ? "System`" + symbol.getSymbolName() : symbol.getFullSymbolName();
256+
String nameWithContext =
257+
symbol.getMathematicaContext().equals("") ? "System`" + symbol.getSymbolName() : symbol.getFullSymbolName();
218258

219259
if (mySymbolVersions.containsKey(nameWithContext)) {
220260
double version = mySymbolVersions.get(nameWithContext);
221261
if (version > myLanguageLevel.getVersionNumber()) {
222-
registerProblem(symbol, "Mathematica " + version + " required. You are using " + myLanguageLevel.getPresentableText());
262+
registerProblem(symbol,
263+
"Mathematica " + version + " required. You are using " + myLanguageLevel.getPresentableText());
223264
}
224265
}
225266
}

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: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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 = new State();
60+
myState.versionNumber = source.myState.versionNumber;
61+
}
62+
63+
@Nullable
64+
public static MathematicaLanguageLevelModuleExtensionImpl getInstance(final Module module) {
65+
return ModuleRootManager.getInstance(module).getModuleExtension(MathematicaLanguageLevelModuleExtensionImpl.class);
66+
}
67+
68+
@Override
69+
public long getStateModificationCount() {
70+
return myState.getModificationCount();
71+
}
72+
73+
@Override
74+
public ModuleExtension getModifiableModel(boolean writable) {
75+
return new MathematicaLanguageLevelModuleExtensionImpl(this, writable);
76+
}
77+
78+
@Override
79+
public void commit() {
80+
if (isChanged()) {
81+
mySource.myState = myState;
82+
}
83+
}
84+
85+
@Override
86+
public boolean isChanged() {
87+
return mySource != null && mySource.myState.versionNumber != myState.versionNumber;
88+
}
89+
90+
@Nullable
91+
@Override
92+
public State getState() {
93+
return myState;
94+
}
95+
96+
@Override
97+
public void loadState(State state) {
98+
myState = state;
99+
}
100+
101+
@Override
102+
public void dispose() {
103+
myModule = null;
104+
myState = null;
105+
}
106+
107+
@Nullable
108+
@Override
109+
public MathematicaLanguageLevel getMathematicaLanguageLevel() {
110+
return MathematicaLanguageLevel.fromDouble(myState.versionNumber);
111+
}
112+
113+
@Override
114+
public void setMathematicaLanguageLevel(MathematicaLanguageLevel languageLevel) {
115+
LOG.assertTrue(myWritable, "Writable model can be retrieved from writable ModifiableRootModel");
116+
myState.versionNumber = languageLevel.getVersionNumber();
117+
}
118+
119+
static class State extends BaseState {
120+
@Property
121+
double versionNumber;
122+
}
123+
}

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)