push python language level only for python modules, flush cached modules in PyTestCase (thanks to Yann)

This commit is contained in:
Ekaterina Tuzova
2014-06-16 14:11:23 +04:00
parent acbd82ee9c
commit dc678fee8f
2 changed files with 46 additions and 17 deletions
@@ -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();
}
}
@@ -16,13 +16,14 @@
package com.jetbrains.python.fixtures;
import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.module.EmptyModuleType;
import com.intellij.openapi.extensions.Extensions;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleType;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.roots.ContentEntry;
import com.intellij.openapi.roots.ModifiableRootModel;
import com.intellij.openapi.roots.OrderRootType;
import com.intellij.openapi.roots.impl.FilePropertyPusher;
import com.intellij.openapi.roots.libraries.Library;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
@@ -39,6 +40,7 @@ import com.intellij.testFramework.fixtures.TestFixtureBuilder;
import com.intellij.testFramework.fixtures.impl.LightTempDirTestFixtureImpl;
import com.jetbrains.python.PythonHelpersLocator;
import com.jetbrains.python.PythonMockSdk;
import com.jetbrains.python.PythonModuleTypeBase;
import com.jetbrains.python.PythonTestUtil;
import com.jetbrains.python.psi.LanguageLevel;
import com.jetbrains.python.psi.PyClass;
@@ -92,6 +94,8 @@ public abstract class PyTestCase extends UsefulTestCase {
setLanguageLevel(null);
myFixture.tearDown();
myFixture = null;
final PythonLanguageLevelPusher levelPusher = Extensions.findExtension(FilePropertyPusher.EP_NAME, PythonLanguageLevelPusher.class);
levelPusher.flushLanguageLevelCache();
super.tearDown();
}
@@ -149,7 +153,7 @@ public abstract class PyTestCase extends UsefulTestCase {
@Override
public ModuleType getModuleType() {
return EmptyModuleType.getInstance();
return PythonModuleTypeBase.getInstance();
}
@Override