avoid using getModuleFile, prefer path
authorVladimir Krivosheev <vladimir.krivosheev@jetbrains.com>
Wed, 2 Sep 2015 13:16:10 +0000 (15:16 +0200)
committerVladimir Krivosheev <vladimir.krivosheev@jetbrains.com>
Thu, 3 Sep 2015 10:32:43 +0000 (12:32 +0200)
java/idea-ui/src/com/intellij/ide/impl/ProjectStructureSelectInTarget.java
java/testFramework/src/com/intellij/testFramework/CompilerTester.java
java/testFramework/src/com/intellij/testFramework/TestSourceBasedTestCase.java
platform/configuration-store-impl/src/ProjectStoreImpl.kt
platform/core-api/src/com/intellij/openapi/module/Module.java
platform/core-api/src/com/intellij/openapi/project/Project.java
platform/editor-ui-ex/src/com/intellij/codeStyle/AbstractConvertLineSeparatorsAction.java
platform/platform-impl/src/com/intellij/openapi/components/impl/stores/IProjectStore.java
platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/NonProjectFileWritingAccessProvider.java
platform/projectModel-api/src/com/intellij/openapi/module/ModuleUtilCore.java

index 70457eb6bdf3ce60ea88738975fc0fe93f5f7858..dc178a2b75e22957067772754a6471299dfedc7e 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2000-2013 JetBrains s.r.o.
+ * Copyright 2000-2015 JetBrains s.r.o.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@ import com.intellij.openapi.application.ApplicationManager;
 import com.intellij.openapi.fileTypes.StdFileTypes;
 import com.intellij.openapi.module.Module;
 import com.intellij.openapi.module.ModuleManager;
+import com.intellij.openapi.module.ModuleUtilCore;
 import com.intellij.openapi.project.DumbAware;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.roots.OrderEntry;
@@ -100,7 +101,7 @@ public class ProjectStructureSelectInTarget extends SelectInTargetBase implement
   @Nullable
   private static Module findModuleByModuleFile(@NotNull Project project, @NotNull VirtualFile file) {
     for (Module module : ModuleManager.getInstance(project).getModules()) {
-      if (file.equals(module.getModuleFile())) {
+      if (ModuleUtilCore.isModuleFile(module, file)) {
         return module;
       }
     }
index ab85a3fabbee7c2d22bcd13fce528f36aaa1d3e5..c6cdc0fe3c49955ab4a391dec7ce1391b7991a0f 100644 (file)
@@ -38,6 +38,7 @@ import com.intellij.testFramework.fixtures.TempDirTestFixture;
 import com.intellij.testFramework.fixtures.impl.TempDirTestFixtureImpl;
 import com.intellij.util.Consumer;
 import com.intellij.util.ObjectUtils;
+import com.intellij.util.ThrowableRunnable;
 import com.intellij.util.concurrency.Semaphore;
 import com.intellij.util.ui.UIUtil;
 import org.jetbrains.annotations.NotNull;
@@ -203,26 +204,19 @@ public class CompilerTester {
     semaphore.down();
 
     final ErrorReportingCallback callback = new ErrorReportingCallback(semaphore);
-    UIUtil.invokeAndWaitIfNeeded(new Runnable() {
+    EdtTestUtil.runInEdtAndWait(new ThrowableRunnable<Throwable>() {
       @Override
-      public void run() {
-        try {
-          getProject().save();
-          CompilerTestUtil.saveApplicationSettings();
-          for (Module module : myModules) {
-            VirtualFile moduleFile = module.getModuleFile();
-            assert moduleFile != null;
-            File ioFile = VfsUtilCore.virtualToIoFile(moduleFile);
-            if (!ioFile.exists()) {
-              getProject().save();
-              assert ioFile.exists() : "File does not exist: " + ioFile.getPath();
-            }
+      public void run() throws Throwable {
+        getProject().save();
+        CompilerTestUtil.saveApplicationSettings();
+        for (Module module : myModules) {
+          File ioFile = new File(module.getModuleFilePath());
+          if (!ioFile.exists()) {
+            getProject().save();
+            assert ioFile.exists() : "File does not exist: " + ioFile.getPath();
           }
-          runnable.consume(callback);
-        }
-        catch (Exception e) {
-          throw new RuntimeException(e);
         }
+        runnable.consume(callback);
       }
     });
 
index 0f4c5a1f53ef8ee20b2b4a2097f3a10dde0e4010..774b9df5b96a813ae58e4f3905da9974c734a9d6 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2000-2014 JetBrains s.r.o.
+ * Copyright 2000-2015 JetBrains s.r.o.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@ import com.intellij.openapi.util.io.FileUtil;
 import com.intellij.openapi.vfs.LocalFileSystem;
 import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.psi.PsiDirectory;
+import com.intellij.util.PathUtil;
 import org.jetbrains.annotations.NonNls;
 
 import java.io.File;
@@ -100,6 +101,6 @@ import java.io.File;
   }
   
   protected String getRootFiles() {
-    return " " + myModule.getModuleFile().getName() + "\n";
+    return " " + PathUtil.getFileName(myModule.getModuleFilePath()) + "\n";
   }
 }
index 5ca7c61f9f6ce1306285ad9a9e72678c245bd3fb..0ec530a25f79f195943071b20fe41f8c6ee6b3b0 100644 (file)
@@ -102,12 +102,9 @@ open class ProjectStoreImpl(override val project: ProjectImpl, private val pathM
     storageManager.clearStorages()
   }
 
-  override fun getProjectBaseDir(): VirtualFile? {
-    val path = getProjectBasePath() ?: return null
-    return LocalFileSystem.getInstance().findFileByPath(path)
-  }
+  override fun getProjectBaseDir() = LocalFileSystem.getInstance().findFileByPath(getProjectBasePath())
 
-  override fun getProjectBasePath(): String? {
+  override fun getProjectBasePath(): String {
     val path = PathUtilRt.getParentPath(getProjectFilePath())
     return if (scheme == StorageScheme.DEFAULT) path else PathUtilRt.getParentPath(path)
   }
@@ -159,10 +156,7 @@ open class ProjectStoreImpl(override val project: ProjectImpl, private val pathM
 
   override fun getPresentableUrl(): String? {
     if (presentableUrl == null) {
-      val url = if (scheme == StorageScheme.DIRECTORY_BASED) getProjectBasePath() else getProjectFilePath()
-      if (url != null) {
-        presentableUrl = FileUtil.toSystemDependentName(url)
-      }
+      presentableUrl = FileUtil.toSystemDependentName(if (scheme == StorageScheme.DIRECTORY_BASED) getProjectBasePath() else getProjectFilePath())
     }
     return presentableUrl
   }
index 9900d384e36733fea0f1f8f7f4e177d8de07e376..36c9bef94dc930e28fe091a485595d3aeafae64d 100644 (file)
@@ -48,11 +48,10 @@ public interface Module extends ComponentManager, AreaInstance, Disposable {
   VirtualFile getModuleFile();
 
   /**
-   * Returns the path to the module .iml file.
-   *
-   * @return the path to the .iml file.
+   * System-independent path to the .iml file.
    */
-  @NotNull String getModuleFilePath();
+  @NotNull
+  String getModuleFilePath();
 
   /**
    * Returns the project to which this module belongs.
index 1e0b2894412d7a632dcf2e772455327245c7fcb5..83fa78028fc7c9d562c61ca93bb552f4476b3198 100644 (file)
@@ -54,7 +54,7 @@ public interface Project extends ComponentManager, AreaInstance {
   VirtualFile getBaseDir();
 
   /**
-   * Returns a system-dependent path to a project base directory (see {@linkplain #getBaseDir()}).<br/>
+   * Returns a system-independent path to a project base directory (see {@linkplain #getBaseDir()}).<br/>
    * Returns <code>null</code> for default project.
    *
    * @return a path to a project base directory, or <code>null</code> for default project
index 1f96eb1aa9a89fe5402585444ba5d60014555dfd..d9872d2e56b43c7b5f1eb3f7c58cc43cd418a299 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2000-2013 JetBrains s.r.o.
+ * Copyright 2000-2015 JetBrains s.r.o.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@ import com.intellij.openapi.fileEditor.FileDocumentManager;
 import com.intellij.openapi.fileEditor.impl.LoadTextUtil;
 import com.intellij.openapi.fileTypes.FileTypeRegistry;
 import com.intellij.openapi.module.Module;
+import com.intellij.openapi.module.ModuleUtilCore;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.roots.FileIndexFacade;
 import com.intellij.openapi.util.text.StringUtil;
@@ -133,7 +134,7 @@ public abstract class AbstractConvertLineSeparatorsAction extends AnAction {
       return false;
     }
     Module module = FileIndexFacade.getInstance(project).getModuleForFile(file);
-    return module == null || !file.equals(module.getModuleFile());
+    return module == null || !ModuleUtilCore.isModuleFile(module, file);
   }
 
   public static void changeLineSeparators(@NotNull final Project project,
index 05e10b5cbacca1c2830a17df7f67c682d4545517..66c346e7d1036697bf0f7e2890b3eb7d7283e863 100644 (file)
@@ -29,6 +29,9 @@ public interface IProjectStore extends IComponentStore {
   VirtualFile getProjectBaseDir();
 
   @Nullable
+  /**
+   * System-independent path.
+   */
   String getProjectBasePath();
 
   @NotNull
@@ -47,12 +50,18 @@ public interface IProjectStore extends IComponentStore {
   VirtualFile getProjectFile();
 
   @NotNull
+  /**
+   * System-independent path.
+   */
   String getProjectFilePath();
 
   @Nullable
   VirtualFile getWorkspaceFile();
 
   @Nullable
+  /**
+   * System-independent path.
+   */
   String getWorkspaceFilePath();
 
   void loadProjectFromTemplate(@NotNull Project project);
index dfa210fab04064ff435539c8aab7bfad0a2d0560..fce25b5691854e68e2462933ec73bb32e05697e7 100644 (file)
@@ -30,6 +30,7 @@ import com.intellij.openapi.roots.ModuleRootEvent;
 import com.intellij.openapi.roots.ProjectFileIndex;
 import com.intellij.openapi.util.Key;
 import com.intellij.openapi.util.NotNullLazyKey;
+import com.intellij.openapi.util.io.FileUtil;
 import com.intellij.openapi.util.registry.Registry;
 import com.intellij.openapi.vfs.*;
 import com.intellij.util.NotNullFunction;
@@ -162,9 +163,14 @@ public class NonProjectFileWritingAccessProvider extends WritingAccessProvider {
       }
 
       IProjectStore store = (IProjectStore)ComponentsPackage.getStateStore(project);
-      if (file.equals(store.getWorkspaceFile()) || file.equals(store.getProjectFile())) return true;
-      for (Module each : ModuleManager.getInstance(project).getModules()) {
-        if (file.equals(each.getModuleFile())) return true;
+      String filePath = file.getPath();
+      if (FileUtil.namesEqual(filePath, store.getWorkspaceFilePath()) || FileUtil.namesEqual(filePath, store.getProjectFilePath())) {
+        return true;
+      }
+      for (Module module : ModuleManager.getInstance(project).getModules()) {
+        if (FileUtil.namesEqual(filePath, module.getModuleFilePath())) {
+          return true;
+        }
       }
     }
 
index db65e295ba03a4009f9904c204b2c3b63a9d5144..6e9b4fda363cb8e56d377e1a4b59f7a192e40564 100644 (file)
@@ -20,10 +20,12 @@ import com.intellij.openapi.application.Result;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.roots.*;
 import com.intellij.openapi.util.Key;
+import com.intellij.openapi.util.io.FileUtil;
 import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.psi.PsiElement;
 import com.intellij.psi.PsiFile;
 import com.intellij.psi.PsiFileSystemItem;
+import com.intellij.util.PathUtilRt;
 import com.intellij.util.containers.HashSet;
 import com.intellij.util.graph.Graph;
 import org.jetbrains.annotations.NotNull;
@@ -214,6 +216,19 @@ public class ModuleUtilCore {
     }
   }
 
+  public static boolean isModuleFile(@NotNull Module module, @NotNull VirtualFile file) {
+    return FileUtil.namesEqual(file.getPath(), module.getModuleFilePath());
+  }
+
+  public static boolean isModuleDir(@NotNull Module module, @NotNull VirtualFile dir) {
+    return FileUtil.namesEqual(dir.getPath(), getModuleDirPath(module));
+  }
+
+  @NotNull
+  public static String getModuleDirPath(@NotNull Module module) {
+    return PathUtilRt.getParentPath(module.getModuleFilePath());
+  }
+
   public interface ModuleVisitor {
     /**
      *