PY-18970 Check that imports in "project" group don't belong to any library root
authorMikhail Golubev <mikhail.golubev@jetbrains.com>
Tue, 3 May 2016 13:16:41 +0000 (16:16 +0300)
committerMikhail Golubev <mikhail.golubev@jetbrains.com>
Wed, 4 May 2016 11:12:10 +0000 (14:12 +0300)
Consider such imports as imports from the standard library or third party
libraries instead.

python/src/com/jetbrains/python/codeInsight/imports/AddImportHelper.java
python/testData/optimizeImports/libraryRootInsideProject/a.py [new file with mode: 0644]
python/testData/optimizeImports/libraryRootInsideProject/lib/b.py [new file with mode: 0644]
python/testData/optimizeImports/libraryRootInsideProject/main.after.py [new file with mode: 0644]
python/testData/optimizeImports/libraryRootInsideProject/main.py [new file with mode: 0644]
python/testSrc/com/jetbrains/python/PyOptimizeImportsTest.java

index 1f5bbbe3511deac9c8d041061b0bf5c701f1de9b..8534a2c900aabd6d4ca1c4018052d4f9acb61c06 100644 (file)
@@ -20,6 +20,7 @@ import com.intellij.openapi.diagnostic.Logger;
 import com.intellij.openapi.module.Module;
 import com.intellij.openapi.module.ModuleUtilCore;
 import com.intellij.openapi.projectRoots.Sdk;
+import com.intellij.openapi.roots.ProjectFileIndex;
 import com.intellij.openapi.roots.ProjectRootManager;
 import com.intellij.openapi.util.text.StringUtil;
 import com.intellij.openapi.vfs.VirtualFile;
@@ -263,7 +264,8 @@ public class AddImportHelper {
       return UNRESOLVED_SYMBOL_PRIORITY;
     }
     final ProjectRootManager projectRootManager = ProjectRootManager.getInstance(toImport.getProject());
-    if (projectRootManager.getFileIndex().isInContent(vFile)) {
+    final ProjectFileIndex fileIndex = projectRootManager.getFileIndex();
+    if (fileIndex.isInContent(vFile) && !fileIndex.isInLibraryClasses(vFile)) {
       return ImportPriority.PROJECT;
     }
     final Module module = ModuleUtilCore.findModuleForPsiElement(importLocation);
diff --git a/python/testData/optimizeImports/libraryRootInsideProject/a.py b/python/testData/optimizeImports/libraryRootInsideProject/a.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/python/testData/optimizeImports/libraryRootInsideProject/lib/b.py b/python/testData/optimizeImports/libraryRootInsideProject/lib/b.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/python/testData/optimizeImports/libraryRootInsideProject/main.after.py b/python/testData/optimizeImports/libraryRootInsideProject/main.after.py
new file mode 100644 (file)
index 0000000..f69993a
--- /dev/null
@@ -0,0 +1,5 @@
+import b
+
+import a
+
+print(a, b)
diff --git a/python/testData/optimizeImports/libraryRootInsideProject/main.py b/python/testData/optimizeImports/libraryRootInsideProject/main.py
new file mode 100644 (file)
index 0000000..8a707f2
--- /dev/null
@@ -0,0 +1,4 @@
+import a
+import b
+
+print(a, b)
index 668b53d841869186ce65e9ad04d0c027838d58e3..c812c354c0168a4a90bb0e1f72ed3db61d466f35 100644 (file)
@@ -17,7 +17,13 @@ package com.jetbrains.python;
 
 import com.intellij.codeInsight.actions.OptimizeImportsAction;
 import com.intellij.ide.DataManager;
+import com.intellij.openapi.application.WriteAction;
+import com.intellij.openapi.projectRoots.Sdk;
+import com.intellij.openapi.projectRoots.SdkModificator;
+import com.intellij.openapi.roots.OrderRootType;
+import com.intellij.openapi.vfs.VirtualFile;
 import com.jetbrains.python.fixtures.PyTestCase;
+import com.jetbrains.python.sdk.PythonSdkType;
 
 /**
  * @author yole
@@ -95,6 +101,38 @@ public class PyOptimizeImportsTest extends PyTestCase {
     doTest();
   }
 
+  // PY-18970
+  public void testLibraryRootInsideProject() {
+    final String testName = getTestName(true);
+    myFixture.copyDirectoryToProject(testName, "");
+    final VirtualFile libDir = myFixture.findFileInTempDir("lib");
+    assertNotNull(libDir);
+
+    final Sdk sdk = PythonSdkType.findPythonSdk(myFixture.getModule());
+    assertNotNull(sdk);
+    WriteAction.run(() -> {
+      final SdkModificator modificator = sdk.getSdkModificator();
+      assertNotNull(modificator);
+      modificator.addRoot(libDir, OrderRootType.CLASSES);
+      modificator.commitChanges();
+    });
+
+    try {
+      myFixture.configureByFile("main.py");
+      OptimizeImportsAction.actionPerformedImpl(DataManager.getInstance().getDataContext(myFixture.getEditor().getContentComponent()));
+      myFixture.checkResultByFile(testName + "/main.after.py");
+    }
+    finally {
+      //noinspection ThrowFromFinallyBlock
+      WriteAction.run(() -> {
+        final SdkModificator modificator = sdk.getSdkModificator();
+        assertNotNull(modificator);
+        modificator.removeRoot(libDir, OrderRootType.CLASSES);
+        modificator.commitChanges();
+      });
+    }
+  }
+
   private void doTest() {
     myFixture.configureByFile(getTestName(true) + ".py");
     OptimizeImportsAction.actionPerformedImpl(DataManager.getInstance().getDataContext(myFixture.getEditor().getContentComponent()));