Skip to content

Commit

Permalink
feat(cpp): add CppCodeModifier class
Browse files Browse the repository at this point in the history
This commit adds a new class called CppCodeModifier to the cpp module. The CppCodeModifier class implements the CodeModifier interface and provides methods for inserting test code into C++ files. The class is applicable to files written in the OCLanguage. The insertTestCode, insertMethod, and insertClass methods are implemented to insert test code at the end of the file, after the last child element, and at the end of the document, respectively.
  • Loading branch information
phodal committed Jan 17, 2024
1 parent 89cda73 commit 5b374c4
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
Expand Up @@ -19,7 +19,7 @@ class CLionWorkspaceContextProvider : ChatContextProvider {
)

override fun isApplicable(project: Project, creationContext: ChatCreationContext): Boolean {
return creationContext.sourceFile?.language == OCLanguage.getInstance()
return creationContext.sourceFile?.language is OCLanguage
}

override suspend fun collect(project: Project, creationContext: ChatCreationContext): List<ChatContextItem> {
Expand Down Expand Up @@ -66,7 +66,7 @@ class CLionWorkspaceContextProvider : ChatContextProvider {
return null
}

cmakeWorkspace.cMakeDependencyFiles.forEach { file ->
cmakeWorkspace.cMakeResourceFiles.forEach { file ->
val text = file.readText()
if (text.contains("gtest") || text.contains("gmock")) {
return ChatContextItem(
Expand Down
51 changes: 51 additions & 0 deletions cpp/src/main/kotlin/cc/unitmesh/cpp/provider/CppCodeModifier.kt
@@ -0,0 +1,51 @@
package cc.unitmesh.cpp.provider

import cc.unitmesh.devti.context.builder.CodeModifier
import com.intellij.lang.Language
import com.intellij.openapi.command.WriteCommandAction
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.PsiManager
import com.jetbrains.cidr.execution.debugger.OCDebuggerTypesHelper
import com.jetbrains.cidr.lang.OCLanguage
import com.jetbrains.cidr.lang.parser.OCElementType
import com.jetbrains.cidr.lang.psi.OCFile
import com.jetbrains.cidr.lang.util.OCElementFactory

class CppCodeModifier : CodeModifier {
override fun isApplicable(language: Language): Boolean = language is OCLanguage

override fun insertTestCode(sourceFile: VirtualFile, project: Project, code: String): Boolean {
val isExit = sourceFile as? OCFile
if (isExit == null) {
insertClass(sourceFile, project, code)
return true
}

insertMethod(sourceFile, project, code)
return true
}

override fun insertMethod(sourceFile: VirtualFile, project: Project, code: String): Boolean {
val file = sourceFile as OCFile
val psiElement = file.lastChild

val codeElement = OCElementFactory.expressionOrStatementsCodeFragment(code, project, file, true, false)

com.intellij.openapi.application.runReadAction {
psiElement?.parent?.addAfter(codeElement, psiElement)
}

return true
}

override fun insertClass(sourceFile: VirtualFile, project: Project, code: String): Boolean {
WriteCommandAction.runWriteCommandAction(project) {
val psiFile = PsiManager.getInstance(project).findFile(sourceFile) as OCFile
val document = psiFile.viewProvider.document!!
document.insertString(document.textLength, code)
}

return true
}
}
4 changes: 4 additions & 0 deletions cpp/src/main/resources/cc.unitmesh.cpp.xml
Expand Up @@ -15,5 +15,9 @@

<chatContextProvider implementation="cc.unitmesh.cpp.provider.CLionWorkspaceContextProvider"/>
<testContextProvider language="ObjectiveC" implementation="cc.unitmesh.cpp.provider.testing.CppWriteTestService"/>

<codeModifier
language="ObjectiveC"
implementationClass="cc.unitmesh.cpp.provider.CppCodeModifier"/>
</extensions>
</idea-plugin>

0 comments on commit 5b374c4

Please sign in to comment.