-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(comment): add handle for kotlin comment #1
- Loading branch information
Showing
6 changed files
with
130 additions
and
14 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
unit-core/src/main/kotlin/cc/unitmesh/core/comment/CodeComment.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,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 | ||
) |
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
11 changes: 11 additions & 0 deletions
11
unit-core/src/main/kotlin/cc/unitmesh/core/comment/DocInstruction.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,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") | ||
} |
9 changes: 9 additions & 0 deletions
9
unit-core/src/main/kotlin/cc/unitmesh/core/comment/TypedCommentIns.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,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 | ||
} |
41 changes: 39 additions & 2 deletions
41
unit-picker/src/main/kotlin/cc/unitmesh/pick/builder/comment/KotlinCommentBuilder.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 |
---|---|---|
@@ -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 | ||
} | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
unit-picker/src/test/kotlin/cc/unitmesh/pick/builder/comment/KotlinCommentBuilderTest.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,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() | ||
} | ||
} |