diff options
Diffstat (limited to 'python/src/com/jetbrains/python/psi/impl/PythonLanguageLevelPusher.java')
-rw-r--r-- | python/src/com/jetbrains/python/psi/impl/PythonLanguageLevelPusher.java | 55 |
1 files changed, 40 insertions, 15 deletions
diff --git a/python/src/com/jetbrains/python/psi/impl/PythonLanguageLevelPusher.java b/python/src/com/jetbrains/python/psi/impl/PythonLanguageLevelPusher.java index c3fd23bc289c..136a7073bb8b 100644 --- a/python/src/com/jetbrains/python/psi/impl/PythonLanguageLevelPusher.java +++ b/python/src/com/jetbrains/python/psi/impl/PythonLanguageLevelPusher.java @@ -15,11 +15,14 @@ */ package com.jetbrains.python.psi.impl; +import com.intellij.facet.Facet; +import com.intellij.facet.FacetManager; import com.intellij.openapi.application.Application; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.fileTypes.FileTypeManager; import com.intellij.openapi.module.Module; import com.intellij.openapi.module.ModuleManager; +import com.intellij.openapi.module.ModuleType; import com.intellij.openapi.module.ModuleUtilCore; import com.intellij.openapi.project.Project; import com.intellij.openapi.projectRoots.Sdk; @@ -37,6 +40,8 @@ import com.intellij.util.containers.WeakHashMap; import com.intellij.util.io.DataInputOutputUtil; import com.intellij.util.messages.MessageBus; import com.jetbrains.python.PythonFileType; +import com.jetbrains.python.PythonModuleTypeBase; +import com.jetbrains.python.facet.PythonFacetSettings; import com.jetbrains.python.psi.LanguageLevel; import com.jetbrains.python.sdk.PythonSdkType; import org.jetbrains.annotations.NotNull; @@ -61,11 +66,13 @@ public class PythonLanguageLevelPusher implements FilePropertyPusher<LanguageLev final Module[] modules = ModuleManager.getInstance(project).getModules(); Set<Sdk> usedSdks = new HashSet<Sdk>(); for (Module module : modules) { - final Sdk sdk = PythonSdkType.findPythonSdk(module); - myModuleSdks.put(module, sdk); - if (sdk != null && !usedSdks.contains(sdk)) { - usedSdks.add(sdk); - updateSdkLanguageLevel(project, sdk); + if (isPythonModule(module)) { + final Sdk sdk = PythonSdkType.findPythonSdk(module); + myModuleSdks.put(module, sdk); + if (sdk != null && !usedSdks.contains(sdk)) { + usedSdks.add(sdk); + updateSdkLanguageLevel(project, sdk); + } } } } @@ -167,17 +174,19 @@ public class PythonLanguageLevelPusher implements FilePropertyPusher<LanguageLev final Module[] modules = ModuleManager.getInstance(project).getModules(); boolean needReparseOpenFiles = false; for (Module module : modules) { - Sdk newSdk = PythonSdkType.findPythonSdk(module); - if (myModuleSdks.containsKey(module)) { - Sdk oldSdk = myModuleSdks.get(module); - if ((newSdk != null || oldSdk != null) && newSdk != oldSdk) { - needReparseOpenFiles = true; + if (isPythonModule(module)) { + Sdk newSdk = PythonSdkType.findPythonSdk(module); + if (myModuleSdks.containsKey(module)) { + Sdk oldSdk = myModuleSdks.get(module); + if ((newSdk != null || oldSdk != null) && newSdk != oldSdk) { + needReparseOpenFiles = true; + } + } + myModuleSdks.put(module, newSdk); + if (newSdk != null && !updatedSdks.contains(newSdk)) { + updatedSdks.add(newSdk); + updateSdkLanguageLevel(project, newSdk); } - } - myModuleSdks.put(module, newSdk); - if (newSdk != null && !updatedSdks.contains(newSdk)) { - updatedSdks.add(newSdk); - updateSdkLanguageLevel(project, newSdk); } } if (needReparseOpenFiles) { @@ -185,6 +194,18 @@ public class PythonLanguageLevelPusher implements FilePropertyPusher<LanguageLev } } + private static boolean isPythonModule(@NotNull final Module module) { + final ModuleType moduleType = ModuleType.get(module); + if (moduleType instanceof PythonModuleTypeBase) return true; + final Facet[] allFacets = FacetManager.getInstance(module).getAllFacets(); + for (Facet facet : allFacets) { + if (facet.getConfiguration() instanceof PythonFacetSettings) { + return true; + } + } + return false; + } + private void updateSdkLanguageLevel(final Project project, final Sdk sdk) { final LanguageLevel languageLevel = PythonSdkType.getLanguageLevelForSdk(sdk); final VirtualFile[] files = sdk.getRootProvider().getFiles(OrderRootType.CLASSES); @@ -240,4 +261,8 @@ public class PythonLanguageLevelPusher implements FilePropertyPusher<LanguageLev LanguageLevel.FORCE_LANGUAGE_LEVEL = languageLevel; pushLanguageLevel(project); } + + public void flushLanguageLevelCache() { + myModuleSdks.clear(); + } } |