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,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 }
0 commit comments