Skip to content

Commit fadba72

Browse files
authored
Go plugin fixes (#1851)
* Delete project SDK field from UnitTestBot action dialog. Add auto detection of project SDK and error dialogs when SDK isn't setup or version less than 1.18 * Run plugin action from context menu
1 parent d8daca8 commit fadba72

File tree

4 files changed

+57
-31
lines changed

4 files changed

+57
-31
lines changed

utbot-intellij-go/src/main/kotlin/org/utbot/intellij/plugin/language/go/GoLanguageAssistant.kt

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
package org.utbot.intellij.plugin.language.go
22

3-
import com.goide.psi.GoFile
4-
import com.goide.psi.GoFunctionOrMethodDeclaration
5-
import com.goide.psi.GoMethodDeclaration
6-
import com.goide.psi.GoPointerType
7-
import com.goide.psi.GoStructType
3+
import com.goide.psi.*
84
import com.intellij.lang.Language
95
import com.intellij.openapi.actionSystem.AnActionEvent
106
import com.intellij.openapi.actionSystem.CommonDataKeys
117
import com.intellij.openapi.editor.Editor
128
import com.intellij.psi.PsiElement
139
import com.intellij.psi.PsiFile
1410
import com.intellij.psi.util.PsiTreeUtil
11+
import org.jetbrains.kotlin.idea.util.module
1512
import org.utbot.intellij.plugin.language.agnostic.LanguageAssistant
1613
import org.utbot.intellij.plugin.language.go.generator.GoUtTestsDialogProcessor
1714

@@ -28,9 +25,12 @@ object GoLanguageAssistant : LanguageAssistant() {
2825

2926
override fun actionPerformed(e: AnActionEvent) {
3027
val project = e.project ?: return
28+
val file = e.getData(CommonDataKeys.PSI_FILE) as? GoFile ?: return
29+
val module = file.module ?: return
3130
val (targetFunctions, focusedTargetFunctions) = getPsiTargets(e) ?: return
3231
GoUtTestsDialogProcessor.createDialogAndGenerateTests(
3332
project,
33+
module,
3434
targetFunctions,
3535
focusedTargetFunctions
3636
)
@@ -43,11 +43,13 @@ object GoLanguageAssistant : LanguageAssistant() {
4343
private fun getPsiTargets(e: AnActionEvent): PsiTargets? {
4444
e.project ?: return null
4545

46-
// The action is being called from editor or return. TODO: support other cases instead of return.
47-
val editor = e.getData(CommonDataKeys.EDITOR) ?: return null
48-
46+
val editor = e.getData(CommonDataKeys.EDITOR)
4947
val file = e.getData(CommonDataKeys.PSI_FILE) as? GoFile ?: return null
50-
val element = findPsiElement(file, editor) ?: return null
48+
val element = if (editor != null) {
49+
findPsiElement(file, editor) ?: return null
50+
} else {
51+
e.getData(CommonDataKeys.PSI_ELEMENT) ?: return null
52+
}
5153

5254
val containingFunction = getContainingFunction(element)
5355
val targetFunctions = extractTargetFunctionsOrMethods(file)

utbot-intellij-go/src/main/kotlin/org/utbot/intellij/plugin/language/go/generator/GoUtTestsDialogProcessor.kt

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,74 @@
11
package org.utbot.intellij.plugin.language.go.generator
22

33
import com.goide.psi.GoFunctionOrMethodDeclaration
4+
import com.goide.sdk.GoSdk
5+
import com.goide.sdk.GoSdkService
6+
import com.goide.sdk.GoSdkVersion
47
import com.intellij.openapi.application.runReadAction
8+
import com.intellij.openapi.module.Module
9+
import com.intellij.openapi.options.ShowSettingsUtil
510
import com.intellij.openapi.progress.ProgressIndicator
611
import com.intellij.openapi.progress.ProgressManager
712
import com.intellij.openapi.progress.Task
813
import com.intellij.openapi.project.Project
14+
import com.intellij.openapi.ui.Messages
915
import org.utbot.go.logic.GoUtTestsGenerationConfig
1016
import org.utbot.intellij.plugin.language.go.models.GenerateGoTestsModel
1117
import org.utbot.intellij.plugin.language.go.ui.GenerateGoTestsDialogWindow
18+
import org.utbot.intellij.plugin.language.go.ui.utils.resolveGoExecutablePath
1219

1320
object GoUtTestsDialogProcessor {
1421

1522
fun createDialogAndGenerateTests(
1623
project: Project,
24+
module: Module,
1725
targetFunctions: Set<GoFunctionOrMethodDeclaration>,
1826
focusedTargetFunctions: Set<GoFunctionOrMethodDeclaration>,
1927
) {
20-
val dialogProcessor = createDialog(project, targetFunctions, focusedTargetFunctions)
21-
if (!dialogProcessor.showAndGet()) return
22-
23-
createTests(dialogProcessor.model)
28+
createDialog(project, module, targetFunctions, focusedTargetFunctions)?.let {
29+
if (it.showAndGet()) createTests(it.model)
30+
}
2431
}
2532

2633
private fun createDialog(
2734
project: Project,
35+
module: Module,
2836
targetFunctions: Set<GoFunctionOrMethodDeclaration>,
2937
focusedTargetFunctions: Set<GoFunctionOrMethodDeclaration>,
30-
): GenerateGoTestsDialogWindow {
38+
): GenerateGoTestsDialogWindow? {
39+
val goSdk = GoSdkService.getInstance(project).getSdk(module)
40+
if (goSdk == GoSdk.NULL) {
41+
val result = Messages.showOkCancelDialog(
42+
project,
43+
"GOROOT is not defined. Select it?",
44+
"Unsupported Go SDK",
45+
"Select",
46+
"Cancel",
47+
Messages.getErrorIcon()
48+
)
49+
if (result == Messages.OK) {
50+
ShowSettingsUtil.getInstance().showSettingsDialog(project, "GOROOT")
51+
}
52+
return null
53+
} else if (!goSdk.isValid || GoSdkVersion.fromText(goSdk.version).isLessThan(GoSdkVersion.GO_1_18)) {
54+
val result = Messages.showOkCancelDialog(
55+
project,
56+
"Go SDK isn't valid or version less than 1.18. Select another SDK?",
57+
"Unsupported Go SDK",
58+
"Select",
59+
"Cancel",
60+
Messages.getErrorIcon()
61+
)
62+
if (result == Messages.OK) {
63+
ShowSettingsUtil.getInstance().showSettingsDialog(project, "GOROOT")
64+
}
65+
return null
66+
}
67+
3168
return GenerateGoTestsDialogWindow(
3269
GenerateGoTestsModel(
3370
project,
71+
goSdk.resolveGoExecutablePath()!!,
3472
targetFunctions,
3573
focusedTargetFunctions,
3674
)
@@ -51,8 +89,7 @@ object GoUtTestsDialogProcessor {
5189
)
5290

5391
IntellijGoUtTestsGenerationController(model, indicator).generateTests(
54-
selectedFunctionsNamesBySourceFiles,
55-
testsGenerationConfig
92+
selectedFunctionsNamesBySourceFiles, testsGenerationConfig
5693
) { indicator.isCanceled }
5794
}
5895
})

utbot-intellij-go/src/main/kotlin/org/utbot/intellij/plugin/language/go/models/GenerateGoTestsModel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ import org.utbot.go.logic.GoUtTestsGenerationConfig
1515
*/
1616
data class GenerateGoTestsModel(
1717
val project: Project,
18+
val goExecutableAbsolutePath: String,
1819
val targetFunctions: Set<GoFunctionOrMethodDeclaration>,
1920
val focusedTargetFunctions: Set<GoFunctionOrMethodDeclaration>,
2021
) {
2122
lateinit var selectedFunctions: Set<GoFunctionOrMethodDeclaration>
22-
lateinit var goExecutableAbsolutePath: String
2323
var eachFunctionExecutionTimeoutMillis: Long = GoUtTestsGenerationConfig.DEFAULT_EACH_EXECUTION_TIMEOUT_MILLIS
2424
var allFunctionExecutionTimeoutMillis: Long = GoUtTestsGenerationConfig.DEFAULT_ALL_EXECUTION_TIMEOUT_MILLIS
2525
}

utbot-intellij-go/src/main/kotlin/org/utbot/intellij/plugin/language/go/ui/GenerateGoTestsDialogWindow.kt

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package org.utbot.intellij.plugin.language.go.ui
22

33
import com.goide.psi.GoFunctionOrMethodDeclaration
44
import com.goide.refactor.ui.GoDeclarationInfo
5-
import com.goide.sdk.combobox.GoSdkChooserCombo
65
import com.intellij.openapi.ui.DialogPanel
76
import com.intellij.openapi.ui.DialogWrapper
87
import com.intellij.openapi.ui.ValidationInfo
@@ -13,7 +12,6 @@ import com.intellij.util.ui.JBUI
1312
import com.intellij.util.ui.UIUtil
1413
import org.utbot.go.logic.GoUtTestsGenerationConfig
1514
import org.utbot.intellij.plugin.language.go.models.GenerateGoTestsModel
16-
import org.utbot.intellij.plugin.language.go.ui.utils.resolveGoExecutablePath
1715
import java.text.ParseException
1816
import java.util.concurrent.TimeUnit
1917
import javax.swing.JComponent
@@ -33,7 +31,6 @@ class GenerateGoTestsDialogWindow(val model: GenerateGoTestsModel) : DialogWrapp
3331
this.preferredScrollableViewportSize = JBUI.size(-1, height)
3432
}
3533

36-
private val projectGoSdkField = GoSdkChooserCombo()
3734
private val allFunctionExecutionTimeoutSecondsSpinner =
3835
JBIntSpinner(
3936
TimeUnit.MILLISECONDS.toSeconds(GoUtTestsGenerationConfig.DEFAULT_ALL_EXECUTION_TIMEOUT_MILLIS).toInt(),
@@ -52,17 +49,14 @@ class GenerateGoTestsDialogWindow(val model: GenerateGoTestsModel) : DialogWrapp
5249
private lateinit var panel: DialogPanel
5350

5451
init {
55-
title = "Generate Tests with UtBot"
52+
title = "Generate Tests with UnitTestBot"
5653
isResizable = false
5754
init()
5855
}
5956

6057
override fun createCenterPanel(): JComponent {
6158
panel = panel {
6259
row("Test source root: near to source files") {}
63-
row("Project Go SDK:") {
64-
component(projectGoSdkField)
65-
}
6660
row("Generate test methods for:") {}
6761
row {
6862
scrollPane(targetFunctionsTable)
@@ -82,7 +76,6 @@ class GenerateGoTestsDialogWindow(val model: GenerateGoTestsModel) : DialogWrapp
8276

8377
override fun doOKAction() {
8478
model.selectedFunctions = targetFunctionsTable.selectedMemberInfos.fromInfos()
85-
model.goExecutableAbsolutePath = projectGoSdkField.sdk.resolveGoExecutablePath()!!
8679
try {
8780
eachFunctionExecutionTimeoutMillisSpinner.commitEdit()
8881
allFunctionExecutionTimeoutSecondsSpinner.commitEdit()
@@ -119,12 +112,6 @@ class GenerateGoTestsDialogWindow(val model: GenerateGoTestsModel) : DialogWrapp
119112

120113
@Suppress("DuplicatedCode") // This method is highly inspired by GenerateTestsDialogWindow.doValidate().
121114
override fun doValidate(): ValidationInfo? {
122-
projectGoSdkField.sdk.resolveGoExecutablePath()
123-
?: return ValidationInfo(
124-
"Go SDK is not configured",
125-
projectGoSdkField.childComponent
126-
)
127-
128115
targetFunctionsTable.tableHeader?.background = UIUtil.getTableBackground()
129116
targetFunctionsTable.background = UIUtil.getTableBackground()
130117
if (targetFunctionsTable.selectedMemberInfos.isEmpty()) {

0 commit comments

Comments
 (0)