Skip to content

Commit 3ccc5c5

Browse files
authored
Merge pull request #180 from domaframework/fix/must-be-called-on-edt
Fix: Assert: must be called on EDT
2 parents a1dc2f6 + d55e246 commit 3ccc5c5

File tree

2 files changed

+53
-17
lines changed

2 files changed

+53
-17
lines changed

src/main/kotlin/org/domaframework/doma/intellij/common/helper/ActiveProjectHelper.kt

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,58 @@
1515
*/
1616
package org.domaframework.doma.intellij.common.helper
1717

18+
import com.intellij.openapi.application.ApplicationManager
1819
import com.intellij.openapi.project.Project
1920
import com.intellij.openapi.project.ProjectManager
2021
import com.intellij.openapi.wm.IdeFocusManager
2122

2223
object ActiveProjectHelper {
2324
private var activeProject: Project? = null
2425

26+
/**
27+
* Sets the current active project.
28+
*
29+
* @param value the project to set as active, or null to clear
30+
*/
2531
fun setCurrentActiveProject(value: Project?) {
2632
activeProject = value
2733
}
2834

35+
/**
36+
* Returns the current active project.
37+
*
38+
* If called from the EDT, tries to get the project with UI focus.
39+
* Otherwise, returns the first open project or the last set active project.
40+
*
41+
* @return the current active [Project], or null if none is available
42+
*/
2943
fun getCurrentActiveProject(): Project? {
30-
val initProject = activeProject
31-
val active = getActiveUIProject()
32-
return active ?: initProject
44+
// If EDT, get from focus
45+
if (ApplicationManager.getApplication().isDispatchThread) {
46+
getActiveUIProject()?.let { return it }
47+
}
48+
// If not EDT, the top of openProjects
49+
ProjectManager
50+
.getInstance()
51+
.openProjects
52+
.firstOrNull()
53+
?.let { return it }
54+
55+
return activeProject
3356
}
3457

58+
/**
59+
* Returns the project that currently has UI focus, if any.
60+
*
61+
* @return the [Project] with UI focus, or null if none
62+
*/
3563
private fun getActiveUIProject(): Project? {
3664
val openProjects: Array<out Project> = ProjectManager.getInstance().openProjects
37-
var active: Project? = null
38-
3965
for (project in openProjects) {
4066
if (IdeFocusManager.getInstance(project).focusOwner != null) {
41-
active = project
42-
break
67+
return project
4368
}
4469
}
45-
46-
return active
70+
return null
4771
}
4872
}

src/main/kotlin/org/domaframework/doma/intellij/setting/DomaToolsConfigurable.kt

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,37 @@ package org.domaframework.doma.intellij.setting
1717

1818
import com.intellij.openapi.options.Configurable
1919
import com.intellij.openapi.options.ConfigurationException
20+
import com.intellij.util.ui.UIUtil.setEnabledRecursively
2021
import org.domaframework.doma.intellij.common.helper.ActiveProjectHelper
2122
import org.domaframework.doma.intellij.setting.state.DomaToolsFormatEnableSettings
2223
import javax.swing.JComponent
2324

2425
class DomaToolsConfigurable : Configurable {
2526
private var mySettingsComponent: SettingComponent? = SettingComponent()
26-
private val project = ActiveProjectHelper.getCurrentActiveProject()
2727

2828
private var formatSettings: DomaToolsFormatEnableSettings? = null
2929

30-
init {
31-
project?.let {
32-
formatSettings = DomaToolsFormatEnableSettings.getInstance(it)
33-
}
34-
}
35-
3630
override fun getDisplayName(): String = "Doma Tools"
3731

38-
override fun createComponent(): JComponent? = mySettingsComponent?.panel
32+
/**
33+
* Creates and returns the settings panel component.
34+
*
35+
* Gets the current active project and initializes the settings panel.
36+
* If no project is active, the panel will be disabled.
37+
* Loads both format settings and custom function settings for the active project.
38+
*
39+
* @return The settings panel component, or null if creation fails
40+
*/
41+
override fun createComponent(): JComponent? {
42+
val project = ActiveProjectHelper.getCurrentActiveProject()
43+
val panel = mySettingsComponent?.panel ?: return null
44+
if (project == null) {
45+
setEnabledRecursively(panel, false)
46+
return null
47+
}
48+
formatSettings = DomaToolsFormatEnableSettings.getInstance(project)
49+
return panel
50+
}
3951

4052
override fun isModified(): Boolean {
4153
val enableFormatModified = formatSettings?.isModified(mySettingsComponent) != false

0 commit comments

Comments
 (0)