Skip to content

Commit

Permalink
fix(devins-cpp): move test config for Intellij IDEA 223 only, which i…
Browse files Browse the repository at this point in the history
…s C++ test configurations and test discovery #100

This commit introduces a new class, `CppAutoTestService`, which provides support for C++ test configurations and test discovery within the IntelliJ platform. The `CppAutoTestService` class is responsible for creating and managing test files for C++ source files, as well as identifying relevant test classes and methods.

The commit also includes a rename of the `CppAutoTestService.kt` file to `CppAutoTestService.kt` within the `233` directory, reflecting a new version or configuration specific to C++ development. Additionally, the `build.gradle.kts` file has been modified to include the `com.intellij.nativeDebug` plugin for C++ debugging support when the platform version is set to 233.

These changes are part of the ongoing effort to enhance the C++ development experience within the IntelliJ platform, specifically for version 233.
  • Loading branch information
phodal committed Mar 25, 2024
1 parent 9dd5c48 commit fb588e3
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 3 deletions.
8 changes: 6 additions & 2 deletions build.gradle.kts
Expand Up @@ -70,14 +70,14 @@ val clionPlugins = listOf(
prop("rustPlugin"),
"org.toml.lang"
)
val cppPlugins = listOf(
var cppPlugins: List<String> = listOf(
"com.intellij.cidr.lang",
"com.intellij.clion",
"com.intellij.cidr.base",
"com.intellij.nativeDebug",
"org.jetbrains.plugins.clion.test.google",
"org.jetbrains.plugins.clion.test.catch"
)

val rustPlugins = listOf(
prop("rustPlugin"),
"org.toml.lang"
Expand Down Expand Up @@ -517,6 +517,10 @@ project(":rust") {
}

project(":cpp") {
if (platformVersion == 233) {
cppPlugins += "com.intellij.nativeDebug"
}

intellij {
version.set(clionVersion)
plugins.set(cppPlugins)
Expand Down
@@ -0,0 +1,61 @@
package cc.unitmesh.cpp.provider.testing

import cc.unitmesh.cpp.util.CppContextPrettify
import cc.unitmesh.devti.context.ClassContext
import cc.unitmesh.devti.provider.AutoTestService
import cc.unitmesh.devti.provider.context.TestFileContext
import com.intellij.execution.configurations.RunConfiguration
import com.intellij.execution.configurations.RunProfile
import com.intellij.openapi.application.WriteAction
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.guessProjectDir
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.jetbrains.cidr.lang.OCLanguage
import com.jetbrains.cidr.lang.psi.OCFunctionDeclaration
import java.io.File

class CppAutoTestService : AutoTestService() {
// TODO in Cpp233 and Cpp222 the RunProfile is different, maybe we can use the same RunProfile in future
override fun runConfigurationClass(project: Project): Class<out RunProfile>? = null
override fun isApplicable(element: PsiElement): Boolean = element.language is OCLanguage

override fun findOrCreateTestFile(sourceFile: PsiFile, project: Project, element: PsiElement): TestFileContext? {
// 1. check project root test folder, if not exist, create it
val baseDir = project.guessProjectDir() ?: return null

val sourceVirtualFile = sourceFile.virtualFile

val testFilePath = getTestFilePath(sourceVirtualFile)
val testFile = WriteAction.computeAndWait<VirtualFile?, Throwable> {
baseDir.findOrCreateChildData(this, testFilePath)
} ?: return null

val currentClass = when (element) {
is OCFunctionDeclaration -> {
CppContextPrettify.printParentStructure(element)
}

else -> null
}

val relatedClasses = lookupRelevantClass(project, element)

return TestFileContext(
true,
testFile,
relatedClasses,
"",
sourceFile.language,
currentClass,
emptyList()
)
}

private fun getTestFilePath(file: VirtualFile) = file.nameWithoutExtension + "_test" + "." + file.extension

override fun lookupRelevantClass(project: Project, element: PsiElement): List<ClassContext> {
return listOf()
}
}
Expand Up @@ -18,7 +18,6 @@ import com.jetbrains.cidr.lang.psi.OCFunctionDeclaration
import java.io.File

class CppAutoTestService : AutoTestService() {
// TODO in Cpp233 and Cpp222 the RunProfile is different, maybe we can use the same RunProfile in future
override fun runConfigurationClass(project: Project): Class<out RunProfile>? = null
override fun isApplicable(element: PsiElement): Boolean = element.language is OCLanguage

Expand Down

0 comments on commit fb588e3

Please sign in to comment.