Skip to content

Commit aa875ec

Browse files
committed
feat(go): add GoVersionChatContextProvider and GoWriteTestService
- Add `com.intellij.openapi.application.ReadAction` and `com.intellij.openapi.application.runReadAction` imports to `GoVersionChatContextProvider.kt` for better performance. - Wrap the code inside `ReadAction.compute` to execute it in a read action. - Update the return statement to return the result of `ReadAction.compute`. - Add `GoVersionChatContextProvider` class to provide chat context for Go versions. - Add `GoWriteTestService` class to provide test file context for Go tests. - Implement `runConfigurationClass` and `isApplicable` methods in `JavaWriteTestService.kt` using expression
1 parent 2eef8d1 commit aa875ec

File tree

5 files changed

+84
-21
lines changed

5 files changed

+84
-21
lines changed

.idea/runConfigurations/RunGoland.xml

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ val webstormVersion = prop("webstormVersion")
9191
val baseVersion = when (baseIDE) {
9292
"idea" -> ideaVersion
9393
"pycharm" -> pycharmVersion
94-
"go" -> golandVersion
94+
"goland" -> golandVersion
9595
"clion" -> clionVersion
9696
"rider" -> riderVersion
9797
"javascript" -> webstormVersion

goland/src/main/kotlin/cc/unitmesh/go/provider/GoVersionChatContextProvider.kt

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import com.goide.psi.GoFile
77
import com.goide.sdk.GoSdkService
88
import com.goide.sdk.GoTargetSdkVersionProvider
99
import com.goide.util.GoUtil
10+
import com.intellij.openapi.application.ReadAction
11+
import com.intellij.openapi.application.runReadAction
1012
import com.intellij.openapi.project.Project
1113

1214
class GoVersionChatContextProvider : ChatContextProvider {
@@ -17,15 +19,17 @@ class GoVersionChatContextProvider : ChatContextProvider {
1719
override suspend fun collect(project: Project, creationContext: ChatCreationContext): List<ChatContextItem> {
1820
val sourceFile = creationContext.sourceFile ?: return emptyList()
1921

20-
val goVersion = GoSdkService.getInstance(project).getSdk(GoUtil.module(sourceFile)).version
21-
val targetVersion = GoTargetSdkVersionProvider.getTargetGoSdkVersion(sourceFile).toString()
22+
return ReadAction.compute<List<ChatContextItem>, Throwable> {
23+
val goVersion = GoSdkService.getInstance(project).getSdk(GoUtil.module(sourceFile)).version
24+
val targetVersion = GoTargetSdkVersionProvider.getTargetGoSdkVersion(sourceFile).toString()
2225

23-
return listOf(
24-
ChatContextItem(
25-
GoVersionChatContextProvider::class,
26-
"Go Version: $goVersion, Target Version: $targetVersion"
26+
listOf(
27+
ChatContextItem(
28+
GoVersionChatContextProvider::class,
29+
"Go Version: $goVersion, Target Version: $targetVersion"
30+
)
2731
)
28-
)
32+
}
2933
}
3034
}
3135

goland/src/main/kotlin/cc/unitmesh/go/provider/testing/GoWriteTestService.kt

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ package cc.unitmesh.go.provider.testing
33
import cc.unitmesh.devti.context.ClassContext
44
import cc.unitmesh.devti.provider.WriteTestService
55
import cc.unitmesh.devti.provider.context.TestFileContext
6+
import cc.unitmesh.go.context.GoMethodContextBuilder
7+
import cc.unitmesh.go.context.GoStructContextBuilder
8+
import cc.unitmesh.go.util.GoPsiUtil
69
import com.goide.execution.testing.GoTestRunConfiguration
7-
import com.goide.psi.GoFile
8-
import com.goide.psi.GoFunctionOrMethodDeclaration
10+
import com.goide.psi.*
911
import com.intellij.execution.configurations.RunProfile
12+
import com.intellij.openapi.application.runReadAction
1013
import com.intellij.openapi.project.Project
1114
import com.intellij.openapi.roots.TestSourcesFilter
1215
import com.intellij.psi.PsiElement
@@ -20,11 +23,51 @@ class GoWriteTestService : WriteTestService() {
2023
override fun runConfigurationClass(project: Project): Class<out RunProfile> = GoTestRunConfiguration::class.java
2124

2225
override fun lookupRelevantClass(project: Project, element: PsiElement): List<ClassContext> {
23-
TODO("Not yet implemented")
26+
return listOf()
2427
}
2528

2629
override fun findOrCreateTestFile(sourceFile: PsiFile, project: Project, element: PsiElement): TestFileContext? {
27-
TODO("Not yet implemented")
30+
val underTestElement = getElementForTests(element) ?: return null
31+
val name = GoPsiUtil.getDeclarationName(underTestElement) ?: return null
32+
val testFileName = toTestFileName(name)
33+
val underTestFile = underTestElement.containingFile as? GoFile ?: return null
34+
35+
val relatedModels = lookupRelevantClass(project, underTestElement).distinctBy { it.name }
36+
37+
val imports = runReadAction {
38+
val importList = PsiTreeUtil.getChildrenOfTypeAsList(underTestFile, GoImportDeclaration::class.java)
39+
importList.map { it.text }
40+
}
41+
42+
val currentObject = when (underTestElement) {
43+
is GoTypeDeclaration,
44+
is GoTypeSpec -> {
45+
GoStructContextBuilder().getClassContext(underTestElement, false)?.format()
46+
}
47+
48+
is GoFunctionOrMethodDeclaration -> GoMethodContextBuilder().getMethodContext(
49+
underTestElement,
50+
false,
51+
false
52+
)
53+
?.format()
54+
55+
else -> null
56+
}
57+
58+
return TestFileContext(
59+
true,
60+
underTestFile.virtualFile,
61+
relatedModels,
62+
testFileName,
63+
underTestFile.language,
64+
currentObject,
65+
imports
66+
)
67+
}
68+
69+
fun toTestFileName(underTestFileName: String): String {
70+
return underTestFileName + "_test.go"
2871
}
2972

3073
fun getElementForTests(elementAtCaret: PsiElement): PsiElement? {

java/src/main/kotlin/cc/unitmesh/idea/service/JavaWriteTestService.kt

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,13 @@ import org.jetbrains.plugins.gradle.service.execution.GradleRunConfiguration
2222
import java.io.File
2323

2424
class JavaWriteTestService : WriteTestService() {
25-
override fun runConfigurationClass(project: Project): Class<out RunProfile> {
26-
return GradleRunConfiguration::class.java
27-
}
28-
29-
override fun isApplicable(element: PsiElement): Boolean {
30-
return element.language is JavaLanguage
31-
}
25+
override fun runConfigurationClass(project: Project): Class<out RunProfile> = GradleRunConfiguration::class.java
26+
override fun isApplicable(element: PsiElement): Boolean = element.language is JavaLanguage
3227

3328
override fun findOrCreateTestFile(sourceFile: PsiFile, project: Project, element: PsiElement): TestFileContext? {
3429
val sourceFilePath = sourceFile.virtualFile
3530
val parentDir = sourceFilePath.parent
36-
val className = sourceFile.name.replace(".java", "") + "Test"
31+
val testFileName = sourceFile.name.replace(".java", "") + "Test"
3732

3833
val packageName = ReadAction.compute<String, Throwable> {
3934
(sourceFile as PsiJavaFile).packageName
@@ -89,7 +84,7 @@ class JavaWriteTestService : WriteTestService() {
8984
}
9085

9186
return if (testFile != null) {
92-
TestFileContext(isNewFile, testFile, relatedModels, className, sourceFile.language, null, imports)
87+
TestFileContext(isNewFile, testFile, relatedModels, testFileName, sourceFile.language, null, imports)
9388
} else {
9489
val targetFile = createTestFile(sourceFile, testDir!!, packageName, project)
9590
TestFileContext(isNewFile = true, targetFile, relatedModels, "", sourceFile.language, null, imports)

0 commit comments

Comments
 (0)