Skip to content

Commit

Permalink
feat(comment): add handle for kotlin comment #1
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Dec 29, 2023
1 parent 032cf3c commit ebd5610
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 14 deletions.
10 changes: 10 additions & 0 deletions unit-core/src/main/kotlin/cc/unitmesh/core/comment/CodeComment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package cc.unitmesh.core.comment

import chapi.domain.core.CodePosition
import kotlinx.serialization.Serializable

@Serializable
data class CodeComment(
val content: String,
val position: CodePosition
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cc.unitmesh.core.comment

import cc.unitmesh.core.completion.TypedIns
import chapi.domain.core.CodeContainer
import chapi.domain.core.CodeDataStruct

/**
* The CommentBuilder interface represents a builder for generating comments in code.
Expand Down Expand Up @@ -34,16 +35,5 @@ interface CommentBuilder {
/// for generate instruction
val docInstruction: DocInstruction
fun build(container: CodeContainer): List<TypedIns>
fun build(codeDataStruct: CodeDataStruct): List<TypedIns>
}


enum class DocInstruction(val value: String) {
CPP("doxygen"),
JAVA("javadoc"),
JAVASCRIPT("JSDoc"),
PHP("PHPDoc"),
GO("Go Doc"),
RUBY("YARD documentation"),
KOTLIN("YARD KDoc")
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package cc.unitmesh.core.comment

enum class DocInstruction(val value: String) {
CPP("doxygen"),
JAVA("javadoc"),
JAVASCRIPT("JSDoc"),
PHP("PHPDoc"),
GO("Go Doc"),
RUBY("YARD documentation"),
KOTLIN("YARD KDoc")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package cc.unitmesh.core.comment

import cc.unitmesh.core.completion.CompletionBuilderType
import cc.unitmesh.core.completion.TypedIns

abstract class TypedCommentIns : TypedIns {
override val type: CompletionBuilderType = CompletionBuilderType.DOCUMENTATION
abstract val builderLevel: CommentBuilderType
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,53 @@
package cc.unitmesh.pick.builder.comment

import cc.unitmesh.core.comment.CodeComment
import cc.unitmesh.core.comment.CommentBuilder
import cc.unitmesh.core.comment.DocInstruction
import cc.unitmesh.core.comment.TypedCommentIns
import cc.unitmesh.core.completion.TypedIns
import chapi.domain.core.CodeContainer
import chapi.domain.core.CodeDataStruct
import chapi.domain.core.CodePosition

class KotlinCommentBuilder : CommentBuilder {
override val commentStart: String = "/**"
override val commentEnd: String = "*/"
override val docInstruction: DocInstruction = DocInstruction.KOTLIN

override fun build(container: CodeContainer): List<TypedIns> {
TODO("Not yet implemented")
override fun build(container: CodeContainer): List<TypedCommentIns> {
return listOf()
}

override fun build(codeDataStruct: CodeDataStruct): List<TypedIns> {
return listOf()
}

companion object {
/**
* Extracts the Kotlin documentation comments (KDoc) from the given code.
*
* @param code the Kotlin code from which to extract the KDoc comments
* @return a list of pairs, where each pair contains the line number and the extracted KDoc comment
*/
fun extractKdocComments(code: String): List<CodeComment> {
val pattern = Regex("""\s+/\*\*([^*]|(\*+[^*/]))*\*+/""")
val matches = pattern.findAll(code)

val comments = mutableListOf<CodeComment>()

for (match in matches) {
val commentContent = match.value.trimIndent()
val startLine = code.substring(0, match.range.first).count { it == '\n' } + 1
val stopLine = code.substring(0, match.range.last).count { it == '\n' } + 1
val startLinePosition = match.range.first - code.lastIndexOf('\n', match.range.first) - 1
val stopLinePosition = match.range.last - code.lastIndexOf('\n', match.range.last) - 1

val position = CodePosition(startLine, startLinePosition, stopLine, stopLinePosition)
val comment = CodeComment(commentContent, position)
comments.add(comment)
}

return comments
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package cc.unitmesh.pick.builder.comment

import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.Assertions.assertEquals

class KotlinCommentBuilderTest {

@Test
fun `should extract KDoc comments when valid code provided`() {
// Given
val kotlinCode = """
/**
* A group of *members*.
*
* This class has no useful logic; it's just a documentation example.
*
* @param T the type of a member in this group.
* @property name the name of this group.
* @constructor Creates an empty group.
*/
class Group<T>(val name: String) {
/**
* Adds a [member] to this group.
* @return the new size of the group.
*/
fun add(member: T): Int { ... }
/**
* Another function with KDoc.
*/
fun anotherFunction() { ... }
}
"""

// When
val result = KotlinCommentBuilder.extractKdocComments(kotlinCode)

// Then
result.size shouldBe 3
result[0].content shouldBe """
/**
* A group of *members*.
*
* This class has no useful logic; it's just a documentation example.
*
* @param T the type of a member in this group.
* @property name the name of this group.
* @constructor Creates an empty group.
*/
""".trimIndent()
result[1].content shouldBe """
/**
* Adds a [member] to this group.
* @return the new size of the group.
*/
""".trimIndent()
}
}

0 comments on commit ebd5610

Please sign in to comment.