|
| 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 | + |
0 commit comments