2626import com .intellij .codeHighlighting .HighlightDisplayLevel ;
2727import com .intellij .codeInspection .LocalInspectionToolSession ;
2828import 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 ;
2932import com .intellij .openapi .projectRoots .Sdk ;
33+ import com .intellij .openapi .roots .ModuleFileIndex ;
34+ import com .intellij .openapi .roots .ModuleRootManager ;
3035import com .intellij .openapi .roots .ProjectRootManager ;
3136import com .intellij .openapi .ui .ComboBox ;
3237import com .intellij .openapi .ui .VerticalFlowLayout ;
3338import com .intellij .openapi .util .TextRange ;
39+ import com .intellij .openapi .vfs .VirtualFile ;
3440import com .intellij .psi .PsiElement ;
3541import com .intellij .psi .PsiElementVisitor ;
42+ import com .intellij .psi .PsiFile ;
3643import de .halirutan .mathematica .codeinsight .completion .util .SymbolVersionProvider ;
3744import de .halirutan .mathematica .codeinsight .inspections .AbstractInspection ;
3845import de .halirutan .mathematica .file .MathematicaFileType ;
4148import de .halirutan .mathematica .lang .psi .api .Symbol ;
4249import de .halirutan .mathematica .lang .psi .api .lists .Association ;
4350import de .halirutan .mathematica .lang .psi .impl .LightBuiltInSymbol ;
51+ import de .halirutan .mathematica .module .MathematicaLanguageLevelModuleExtensionImpl ;
52+ import de .halirutan .mathematica .module .MathematicaModuleType ;
4453import de .halirutan .mathematica .sdk .MathematicaLanguageLevel ;
4554import de .halirutan .mathematica .sdk .MathematicaSdkType ;
4655import 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 }
0 commit comments