generated from JetBrains/intellij-platform-plugin-template
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
3 changed files
with
57 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
cpp/src/main/kotlin/cc/unitmesh/cpp/provider/CppCodeModifier.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters