-
Notifications
You must be signed in to change notification settings - Fork 329
Fix IllegalStateException in ProjectOpenProcessor #8633
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+207
−160
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 0 additions & 57 deletions
57
src/io/flutter/editor/FlutterStudioProjectOpenProcessor.java
This file was deleted.
Oops, something went wrong.
79 changes: 79 additions & 0 deletions
79
src/io/flutter/editor/FlutterStudioProjectOpenProcessor.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| /* | ||
| * Copyright 2025 The Chromium Authors. All rights reserved. | ||
| * Use of this source code is governed by a BSD-style license that can be | ||
| * found in the LICENSE file. | ||
| */ | ||
| package io.flutter.editor | ||
|
|
||
| import com.intellij.openapi.application.writeAction | ||
| import com.intellij.openapi.project.Project | ||
| import com.intellij.openapi.vfs.VirtualFile | ||
| import com.intellij.projectImport.ProjectOpenProcessor | ||
| import com.intellij.ui.EditorNotifications | ||
| import io.flutter.FlutterUtils | ||
| import io.flutter.project.FlutterProjectOpenProcessor | ||
| import io.flutter.pub.PubRoot | ||
| import io.flutter.utils.FlutterModuleUtils | ||
|
|
||
| /** | ||
| * Originally `FlutterStudioProjectOpenProcessor.java`. | ||
| * | ||
| * This processor is specific to Android Studio (or when the Flutter Studio plugin is active). | ||
| * It extends `FlutterProjectOpenProcessor` to provide specialized handling for Android Studio, | ||
| * particularly ensuring that the project is correctly re-detected if the opening process causes a reload. | ||
| * | ||
| * Converted to Kotlin to support `openProjectAsync`. | ||
| */ | ||
| class FlutterStudioProjectOpenProcessor : FlutterProjectOpenProcessor() { | ||
| override val name: String | ||
| get() = "Flutter Studio" | ||
|
|
||
| override fun canOpenProject(file: VirtualFile): Boolean = | ||
| PubRoot.forDirectory(file)?.declaresFlutter() == true | ||
|
|
||
| /** | ||
| * Replaces the deprecated `doOpenProject`. | ||
| * | ||
| * Performs the same logic as the original Java implementation but using `suspend` and `writeAction`. | ||
| * | ||
| * Key differences from the base class: | ||
| * - Explicitly looks up the project again using `FlutterUtils.findProject` after opening, as the project instance might have changed | ||
| * (e.g. if the platform closed and reopened it during import). | ||
| * - Ensures Dart SDK is enabled and notifications are updated. | ||
| */ | ||
| override suspend fun openProjectAsync( | ||
| virtualFile: VirtualFile, | ||
| projectToClose: Project?, | ||
| forceOpenInNewFrame: Boolean, | ||
| ): Project? { | ||
| val importProvider = getDelegateImportProvider(virtualFile) ?: return null | ||
| val project = importProvider.openProjectAsync(virtualFile, projectToClose, forceOpenInNewFrame) | ||
|
|
||
| // A callback may have caused the project to be reloaded. Find the new Project object. | ||
| val newProject = FlutterUtils.findProject(virtualFile.path) | ||
| if (newProject == null || newProject.isDisposed) { | ||
| return newProject | ||
| } | ||
|
|
||
| for (module in FlutterModuleUtils.getModules(newProject)) { | ||
| if (FlutterModuleUtils.declaresFlutter(module) && !FlutterModuleUtils.isFlutterModule(module)) { | ||
| writeAction { | ||
| FlutterModuleUtils.setFlutterModuleType(module) | ||
| } | ||
| FlutterModuleUtils.enableDartSDK(module) | ||
| } | ||
| } | ||
| newProject.save() | ||
| EditorNotifications.getInstance(newProject).updateAllNotifications() | ||
|
|
||
| return newProject | ||
| } | ||
|
|
||
| override fun doOpenProject( | ||
| virtualFile: VirtualFile, | ||
| projectToClose: Project?, | ||
| forceOpenInNewFrame: Boolean, | ||
| ): Project? { | ||
| return null | ||
| } | ||
| } |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| /* | ||
| * Copyright 2025 The Chromium Authors. All rights reserved. | ||
| * Use of this source code is governed by a BSD-style license that can be | ||
| * found in the LICENSE file. | ||
| */ | ||
| package io.flutter.project | ||
|
|
||
| import com.intellij.openapi.application.ApplicationInfo | ||
| import com.intellij.openapi.application.writeAction | ||
| import com.intellij.openapi.module.Module | ||
| import com.intellij.openapi.project.Project | ||
| import com.intellij.openapi.vfs.VirtualFile | ||
| import com.intellij.projectImport.ProjectOpenProcessor | ||
| import icons.FlutterIcons | ||
| import io.flutter.FlutterBundle | ||
| import io.flutter.FlutterUtils | ||
| import io.flutter.pub.PubRoot | ||
| import io.flutter.utils.FlutterModuleUtils | ||
| import java.util.Objects | ||
| import javax.swing.Icon | ||
|
|
||
| /** | ||
| * Originally `FlutterProjectOpenProcessor.java`. | ||
| * | ||
| * This processor handles opening Flutter projects when they are selected directly (e.g. via "Open" in the IDE). | ||
| * It delegates the actual opening to the platform's default processor (e.g. Gradle or Maven processor if applicable, | ||
| * or the generic project opener) and then ensures that any modules in the project are correctly configured as Flutter modules. | ||
| * | ||
| * Converted to Kotlin to support `openProjectAsync` which is a suspend function. | ||
| */ | ||
| open class FlutterProjectOpenProcessor : ProjectOpenProcessor() { | ||
| override val name: String | ||
| get() = FlutterBundle.message("flutter.module.name") | ||
|
|
||
| override fun getIcon(file: VirtualFile): Icon? { | ||
| return FlutterIcons.Flutter | ||
| } | ||
|
|
||
| override fun canOpenProject(file: VirtualFile): Boolean { | ||
| val info = ApplicationInfo.getInstance() | ||
| if (FlutterUtils.isAndroidStudio()) { | ||
| return false | ||
| } | ||
| val root = PubRoot.forDirectory(file) | ||
| return root != null && root.declaresFlutter() | ||
| } | ||
|
|
||
| /** | ||
| * Replaces the deprecated `doOpenProject`. | ||
| * | ||
| * This method is `suspend` and must be used instead of `doOpenProject` to avoid `IllegalStateException` in newer IDE versions. | ||
| * | ||
| * It performs the following steps: | ||
| * 1. Finds a delegate processor (e.g. Gradle) to open the project. | ||
| * 2. Opens the project asynchronously. | ||
| * 3. Once opened, checks if the project contains Flutter modules that are not yet configured as such (e.g. missing module type). | ||
| * 4. Configures these modules as Flutter modules within a write action. | ||
| */ | ||
| override suspend fun openProjectAsync( | ||
| virtualFile: VirtualFile, | ||
| projectToClose: Project?, | ||
| forceOpenInNewFrame: Boolean, | ||
| ): Project? { | ||
| // Delegate opening to the platform open processor. | ||
| val importProvider = getDelegateImportProvider(virtualFile) ?: return null | ||
| val project = importProvider.openProjectAsync(virtualFile, projectToClose, forceOpenInNewFrame) | ||
| if (project == null || project.isDisposed) return project | ||
|
|
||
| // Convert any modules that use Flutter but don't have IntelliJ Flutter metadata. | ||
| convertToFlutterProject(project) | ||
|
|
||
| return project | ||
| } | ||
|
|
||
| /** | ||
| * Deprecated method, kept to satisfy the compiler/interface. | ||
| * | ||
| * We return `null` to indicate that this processor does not support the synchronous opening method | ||
| * and that `openProjectAsync` should be used instead. | ||
| */ | ||
| override fun doOpenProject( | ||
| virtualFile: VirtualFile, | ||
| projectToClose: Project?, | ||
| forceOpenInNewFrame: Boolean, | ||
| ): Project? { | ||
| return null | ||
| } | ||
|
|
||
| protected open fun getDelegateImportProvider(file: VirtualFile): ProjectOpenProcessor? { | ||
| return EXTENSION_POINT_NAME.extensionList.stream().filter { processor: ProjectOpenProcessor -> | ||
| processor.canOpenProject(file) && !Objects.equals( | ||
| processor.name, | ||
| name | ||
| ) | ||
| }.findFirst().orElse(null) | ||
| } | ||
|
|
||
|
|
||
| companion object { | ||
| /** | ||
| * Sets up a project that doesn't have any Flutter modules. | ||
| * | ||
| * | ||
| * (It probably wasn't created with "flutter create" and probably didn't have any IntelliJ configuration before.) | ||
| */ | ||
| private fun convertToFlutterProject(project: Project) { | ||
| for (module in FlutterModuleUtils.getModules(project)) { | ||
| if (FlutterModuleUtils.declaresFlutter(module) && !FlutterModuleUtils.isFlutterModule(module)) { | ||
| FlutterModuleUtils.setFlutterModuleAndReload(module, project) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this required by your change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes to avoid runtime issues with the Kotlin files being introduced.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it, thanks. Are there any kotlin files in
vmServiceDrivers? (Or will there be?)