From 2f111101a7946d4ebb53edba54548935ea4ca4ab Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Tue, 16 Jan 2024 09:27:41 +0800 Subject: [PATCH] refactor(test): add line comments to related classes and current class Add line comments to the related classes and current class in the TestCodeGenTask.kt file. This is done by retrieving the line comment prefix for the file's language and using it to format the comments. The related classes are preceded by the comment "// here are related classes:", and the current class is preceded by the comment "// here is current class information:". The comments are added to the prompter string. --- .../intentions/action/task/TestCodeGenTask.kt | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/cc/unitmesh/devti/intentions/action/task/TestCodeGenTask.kt b/src/main/kotlin/cc/unitmesh/devti/intentions/action/task/TestCodeGenTask.kt index a5f260d83a..318c199288 100644 --- a/src/main/kotlin/cc/unitmesh/devti/intentions/action/task/TestCodeGenTask.kt +++ b/src/main/kotlin/cc/unitmesh/devti/intentions/action/task/TestCodeGenTask.kt @@ -10,6 +10,7 @@ import cc.unitmesh.devti.provider.WriteTestService import cc.unitmesh.devti.provider.context.* import cc.unitmesh.devti.statusbar.AutoDevStatus import cc.unitmesh.devti.statusbar.AutoDevStatusService +import com.intellij.lang.LanguageCommenters import com.intellij.openapi.application.ApplicationManager import com.intellij.openapi.application.ReadAction import com.intellij.openapi.application.runReadAction @@ -32,6 +33,9 @@ class TestCodeGenTask(val request: TestCodeGenRequest) : private val lang = request.file.language.displayName private val writeTestService = WriteTestService.context(request.element) + val commenter = LanguageCommenters.INSTANCE.forLanguage(request.file.language) ?: null + val comment = commenter?.lineCommentPrefix ?: "//" + override fun run(indicator: ProgressIndicator) { indicator.isIndeterminate = true indicator.fraction = 0.1 @@ -76,21 +80,21 @@ class TestCodeGenTask(val request: TestCodeGenRequest) : val relatedClasses = testContext.relatedClasses.joinToString("\n") { it.format() }.lines().joinToString("\n") { - "// $it" + "$comment $it" } - "// here are related classes:\n$relatedClasses\n" + "$comment here are related classes:\n$relatedClasses\n" } if (testContext.currentClass != null) { val currentClassInfo = runReadAction { testContext.currentClass.format() }.lines().joinToString("\n") { - "// $it" + "$comment $it" } - prompter += "\n// here is current class information:\n$currentClassInfo\n" + prompter += "\n$comment here is current class information:\n$currentClassInfo\n" } val importString = testContext.imports.joinToString("\n") { - "// $it" + "$comment $it" } prompter += "\nCode:\n$importString\n```${lang.lowercase()}\n${request.selectText}\n```\n"