Skip to content

Commit a9c1048

Browse files
committed
Refactor ActiveProjectHelper to manage current active project state and update usages
1 parent 3e79c74 commit a9c1048

File tree

6 files changed

+72
-31
lines changed

6 files changed

+72
-31
lines changed

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

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,29 @@ import com.intellij.openapi.project.ProjectManager
2020
import com.intellij.openapi.wm.IdeFocusManager
2121

2222
object ActiveProjectHelper {
23-
val activeProject: Project?
24-
get() {
25-
val openProjects: Array<out Project> = ProjectManager.getInstance().openProjects
26-
var activeProject: Project? = null
23+
private var activeProject: Project? = null
2724

28-
for (project in openProjects) {
29-
if (IdeFocusManager.getInstance(project).focusOwner != null) {
30-
activeProject = project
31-
break
32-
}
33-
}
25+
fun setCurrentActiveProject(value: Project?) {
26+
activeProject = value
27+
}
28+
29+
fun getCurrentActiveProject(): Project? {
30+
val initProject = activeProject
31+
val active = getActiveUIProject()
32+
return active ?: initProject
33+
}
34+
35+
private fun getActiveUIProject(): Project? {
36+
val openProjects: Array<out Project> = ProjectManager.getInstance().openProjects
37+
var active: Project? = null
3438

35-
return activeProject
39+
for (project in openProjects) {
40+
if (IdeFocusManager.getInstance(project).focusOwner != null) {
41+
active = project
42+
break
43+
}
3644
}
45+
46+
return active
47+
}
3748
}

src/main/kotlin/org/domaframework/doma/intellij/formatter/SqlFormattingModelBuilder.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,9 @@ import org.domaframework.doma.intellij.setting.state.DomaToolsFormatEnableSettin
3434
class SqlFormattingModelBuilder : FormattingModelBuilder {
3535
override fun createModel(formattingContext: FormattingContext): FormattingModel {
3636
val codeStyleSettings = formattingContext.codeStyleSettings
37-
val project = ActiveProjectHelper.activeProject
37+
val project = ActiveProjectHelper.getCurrentActiveProject()
3838
val setting = project?.let { DomaToolsFormatEnableSettings.getInstance(it) }
3939
val isEnableFormat = setting?.state?.isEnableSqlFormat == true
40-
4140
val spacingBuilder =
4241
if (!isEnableFormat) {
4342
SpacingBuilder(codeStyleSettings, SqlLanguage.INSTANCE)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import javax.swing.JComponent
2424

2525
class DomaToolsConfigurable : Configurable {
2626
private var mySettingsComponent: SettingComponent? = SettingComponent()
27-
private val project = ActiveProjectHelper.activeProject
27+
private val project = ActiveProjectHelper.getCurrentActiveProject()
2828

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

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class SettingComponent {
4444
ToolbarDecorator
4545
.createDecorator(customFunctionClassList)
4646
.setAddAction { actionButton ->
47-
val project = ActiveProjectHelper.activeProject
47+
val project = ActiveProjectHelper.getCurrentActiveProject()
4848
if (project != null) {
4949
val expressionFunction = ExpressionFunctionsHelper
5050
val expressionFunctionInterface =
@@ -87,7 +87,7 @@ class SettingComponent {
8787
.createPanel()
8888

8989
// Disable customFunctionPanel if no active project is available
90-
if (ActiveProjectHelper.activeProject == null) {
90+
if (ActiveProjectHelper.getCurrentActiveProject() == null) {
9191
setEnabledRecursively(customFunctionPanel, false)
9292
}
9393

src/test/kotlin/org/domaframework/doma/intellij/DomaSqlTest.kt

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import com.intellij.psi.PsiClass
2929
import com.intellij.testFramework.PsiTestUtil
3030
import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase
3131
import org.domaframework.doma.intellij.extension.getResourcesSQLFile
32+
import org.domaframework.doma.intellij.setting.SettingComponent
33+
import org.domaframework.doma.intellij.setting.state.DomaToolsCustomFunctionSettings
3234
import org.jetbrains.jps.model.java.JavaResourceRootType
3335
import org.jetbrains.jps.model.java.JavaSourceRootType
3436
import org.junit.Ignore
@@ -97,7 +99,8 @@ open class DomaSqlTest : LightJavaCodeInsightFixtureTestCase() {
9799
}
98100

99101
private fun setUpJdk(module: Module) {
100-
val newJdk = JavaSdk.getInstance().createJdk("Doma Test JDK", System.getProperty("java.home"), false)
102+
val newJdk =
103+
JavaSdk.getInstance().createJdk("Doma Test JDK", System.getProperty("java.home"), false)
101104

102105
WriteAction.runAndWait<RuntimeException> {
103106
ProjectJdkTable.getInstance().addJdk(newJdk)
@@ -181,4 +184,20 @@ open class DomaSqlTest : LightJavaCodeInsightFixtureTestCase() {
181184
assertNotNull("Not Found [$testDaoName]", dao)
182185
return dao
183186
}
187+
188+
protected fun addExpressionJavaFile(fileName: String) {
189+
val file = File("$testDataPath/java/$packagePath/expression/$fileName")
190+
myFixture.addFileToProject(
191+
"main/java/$packagePath/entity/$fileName",
192+
file.readText(),
193+
)
194+
}
195+
196+
protected fun settingCustomFunctions(newClassNames: MutableList<String>) {
197+
val settings = DomaToolsCustomFunctionSettings.getInstance(project)
198+
val component = SettingComponent()
199+
component.customFunctionClassNames = newClassNames.toMutableList()
200+
settings.apply(component)
201+
assertEquals(newClassNames, settings.getState().customFunctionClassNames)
202+
}
184203
}

src/test/kotlin/org/domaframework/doma/intellij/formatter/SqlFormatterTest.kt

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import com.intellij.openapi.command.WriteCommandAction
1919
import com.intellij.psi.codeStyle.CodeStyleManager
2020
import com.intellij.testFramework.fixtures.BasePlatformTestCase
2121
import com.intellij.util.ThrowableRunnable
22+
import org.domaframework.doma.intellij.common.helper.ActiveProjectHelper
23+
import org.domaframework.doma.intellij.setting.SettingComponent
2224
import org.domaframework.doma.intellij.setting.state.DomaToolsFormatEnableSettings
2325

2426
class SqlFormatterTest : BasePlatformTestCase() {
@@ -28,56 +30,66 @@ class SqlFormatterTest : BasePlatformTestCase() {
2830

2931
override fun setUp() {
3032
super.setUp()
31-
val settings = DomaToolsFormatEnableSettings.getInstance(myFixture.project)
32-
settings.state.isEnableSqlFormat = true
33+
settingSqlFormat(true)
34+
if (project != null) {
35+
ActiveProjectHelper.setCurrentActiveProject(project)
36+
}
37+
}
38+
39+
private fun settingSqlFormat(enabled: Boolean) {
40+
val settings = DomaToolsFormatEnableSettings.getInstance(project)
41+
val component = SettingComponent()
42+
component.enableFormat = enabled
43+
settings.apply(component)
44+
assertEquals(enabled, settings.getState().isEnableSqlFormat)
3345
}
3446

3547
override fun tearDown() {
3648
try {
37-
val settings = DomaToolsFormatEnableSettings.getInstance(myFixture.project)
38-
settings.state.isEnableSqlFormat = false
49+
settingSqlFormat(false)
50+
ActiveProjectHelper.setCurrentActiveProject(null)
3951
} finally {
4052
super.tearDown()
4153
}
4254
}
4355

4456
fun testSelectFormatter() {
45-
formatSqlFime("Select.sql", "FormattedSelect.sql")
57+
formatSqlFile("Select.sql", "FormattedSelect.sql")
4658
}
4759

4860
fun testCreateTableFormatter() {
49-
formatSqlFime("CreateTable.sql", "FormattedCreateTable.sql")
61+
formatSqlFile("CreateTable.sql", "FormattedCreateTable.sql")
5062
}
5163

5264
fun testCreateViewFormatter() {
53-
formatSqlFime("CreateView.sql", "FormattedCreateView.sql")
65+
formatSqlFile("CreateView.sql", "FormattedCreateView.sql")
5466
}
5567

5668
fun testInsertFormatter() {
57-
formatSqlFime("Insert.sql", "FormattedInsert.sql")
69+
formatSqlFile("Insert.sql", "FormattedInsert.sql")
5870
}
5971

6072
fun testInsertWithBindVariableFormatter() {
61-
formatSqlFime("InsertWithBindVariable.sql", "FormattedInsertWithBindVariable.sql")
73+
formatSqlFile("InsertWithBindVariable.sql", "FormattedInsertWithBindVariable.sql")
6274
}
6375

6476
fun testUpdateFormatter() {
65-
formatSqlFime("Update.sql", "FormattedUpdate.sql")
77+
formatSqlFile("Update.sql", "FormattedUpdate.sql")
6678
}
6779

6880
fun testUpdateBindVariableFormatter() {
69-
formatSqlFime("UpdateBindVariable.sql", "FormattedUpdateBindVariable.sql")
81+
formatSqlFile("UpdateBindVariable.sql", "FormattedUpdateBindVariable.sql")
7082
}
7183

7284
fun testUpdateTupleAssignmentFormatter() {
73-
formatSqlFime("UpdateTupleAssignment.sql", "FormattedUpdateTupleAssignment.sql")
85+
formatSqlFile("UpdateTupleAssignment.sql", "FormattedUpdateTupleAssignment.sql")
7486
}
7587

7688
fun testDeleteFormatter() {
77-
formatSqlFime("Delete.sql", "FormattedDelete.sql")
89+
formatSqlFile("Delete.sql", "FormattedDelete.sql")
7890
}
7991

80-
private fun formatSqlFime(
92+
private fun formatSqlFile(
8193
beforeFile: String,
8294
afterFile: String,
8395
) {

0 commit comments

Comments
 (0)