Skip to content

Commit

Permalink
feat: add support for java comments
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Dec 29, 2023
1 parent 3558b82 commit 8f0b49d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package cc.unitmesh.pick.builder

import cc.unitmesh.core.SupportedLang
import cc.unitmesh.core.comment.DocInstruction
import cc.unitmesh.core.completion.TypedIns
import cc.unitmesh.core.completion.TypedInsBuilder
import cc.unitmesh.pick.builder.comment.KotlinCommentBuilder
import cc.unitmesh.pick.builder.comment.JvmCommentBuilder
import cc.unitmesh.pick.worker.job.JobContext
import chapi.domain.core.CodeContainer

class DocumentationTypedInsBuilder(val context: JobContext) : TypedInsBuilder {
private val kotlinCommentBuilder = KotlinCommentBuilder()
private val kotlinCommentBuilder = JvmCommentBuilder("kotlin")
private val javaCommentBuilder = JvmCommentBuilder("java", DocInstruction.JAVA)

override fun build(container: CodeContainer): List<TypedIns> {
val language = context.project.language
return when (language) {
SupportedLang.JAVA -> kotlinCommentBuilder.build(context.job.code, container)
SupportedLang.TYPESCRIPT -> TODO()
SupportedLang.JAVA -> javaCommentBuilder.build(context.job.code, container)
SupportedLang.KOTLIN -> {
kotlinCommentBuilder.build(context.job.code, container)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import chapi.domain.core.CodePosition

private const val DOC_THRESHOLD = 5

class KotlinCommentBuilder : CommentBuilder {
override val docInstruction: DocInstruction = DocInstruction.KOTLIN
class JvmCommentBuilder(
val language: String,
override val docInstruction: DocInstruction = DocInstruction.KOTLIN,
) : CommentBuilder {

override fun build(code: String, container: CodeContainer): List<TypedCommentIns> {
val posComments = try {
Expand All @@ -29,7 +31,7 @@ class KotlinCommentBuilder : CommentBuilder {

container.DataStructures.forEach { dataStruct ->
val classComment = startLineCommentMap[dataStruct.Position.StartLine - 1]
classComment?.let { comments.add(ClassCommentIns(docInstruction, dataStruct, it, language = "kotlin")) }
classComment?.let { comments.add(ClassCommentIns(docInstruction, dataStruct, it, language = language)) }

dataStruct.Functions
.filter { it.Name != "constructor" && it.Name != "PrimaryConstructor" }
Expand All @@ -42,7 +44,7 @@ class KotlinCommentBuilder : CommentBuilder {
function,
it,
dataStruct,
language = "kotlin"
language = language
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@ data class MethodCommentIns(

override fun unique(): Instruction {
val instruction = "Write ${docInstruction.value} for given method " + function.Name + " .\n"
val input =
"### Current class:\n" + currentDataStruct.toUml() + "\n###\n" + "Code:\n```$language\n" + currentDataStruct.Content + "\n```"
val input = """### Current class:
${currentDataStruct.toUml()}
###
Code:
```$language
${currentDataStruct.Content}
```
"""
val output = comment.content

return Instruction(instruction, input, output)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import chapi.ast.kotlinast.KotlinAnalyser
import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Test

class KotlinCommentBuilderTest {
class JvmCommentBuilderTest {
// Given
private val kotlinCode = """
/**
Expand Down Expand Up @@ -35,7 +35,7 @@ class Group<T>(val name: String) {
@Test
fun `should extract KDoc comments when valid code provided`() {
// When
val result = KotlinCommentBuilder().extractKdocComments(kotlinCode)
val result = JvmCommentBuilder("kotlin").extractKdocComments(kotlinCode)

// Then
result.size shouldBe 3
Expand Down Expand Up @@ -64,7 +64,7 @@ class Group<T>(val name: String) {
val codeContainer = KotlinAnalyser().analysis(kotlinCode, "test.kt")

// When
val result = KotlinCommentBuilder().build(kotlinCode, codeContainer)
val result = JvmCommentBuilder("kotlin").build(kotlinCode, codeContainer)

// Then
result.size shouldBe 3
Expand Down

0 comments on commit 8f0b49d

Please sign in to comment.