Skip to content

Commit 5b374c4

Browse files
committed
feat(cpp): add CppCodeModifier class
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.
1 parent 89cda73 commit 5b374c4

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

cpp/src/main/kotlin/cc/unitmesh/cpp/provider/CLionWorkspaceContextProvider.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class CLionWorkspaceContextProvider : ChatContextProvider {
1919
)
2020

2121
override fun isApplicable(project: Project, creationContext: ChatCreationContext): Boolean {
22-
return creationContext.sourceFile?.language == OCLanguage.getInstance()
22+
return creationContext.sourceFile?.language is OCLanguage
2323
}
2424

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

69-
cmakeWorkspace.cMakeDependencyFiles.forEach { file ->
69+
cmakeWorkspace.cMakeResourceFiles.forEach { file ->
7070
val text = file.readText()
7171
if (text.contains("gtest") || text.contains("gmock")) {
7272
return ChatContextItem(
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package cc.unitmesh.cpp.provider
2+
3+
import cc.unitmesh.devti.context.builder.CodeModifier
4+
import com.intellij.lang.Language
5+
import com.intellij.openapi.command.WriteCommandAction
6+
import com.intellij.openapi.project.Project
7+
import com.intellij.openapi.vfs.VirtualFile
8+
import com.intellij.psi.PsiManager
9+
import com.jetbrains.cidr.execution.debugger.OCDebuggerTypesHelper
10+
import com.jetbrains.cidr.lang.OCLanguage
11+
import com.jetbrains.cidr.lang.parser.OCElementType
12+
import com.jetbrains.cidr.lang.psi.OCFile
13+
import com.jetbrains.cidr.lang.util.OCElementFactory
14+
15+
class CppCodeModifier : CodeModifier {
16+
override fun isApplicable(language: Language): Boolean = language is OCLanguage
17+
18+
override fun insertTestCode(sourceFile: VirtualFile, project: Project, code: String): Boolean {
19+
val isExit = sourceFile as? OCFile
20+
if (isExit == null) {
21+
insertClass(sourceFile, project, code)
22+
return true
23+
}
24+
25+
insertMethod(sourceFile, project, code)
26+
return true
27+
}
28+
29+
override fun insertMethod(sourceFile: VirtualFile, project: Project, code: String): Boolean {
30+
val file = sourceFile as OCFile
31+
val psiElement = file.lastChild
32+
33+
val codeElement = OCElementFactory.expressionOrStatementsCodeFragment(code, project, file, true, false)
34+
35+
com.intellij.openapi.application.runReadAction {
36+
psiElement?.parent?.addAfter(codeElement, psiElement)
37+
}
38+
39+
return true
40+
}
41+
42+
override fun insertClass(sourceFile: VirtualFile, project: Project, code: String): Boolean {
43+
WriteCommandAction.runWriteCommandAction(project) {
44+
val psiFile = PsiManager.getInstance(project).findFile(sourceFile) as OCFile
45+
val document = psiFile.viewProvider.document!!
46+
document.insertString(document.textLength, code)
47+
}
48+
49+
return true
50+
}
51+
}

cpp/src/main/resources/cc.unitmesh.cpp.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,9 @@
1515

1616
<chatContextProvider implementation="cc.unitmesh.cpp.provider.CLionWorkspaceContextProvider"/>
1717
<testContextProvider language="ObjectiveC" implementation="cc.unitmesh.cpp.provider.testing.CppWriteTestService"/>
18+
19+
<codeModifier
20+
language="ObjectiveC"
21+
implementationClass="cc.unitmesh.cpp.provider.CppCodeModifier"/>
1822
</extensions>
1923
</idea-plugin>

0 commit comments

Comments
 (0)