disable "theOnlyProject" optimization when the default project is alive to avoid confusion when "Settings|Code style" code fragments expect their .getProject() to return DefaultProject

This commit is contained in:
Alexey Kudravtsev
2019-03-20 17:07:24 +03:00
parent 25db6f666f
commit 57b3f4192e
3 changed files with 33 additions and 6 deletions
@@ -20,10 +20,14 @@ import com.intellij.openapi.fileTypes.FileTypeRegistry;
import com.intellij.openapi.fileTypes.InternalFileType;
import com.intellij.openapi.vfs.VfsUtilCore;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class ProjectCoreUtil {
@Deprecated
@ApiStatus.Experimental
public static volatile Project theProject;
public static boolean isProjectOrWorkspaceFile(@NotNull VirtualFile file) {
@@ -37,7 +41,7 @@ public class ProjectCoreUtil {
}
/**
* @return the only open project if there is one, null if no or several projects are open
* @return the only open project if there is one, null if no projects open, or several projects are open, or default project is created
*/
@Nullable
public static Project theOnlyOpenProject() {
@@ -2,6 +2,8 @@
package com.intellij.openapi.project.impl;
import com.intellij.openapi.components.ComponentConfig;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.ProjectManager;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -9,10 +11,14 @@ import org.jetbrains.annotations.Nullable;
* @author peter
*/
final class DefaultProject extends ProjectImpl {
private static final Logger LOG = Logger.getInstance(DefaultProject.class);
private static final String TEMPLATE_PROJECT_NAME = "Default (Template) Project";
DefaultProject(@NotNull String filePath) {
super(filePath, TEMPLATE_PROJECT_NAME);
if (LOG.isDebugEnabled()) {
LOG.debug("Created DefaultProject " + this, new Exception());
}
}
@Override
@@ -38,4 +44,13 @@ final class DefaultProject extends ProjectImpl {
protected boolean isComponentSuitable(@NotNull ComponentConfig componentConfig) {
return super.isComponentSuitable(componentConfig) && componentConfig.isLoadForDefaultProject();
}
@Override
public synchronized void dispose() {
super.dispose();
if (LOG.isDebugEnabled()) {
LOG.debug("Disposed DefaultProject "+this);
}
((ProjectManagerImpl)ProjectManager.getInstance()).updateTheOnlyProjectField();
}
}
@@ -336,7 +336,11 @@ public class ProjectManagerImpl extends ProjectManagerEx implements Disposable {
@NotNull
public synchronized Project getDefaultProject() {
LOG.assertTrue(!ApplicationManager.getApplication().isDisposed(), "Default project has been already disposed!");
return myDefaultProjectTimed.get();
Project defaultProject = myDefaultProjectTimed.get();
// disable "the only project" optimization since we have now more than one project.
// (even though the default project is not a real project, it can be used indirectly in e.g. "Settings|Code Style" code fragments PSI)
updateTheOnlyProjectField();
return defaultProject;
}
@Override
@@ -440,18 +444,22 @@ public class ProjectManagerImpl extends ProjectManagerEx implements Disposable {
return false;
}
myOpenProjects = ArrayUtil.append(myOpenProjects, project);
//noinspection AssignmentToStaticFieldFromInstanceMethod
ProjectCoreUtil.theProject = myOpenProjects.length == 1 ? project : null;
updateTheOnlyProjectField();
myOpenProjectByHash.put(project.getLocationHash(), project);
}
return true;
}
void updateTheOnlyProjectField() {
synchronized (lock) {
ProjectCoreUtil.theProject = myOpenProjects.length == 1 && !isDefaultProjectInitialized() ? myOpenProjects[0] : null;
}
}
private void removeFromOpened(@NotNull Project project) {
synchronized (lock) {
myOpenProjects = ArrayUtil.remove(myOpenProjects, project);
//noinspection AssignmentToStaticFieldFromInstanceMethod
ProjectCoreUtil.theProject = myOpenProjects.length == 1 ? myOpenProjects[0] : null;
updateTheOnlyProjectField();
myOpenProjectByHash.values().remove(project); // remove by value and not by key!
}
}