Skip to content

Commit

Permalink
Added options for the "Check Symbol Version" inspection.
Browse files Browse the repository at this point in the history
  • Loading branch information
halirutan committed Nov 25, 2016
1 parent bd06993 commit cdc85bc
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ consistent.compound.expression.in.file.message=Missing semicolon
bugs.implicit.times.through.linebreak.name=Multiplication through linebreak
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.
bugs.implicit.times.through.linebreak.message=Missing comma or semicolon
bugs.unsupported.version.name=Unsupported version for function
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.
bugs.unsupported.version.name=Function is part of later version
bugs.unsupported.version.description=Reports functions or symbols that are part of a later version of Mathematica than specified. Per default, the language version of the specified Mathematica SDK is used.
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@

package de.halirutan.mathematica.codeinsight.inspections.bugs;

import com.intellij.codeHighlighting.HighlightDisplayLevel;
import com.intellij.codeInspection.LocalInspectionToolSession;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.ui.ComboBox;
import com.intellij.openapi.ui.VerticalFlowLayout;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
Expand All @@ -40,15 +43,87 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;

/**
* Provides warnings when you are using Mathematica symbols that are introduces later then the version you are using.
* Provides warnings when you are using Mathematica symbols that are introduces later than the version you are using.
*
* @author halirutan
*/
public class UnsupportedVersion extends AbstractInspection {

@SuppressWarnings("InstanceVariableNamingConvention")
public MathematicaLanguageLevel languageLevel = MathematicaLanguageLevel.HIGHEST;

@SuppressWarnings("InstanceVariableNamingConvention")
public boolean useSDKLanguageLevelOrHighest = true;

/**
* Sets the correct text for the info label in the inspection settings page
*
* @param label label to set the text
*/
private void setLabelTextToVersion(JLabel label) {
if (useSDKLanguageLevelOrHighest) {
label.setText("Use language version from Project SDK");
} else {
label.setText(languageLevel.getPresentableText());
}
}

@Nullable
@Override
public JComponent createOptionsPanel() {
final JPanel mainPanel = new JPanel(new VerticalFlowLayout(VerticalFlowLayout.TOP));
final JCheckBox useSDKCheckbox = new JCheckBox("Use Project SDK Language Level");
final JLabel infoLabel = new JLabel();
//noinspection Since15
final ComboBox<MathematicaLanguageLevel> versionComboBox = new ComboBox<MathematicaLanguageLevel>();

for (MathematicaLanguageLevel level : MathematicaLanguageLevel.values()) {
//noinspection unchecked
versionComboBox.addItem(level);
}
versionComboBox.setSelectedItem(languageLevel);
versionComboBox.setEditable(false);
//noinspection unchecked
versionComboBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
final MathematicaLanguageLevel selectedItem = (MathematicaLanguageLevel) versionComboBox.getSelectedItem();
if (selectedItem != null) {
languageLevel = selectedItem;
setLabelTextToVersion(infoLabel);
}
}
});

useSDKCheckbox.setSelected(useSDKLanguageLevelOrHighest);
useSDKCheckbox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
useSDKLanguageLevelOrHighest = useSDKCheckbox.isSelected();
versionComboBox.setVisible(!useSDKLanguageLevelOrHighest);
if (!useSDKLanguageLevelOrHighest) {
languageLevel = (MathematicaLanguageLevel) versionComboBox.getSelectedItem();
}
setLabelTextToVersion(infoLabel);
}
});

setLabelTextToVersion(infoLabel);
versionComboBox.setVisible(!useSDKLanguageLevelOrHighest);

mainPanel.add(infoLabel);
mainPanel.add(useSDKCheckbox);
mainPanel.add(versionComboBox);

return mainPanel;
}

@Nls
@NotNull
@Override
Expand All @@ -69,36 +144,50 @@ public String getGroupDisplayName() {
return MathematicaInspectionBundle.message("group.bugs");
}

@NotNull
@Override
public HighlightDisplayLevel getDefaultLevel() {
return HighlightDisplayLevel.ERROR;
}

@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly, @NotNull LocalInspectionToolSession session) {
if(session.getFile().getFileType() instanceof MathematicaFileType) {
return new WrongVersionVisitor(holder);
if (session.getFile().getFileType() instanceof MathematicaFileType) {
if (useSDKLanguageLevelOrHighest) {
final ProjectRootManager manager = ProjectRootManager.getInstance(holder.getProject());
final Sdk projectSdk = manager.getProjectSdk();
if (projectSdk != null && projectSdk.getSdkType() instanceof MathematicaSdkType) {
languageLevel = MathematicaLanguageLevel.createFromSdk(projectSdk);
}
}
return new WrongVersionVisitor(holder, languageLevel);
} else return PsiElementVisitor.EMPTY_VISITOR;
}



/**
* This visitor just inspects all symbols in the file. For each symbol it checks whether it is in the list of built-in
* symbols and if yes, if it is already defined in the Mathematica version the user specified
*/
private static class WrongVersionVisitor extends MathematicaVisitor {


private HashMap<String, Double> mySymbolVersions = SymbolVersionProvider.getSymbolNames();
private MathematicaLanguageLevel myLanguageLevel = MathematicaLanguageLevel.HIGHEST;
private final ProblemsHolder myHolder;

WrongVersionVisitor(final ProblemsHolder holder) {
WrongVersionVisitor(final ProblemsHolder holder, final MathematicaLanguageLevel usedLanguageVersion) {
this.myHolder = holder;
final Sdk projectSdk = ProjectRootManager.getInstance(myHolder.getProject()).getProjectSdk();
if (projectSdk != null && projectSdk.getSdkType() instanceof MathematicaSdkType) {
myLanguageLevel = MathematicaLanguageLevel.createFromSdk(projectSdk);
myLanguageLevel = usedLanguageVersion;
}

}

private void registerProblem(final PsiElement element, final String message) {
myHolder.registerProblem(
element,
TextRange.from(0,element.getTextLength()),
TextRange.from(0, element.getTextLength()),
message);
}

Expand All @@ -113,12 +202,11 @@ public void visitSymbol(Symbol symbol) {
if (mySymbolVersions.containsKey(nameWithContext)) {
double version = mySymbolVersions.get(nameWithContext);
if (version > myLanguageLevel.getVersionNumber()) {
registerProblem(symbol, "Mathematica " + version + " required. Your project SDK is " + myLanguageLevel.getPresentableText());
registerProblem(symbol, "Mathematica " + version + " required. You are using " + myLanguageLevel.getPresentableText());
}
}


}
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,13 @@
*/
package de.halirutan.mathematica.module;

import com.intellij.core.JavaCoreBundle;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.roots.LanguageLevelModuleExtension;
import com.intellij.openapi.roots.LanguageLevelProjectExtension;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.ArrayUtil;
import de.halirutan.mathematica.MathematicaBundle;
import de.halirutan.mathematica.sdk.MathematicaSdkType;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.lang.manifest.ManifestBundle;

/**
* @author dsl
Expand All @@ -52,7 +46,6 @@ public enum MathematicaLanguageLevel {


public static final MathematicaLanguageLevel HIGHEST = M_11;
public static final Key<MathematicaLanguageLevel> KEY = Key.create("MATHEMATICA_LANGUAGE_LEVEL");

private final String myName;
private final String myPresentableText;
Expand Down Expand Up @@ -101,4 +94,10 @@ public boolean isAtLeast(@NotNull MathematicaLanguageLevel level) {
public boolean isLessThan(@NotNull MathematicaLanguageLevel level) {
return compareTo(level) < 0;
}


@Override
public String toString() {
return myPresentableText;
}
}
11 changes: 6 additions & 5 deletions src/de/halirutan/mathematica/sdk/MathematicaSdkType.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static MathematicaSdkType getInstance() {
* Path to the install directory
* @return Version number in the format e.g. 9.0.1
*/
public static String getMathematicaVersionString(String path) {
private static String getMathematicaVersionString(String path) {
File versionID = new File(path + File.separatorChar + ".VersionID");
String versionString = "Unknown";

Expand Down Expand Up @@ -136,20 +136,20 @@ public String suggestSdkName(String currentSdkName, String sdkHome) {
}

@Override
public boolean isRootTypeApplicable(OrderRootType type) {
public boolean isRootTypeApplicable(@NotNull OrderRootType type) {
return type.equals(OrderRootType.SOURCES) || type.equals(OrderRootType.DOCUMENTATION) || type.equals(OrderRootType.CLASSES);
}

@Nullable
@Override
public AdditionalDataConfigurable createAdditionalDataConfigurable(SdkModel sdkModel, SdkModificator sdkModificator) {
public AdditionalDataConfigurable createAdditionalDataConfigurable(@NotNull SdkModel sdkModel, @NotNull SdkModificator sdkModificator) {
return null;
}

@NotNull
@Override
public String getPresentableName() {
return "Mathematica Sdk";
return "Mathematica SDK";
}

@Override
Expand All @@ -161,13 +161,14 @@ public Icon getIcon() {
return MathematicaIcons.FILE_ICON;
}

@NotNull
@Override
public Icon getIconForAddAction() {
return MathematicaIcons.FILE_ICON;
}

@Override
public boolean setupSdkPaths(Sdk sdk, SdkModel sdkModel) {
public boolean setupSdkPaths(@NotNull Sdk sdk, @NotNull SdkModel sdkModel) {
final SdkModificator sdkModificator = sdk.getSdkModificator();
final String homePath = sdk.getHomePath();
sdkModificator.setVersionString(getMathematicaVersionString(homePath));
Expand Down

0 comments on commit cdc85bc

Please sign in to comment.