From cd7026ba94fcac09e68f7cf86cef52fccc6b07ea Mon Sep 17 00:00:00 2001 From: Ilia Zakoulov Date: Tue, 18 Nov 2025 15:42:02 +0100 Subject: [PATCH] PY-84031: Replace getInstance with direct openProjectAndFile call in CommandLineProjectOpenProcessor. This change allows inheritors of CommandLineProjectOpenProcessor to optinally openProjectAndFile by returning null. If null is returned, next CommandLineProjectOpenProcessor will be taken. In previous implementation it would take the first CommandLineProjectOpenProcessor, but there is no guarantee that it will open a project. Space-RevId: 8294fbbd603519a0c348a1d97346ac168794c2ed GitOrigin-RevId: 91bb048e921f265681c92606fb71c107d28c3867 --- .../platform-impl/api-dump-unreviewed.txt | 2 -- platform/platform-impl/api-dump.txt | 3 +++ .../com/intellij/ide/CommandLineProcessor.kt | 2 +- .../src/com/intellij/ide/impl/ProjectUtil.kt | 19 ++++++++----------- .../CommandLineProjectOpenProcessor.kt | 17 ++++++++--------- 5 files changed, 20 insertions(+), 23 deletions(-) diff --git a/platform/platform-impl/api-dump-unreviewed.txt b/platform/platform-impl/api-dump-unreviewed.txt index 4dad8506669e..7f6243718b26 100644 --- a/platform/platform-impl/api-dump-unreviewed.txt +++ b/platform/platform-impl/api-dump-unreviewed.txt @@ -10469,8 +10469,6 @@ f:com.intellij.platform.AttachProjectAction$Companion - f:attachProject(com.intellij.openapi.vfs.VirtualFile,com.intellij.openapi.project.Project):V - f:getTO_SELECT_KEY():com.intellij.openapi.util.Key f:com.intellij.platform.CommandLineProjectOpenProcessor$Companion -- f:getInstance():com.intellij.platform.CommandLineProjectOpenProcessor -- f:getInstanceIfExists():com.intellij.platform.CommandLineProjectOpenProcessor a:com.intellij.platform.DirectoryProjectConfigurator$AsyncDirectoryProjectConfigurator - com.intellij.platform.DirectoryProjectConfigurator - ():V diff --git a/platform/platform-impl/api-dump.txt b/platform/platform-impl/api-dump.txt index 033af7cf9830..8edd82b37b06 100644 --- a/platform/platform-impl/api-dump.txt +++ b/platform/platform-impl/api-dump.txt @@ -1089,6 +1089,9 @@ com.intellij.platform.CommandLineProjectOpenProcessor - sf:Companion:com.intellij.platform.CommandLineProjectOpenProcessor$Companion - a:openProjectAndFile(java.nio.file.Path,Z,com.intellij.ide.impl.OpenProjectTask,kotlin.coroutines.Continuation):java.lang.Object - bs:openProjectAndFile$default(com.intellij.platform.CommandLineProjectOpenProcessor,java.nio.file.Path,Z,com.intellij.ide.impl.OpenProjectTask,kotlin.coroutines.Continuation,I,java.lang.Object):java.lang.Object +f:com.intellij.platform.CommandLineProjectOpenProcessor$Companion +- f:openProjectAndFile(java.nio.file.Path,Z,com.intellij.ide.impl.OpenProjectTask,kotlin.coroutines.Continuation):java.lang.Object +- bs:openProjectAndFile$default(com.intellij.platform.CommandLineProjectOpenProcessor$Companion,java.nio.file.Path,Z,com.intellij.ide.impl.OpenProjectTask,kotlin.coroutines.Continuation,I,java.lang.Object):java.lang.Object com.intellij.platform.DirectoryProjectConfigurator - a:configureProject(com.intellij.openapi.project.Project,com.intellij.openapi.vfs.VirtualFile,com.intellij.openapi.util.Ref,Z):V - isEdtRequired():Z diff --git a/platform/platform-impl/src/com/intellij/ide/CommandLineProcessor.kt b/platform/platform-impl/src/com/intellij/ide/CommandLineProcessor.kt index 962b43f2ff3a..764a47e863be 100644 --- a/platform/platform-impl/src/com/intellij/ide/CommandLineProcessor.kt +++ b/platform/platform-impl/src/com/intellij/ide/CommandLineProcessor.kt @@ -135,7 +135,7 @@ object CommandLineProcessor { } if (projects.isEmpty()) { - val project = CommandLineProjectOpenProcessor.getInstance().openProjectAndFile(ioFile, tempProject, OpenProjectTask { + val project = CommandLineProjectOpenProcessor.openProjectAndFile(ioFile, tempProject, OpenProjectTask { this.line = line this.column = column }) ?: return createError(IdeBundle.message("dialog.message.no.project.found.to.open.file.in")) diff --git a/platform/platform-impl/src/com/intellij/ide/impl/ProjectUtil.kt b/platform/platform-impl/src/com/intellij/ide/impl/ProjectUtil.kt index 73874fc52f78..d93a4545e378 100644 --- a/platform/platform-impl/src/com/intellij/ide/impl/ProjectUtil.kt +++ b/platform/platform-impl/src/com/intellij/ide/impl/ProjectUtil.kt @@ -549,18 +549,15 @@ object ProjectUtil { LOG.debug { "$location: open file $file" } if (projectToClose == null) { - val processor = CommandLineProjectOpenProcessor.getInstanceIfExists() - if (processor != null) { - val opened = FUSProjectHotStartUpMeasurer.withProjectContextElement(file) { - processor.openProjectAndFile(file = file, tempProject = false) + val openedProject = FUSProjectHotStartUpMeasurer.withProjectContextElement(file) { + CommandLineProjectOpenProcessor.openProjectAndFile(file = file, tempProject = false) + } + if (openedProject != null) { + if (result == null) { + result = openedProject } - if (opened != null) { - if (result == null) { - result = opened - } - else { - FUSProjectHotStartUpMeasurer.openingMultipleProjects(false, list.size, false) - } + else { + FUSProjectHotStartUpMeasurer.openingMultipleProjects(false, list.size, false) } } } diff --git a/platform/platform-impl/src/com/intellij/platform/CommandLineProjectOpenProcessor.kt b/platform/platform-impl/src/com/intellij/platform/CommandLineProjectOpenProcessor.kt index 66e8154273ef..c4503c1d9aac 100644 --- a/platform/platform-impl/src/com/intellij/platform/CommandLineProjectOpenProcessor.kt +++ b/platform/platform-impl/src/com/intellij/platform/CommandLineProjectOpenProcessor.kt @@ -4,7 +4,6 @@ package com.intellij.platform import com.intellij.ide.impl.OpenProjectTask import com.intellij.openapi.project.Project import com.intellij.projectImport.ProjectOpenProcessor -import org.jetbrains.annotations.ApiStatus import java.nio.file.Path /** @@ -18,20 +17,20 @@ interface CommandLineProjectOpenProcessor { * @param file the file to open * @param tempProject if `true`, always opens the file in a new temporary project, otherwise searches the parent directories * for `.idea` subdirectory, and if found, opens that directory. + * + * @return project or null if it cannot be opened with that open processor */ suspend fun openProjectAndFile(file: Path, tempProject: Boolean, options: OpenProjectTask = OpenProjectTask()): Project? companion object { - fun getInstance(): CommandLineProjectOpenProcessor = getInstanceIfExists() ?: getDefaultInstance() - - @ApiStatus.Internal - fun getDefaultInstance(): CommandLineProjectOpenProcessor = PlatformProjectOpenProcessor.getInstance() - - fun getInstanceIfExists(): CommandLineProjectOpenProcessor? { - return ProjectOpenProcessor.EXTENSION_POINT_NAME.getIterable() + suspend fun openProjectAndFile(file: Path, tempProject: Boolean, options: OpenProjectTask = OpenProjectTask()): Project? { + val project = ProjectOpenProcessor.EXTENSION_POINT_NAME.getIterable() .asSequence() .filterIsInstance() - .firstOrNull() + .firstNotNullOfOrNull { + it.openProjectAndFile(file, tempProject, options) + } + return project } } } \ No newline at end of file