diff --git a/platform/platform-impl/src/com/intellij/ide/impl/ProjectUtil.java b/platform/platform-impl/src/com/intellij/ide/impl/ProjectUtil.java index 4f42abb3004b..5a5e2a16c7b0 100644 --- a/platform/platform-impl/src/com/intellij/ide/impl/ProjectUtil.java +++ b/platform/platform-impl/src/com/intellij/ide/impl/ProjectUtil.java @@ -283,26 +283,26 @@ public class ProjectUtil { public static boolean isSameProject(String projectFilePath, @NotNull Project project) { IProjectStore projectStore = ProjectKt.getStateStore(project); - - String toOpen = FileUtil.toSystemIndependentName(projectFilePath); - String existing = projectStore.getProjectFilePath(); - - String existingBaseDir = projectStore.getProjectBasePath(); - if (existingBaseDir == null) { + String existingBaseDirPath = projectStore.getProjectBasePath(); + if (existingBaseDirPath == null) { // could be null if not yet initialized return false; } - final File openFile = new File(toOpen); - if (openFile.isDirectory()) { - return FileUtil.pathsEqual(toOpen, existingBaseDir); - } - if (StorageScheme.DIRECTORY_BASED == projectStore.getStorageScheme()) { - // todo: check if IPR is located not under the project base dir - return FileUtil.pathsEqual(FileUtil.toSystemIndependentName(openFile.getParentFile().getPath()), existingBaseDir); + final File projectFile = new File(projectFilePath); + if (projectFile.isDirectory()) { + return FileUtil.pathsEqual(projectFilePath, existingBaseDirPath); } - return FileUtil.pathsEqual(toOpen, existing); + if (projectStore.getStorageScheme() == StorageScheme.DEFAULT) { + return FileUtil.pathsEqual(projectFilePath, projectStore.getProjectFilePath()); + } + + File parent = projectFile.getParentFile(); + if (parent.getName().equals(Project.DIRECTORY_STORE_FOLDER)) { + parent = parent.getParentFile(); + } + return parent != null && FileUtil.pathsEqual(parent.getPath(), existingBaseDirPath); } public static void focusProjectWindow(final Project p, boolean executeIfAppInactive) { diff --git a/platform/platform-tests/testSrc/com/intellij/openapi/project/impl/ProjectOpeningTest.java b/platform/platform-tests/testSrc/com/intellij/openapi/project/impl/ProjectOpeningTest.java index d6a016f6f346..be90a7042fba 100644 --- a/platform/platform-tests/testSrc/com/intellij/openapi/project/impl/ProjectOpeningTest.java +++ b/platform/platform-tests/testSrc/com/intellij/openapi/project/impl/ProjectOpeningTest.java @@ -15,6 +15,7 @@ */ package com.intellij.openapi.project.impl; +import com.intellij.ide.impl.ProjectUtil; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.progress.ProgressIndicator; import com.intellij.openapi.progress.ProgressManager; @@ -29,6 +30,7 @@ import com.intellij.testFramework.PlatformTestUtil; import org.jetbrains.annotations.NotNull; import java.io.File; +import java.io.IOException; import static com.intellij.openapi.startup.StartupActivity.POST_STARTUP_ACTIVITY; @@ -85,6 +87,31 @@ public class ProjectOpeningTest extends PlatformTestCase { */ + public void testIsSameProjectForDirectoryBasedProject() throws IOException { + File projectDir = createTempDir("project"); + Project dirBasedProject = ProjectManager.getInstance().createProject("project", projectDir.getAbsolutePath()); + disposeOnTearDown(dirBasedProject); + + assertTrue(ProjectUtil.isSameProject(projectDir.getAbsolutePath(), dirBasedProject)); + assertFalse(ProjectUtil.isSameProject(createTempDir("project2").getAbsolutePath(), dirBasedProject)); + File iprFilePath = new File(projectDir, "project.ipr"); + assertTrue(ProjectUtil.isSameProject(iprFilePath.getAbsolutePath(), dirBasedProject)); + File miscXmlFilePath = new File(projectDir, ".idea/misc.xml"); + assertTrue(ProjectUtil.isSameProject(miscXmlFilePath.getAbsolutePath(), dirBasedProject)); + } + + public void testIsSameProjectForFileBasedProject() throws IOException { + File projectDir = createTempDir("project"); + File iprFilePath = new File(projectDir, "project.ipr"); + Project fileBasedProject = ProjectManager.getInstance().createProject(iprFilePath.getName(), iprFilePath.getAbsolutePath()); + disposeOnTearDown(fileBasedProject); + + assertTrue(ProjectUtil.isSameProject(projectDir.getAbsolutePath(), fileBasedProject)); + assertFalse(ProjectUtil.isSameProject(createTempDir("project2").getAbsolutePath(), fileBasedProject)); + File iprFilePath2 = new File(projectDir, "project2.ipr"); + assertFalse(ProjectUtil.isSameProject(iprFilePath2.getAbsolutePath(), fileBasedProject)); + } + private static void closeProject(final Project project) { if (project != null && !project.isDisposed()) { ProjectManager.getInstance().closeProject(project);