Skip to content

Commit

Permalink
refactor: extact api for doc
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Jan 7, 2024
1 parent 61ef273 commit 3cfac59
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
19 changes: 15 additions & 4 deletions unit-core/src/main/kotlin/cc/unitmesh/core/comment/CodeComment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import kotlinx.serialization.Serializable
@Serializable
data class CodeComment(
val content: String,
val position: CodePosition
val position: CodePosition,
) {
companion object {
/**
Expand All @@ -34,12 +34,23 @@ data class CodeComment(
}

/**
* Extracts the Kotlin documentation comments (KDoc) from the given code.
* Extracts the 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, language: SupportedLang): List<CodeComment> {
fun extractComments(code: String, language: SupportedLang): List<CodeComment> {
return when (language) {
SupportedLang.KOTLIN -> extractKotlinComment(code)
SupportedLang.JAVA -> extractKotlinComment(code)
else -> {
println("Unsupported language: $language")
emptyList()
}
}
}

private fun extractKotlinComment(code: String): List<CodeComment> {
val pattern = Regex("""/\*\*[^*]*\*+([^/*][^*]*\*+)*/""")

val matches = pattern.findAll(code)
Expand All @@ -52,7 +63,7 @@ data class CodeComment(
val stopLinePosition = match.range.last - code.lastIndexOf('\n', match.range.last) - 1

val position = CodePosition(startLine, startLinePosition, stopLine, stopLinePosition)
val content = Companion.reIndentComment(commentContent)
val content = reIndentComment(commentContent)

CodeComment(content, position)
}.toList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ class JvmCommentBuilder(val language: SupportedLang, override val docInstruction
*/
override fun build(code: String, container: CodeContainer): List<TypedCommentIns> {
val posComments = try {
CodeComment.extractKdocComments(code, language)
CodeComment.extractComments(code, language)
} catch (e: Exception) {
emptyList()
}

if (posComments.isEmpty()) return emptyList()

val startLineCommentMap: Map<Int, CodeComment> =
posComments.filter { it.content.isNotBlank() && it.content.length >= DOC_THRESHOLD }.associateBy {
it.position.StopLine
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Group<T>(val name: String) {
@Test
fun `should extract KDoc comments when valid code provided`() {
// When
val result = CodeComment.extractKdocComments(kotlinCode, SupportedLang.KOTLIN)
val result = CodeComment.extractComments(kotlinCode, SupportedLang.KOTLIN)

// Then
result.size shouldBe 3
Expand Down

0 comments on commit 3cfac59

Please sign in to comment.