Skip to content

Commit 2c19974

Browse files
committed
Ensure getCurrentActiveProject is called on EDT and improve project retrieval logic
1 parent a1dc2f6 commit 2c19974

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
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: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,28 @@ 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+
override fun createComponent(): JComponent? {
33+
val project = ActiveProjectHelper.getCurrentActiveProject()
34+
val panel = mySettingsComponent?.panel ?: return null
35+
if (project == null) {
36+
setEnabledRecursively(panel, false)
37+
return null
38+
}
39+
formatSettings = DomaToolsFormatEnableSettings.getInstance(project)
40+
return panel
41+
}
3942

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

0 commit comments

Comments
 (0)