Skip to content

Commit aa9e41f

Browse files
committed
Ensure getCurrentActiveProject is called on EDT and improve project retrieval logic
1 parent bedb330 commit aa9e41f

File tree

3 files changed

+27
-24
lines changed

3 files changed

+27
-24
lines changed

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
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
@@ -27,22 +28,27 @@ object ActiveProjectHelper {
2728
}
2829

2930
fun getCurrentActiveProject(): Project? {
30-
val initProject = activeProject
31-
val active = getActiveUIProject()
32-
return active ?: initProject
31+
// If EDT, get from focus
32+
if (ApplicationManager.getApplication().isDispatchThread) {
33+
getActiveUIProject()?.let { return it }
34+
}
35+
// If not EDT, the top of openProjects
36+
ProjectManager
37+
.getInstance()
38+
.openProjects
39+
.firstOrNull()
40+
?.let { return it }
41+
42+
return activeProject
3343
}
3444

3545
private fun getActiveUIProject(): Project? {
3646
val openProjects: Array<out Project> = ProjectManager.getInstance().openProjects
37-
var active: Project? = null
38-
3947
for (project in openProjects) {
4048
if (IdeFocusManager.getInstance(project).focusOwner != null) {
41-
active = project
42-
break
49+
return project
4350
}
4451
}
45-
46-
return active
52+
return null
4753
}
4854
}

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,31 @@ 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.DomaToolsCustomFunctionSettings
2223
import org.domaframework.doma.intellij.setting.state.DomaToolsFormatEnableSettings
2324
import javax.swing.JComponent
2425

2526
class DomaToolsConfigurable : Configurable {
2627
private var mySettingsComponent: SettingComponent? = SettingComponent()
27-
private val project = ActiveProjectHelper.getCurrentActiveProject()
2828

2929
private var formatSettings: DomaToolsFormatEnableSettings? = null
3030
private var customFunctionsSettings: DomaToolsCustomFunctionSettings? = null
3131

32-
init {
33-
project?.let {
34-
customFunctionsSettings = DomaToolsCustomFunctionSettings.getInstance(it)
35-
formatSettings = DomaToolsFormatEnableSettings.getInstance(it)
36-
}
37-
}
38-
3932
override fun getDisplayName(): String = "Doma Tools"
4033

41-
override fun createComponent(): JComponent? = mySettingsComponent?.panel
34+
override fun createComponent(): JComponent? {
35+
val project = ActiveProjectHelper.getCurrentActiveProject()
36+
val panel = mySettingsComponent?.panel ?: return null
37+
if (project == null) {
38+
setEnabledRecursively(panel, false)
39+
return null
40+
}
41+
customFunctionsSettings = DomaToolsCustomFunctionSettings.getInstance(project)
42+
formatSettings = DomaToolsFormatEnableSettings.getInstance(project)
43+
return panel
44+
}
4245

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

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import com.intellij.ui.components.JBCheckBox
2020
import com.intellij.ui.components.JBLabel
2121
import com.intellij.ui.components.JBList
2222
import com.intellij.util.ui.FormBuilder
23-
import com.intellij.util.ui.UIUtil.setEnabledRecursively
2423
import org.domaframework.doma.intellij.bundle.MessageBundle
2524
import org.domaframework.doma.intellij.common.helper.ActiveProjectHelper
2625
import org.domaframework.doma.intellij.common.helper.ExpressionFunctionsHelper
@@ -86,11 +85,6 @@ class SettingComponent {
8685
}.disableUpDownActions()
8786
.createPanel()
8887

89-
// Disable customFunctionPanel if no active project is available
90-
if (ActiveProjectHelper.getCurrentActiveProject() == null) {
91-
setEnabledRecursively(customFunctionPanel, false)
92-
}
93-
9488
this.panel =
9589
FormBuilder
9690
.createFormBuilder()

0 commit comments

Comments
 (0)