Skip to content

Commit

Permalink
refactor(test): add line comments to related classes and current class
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
phodal committed Jan 16, 2024
1 parent 8123892 commit 2f11110
Showing 1 changed file with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 2f11110

Please sign in to comment.