|
| 1 | +package cc.unitmesh.go.provider.testing |
| 2 | + |
| 3 | +import cc.unitmesh.devti.context.ClassContext |
| 4 | +import cc.unitmesh.devti.provider.WriteTestService |
| 5 | +import cc.unitmesh.devti.provider.context.TestFileContext |
| 6 | +import com.goide.execution.testing.GoTestRunConfiguration |
| 7 | +import com.goide.psi.GoFile |
| 8 | +import com.goide.psi.GoFunctionOrMethodDeclaration |
| 9 | +import com.intellij.execution.configurations.RunProfile |
| 10 | +import com.intellij.openapi.project.Project |
| 11 | +import com.intellij.openapi.roots.TestSourcesFilter |
| 12 | +import com.intellij.psi.PsiElement |
| 13 | +import com.intellij.psi.PsiFile |
| 14 | +import com.intellij.psi.util.PsiTreeUtil |
| 15 | +import com.intellij.testIntegration.TestFinderHelper |
| 16 | +import com.intellij.util.PlatformUtils |
| 17 | + |
| 18 | +class GoWriteTestService : WriteTestService() { |
| 19 | + override fun isApplicable(element: PsiElement): Boolean = PlatformUtils.isGoIde() |
| 20 | + override fun runConfigurationClass(project: Project): Class<out RunProfile> = GoTestRunConfiguration::class.java |
| 21 | + |
| 22 | + override fun lookupRelevantClass(project: Project, element: PsiElement): List<ClassContext> { |
| 23 | + TODO("Not yet implemented") |
| 24 | + } |
| 25 | + |
| 26 | + override fun findOrCreateTestFile(sourceFile: PsiFile, project: Project, element: PsiElement): TestFileContext? { |
| 27 | + TODO("Not yet implemented") |
| 28 | + } |
| 29 | + |
| 30 | + fun getElementForTests(elementAtCaret: PsiElement): PsiElement? { |
| 31 | + val parent = PsiTreeUtil.getParentOfType(elementAtCaret, GoFunctionOrMethodDeclaration::class.java, false) |
| 32 | + if (parent == null) { |
| 33 | + val goFile: GoFile = elementAtCaret as? GoFile ?: return null |
| 34 | + return if (goFile.functions.isNotEmpty() || goFile.methods.isNotEmpty()) { |
| 35 | + goFile |
| 36 | + } else { |
| 37 | + null |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + val virtualFile = elementAtCaret.containingFile?.virtualFile ?: return null |
| 42 | + |
| 43 | + val project = elementAtCaret.project |
| 44 | + if (TestSourcesFilter.isTestSources(virtualFile, project) || TestFinderHelper.isTest(parent)) return null |
| 45 | + |
| 46 | + return parent |
| 47 | + } |
| 48 | + |
| 49 | +} |
0 commit comments