Skip to content

Commit

Permalink
refactor: move build comments for CodeComment
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Jan 8, 2024
1 parent 3f374cb commit 707f53e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ package cc.unitmesh.quality.comment
import chapi.domain.core.CodePosition
import kotlinx.serialization.Serializable


/**
* a doc must have at least 3 lines, start with /** and end with */, and have at least one line of content
*/
const val DOC_THRESHOLD = 3

@Serializable
data class CodeComment(
val content: String,
Expand Down Expand Up @@ -32,6 +38,12 @@ data class CodeComment(
return linesWithLeadingSpace.joinToString("\n")
}

/**
* Extracts Kotlin comments from the given code and returns a list of [CodeComment] objects.
*
* @param code The Kotlin code from which to extract comments.
* @return A list of [CodeComment] objects representing the extracted comments.
*/
fun extractKotlinComment(code: String): List<CodeComment> {
val pattern = Regex("""/\*\*[^*]*\*+([^/*][^*]*\*+)*/""")

Expand All @@ -50,5 +62,22 @@ data class CodeComment(
CodeComment(content, position)
}.toList()
}

/**
* Returns a map of line numbers to code comments.
*
* This method takes a list of `CodeComment` objects and filters out any comments that are blank or have a length less than the `DOC_THRESHOLD` constant. It then associates each comment with its corresponding stop line number in the source code, resulting in a map where the keys are line numbers and the values are the associated comments.
*
* @param posComments the list of `CodeComment` objects to process
* @return a map of line numbers to code comments
*/
fun lineCommentMap(posComments: List<CodeComment>): Map<Int, CodeComment> {
val startLineCommentMap: Map<Int, CodeComment> =
posComments.filter { it.content.isNotBlank() && it.content.length >= DOC_THRESHOLD }.associateBy {
it.position.StopLine
}

return startLineCommentMap
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import cc.unitmesh.core.comment.DocCommentInstruction
import cc.unitmesh.core.comment.TypedCommentIns
import cc.unitmesh.pick.builder.comment.ins.ClassCommentIns
import cc.unitmesh.pick.builder.comment.ins.MethodCommentIns
import cc.unitmesh.quality.comment.DOC_THRESHOLD
import chapi.domain.core.CodeContainer

private const val DOC_THRESHOLD = 5

class JvmCommentBuilder(val language: SupportedLang, override val docCommentInstruction: DocCommentInstruction = DocCommentInstruction.KOTLIN) :
class JvmCommentBuilder(
val language: SupportedLang,
override val docCommentInstruction: DocCommentInstruction = DocCommentInstruction.KOTLIN,
) :
CommentBuilder {

/**
Expand All @@ -30,22 +32,33 @@ class JvmCommentBuilder(val language: SupportedLang, override val docCommentInst

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

val startLineCommentMap: Map<Int, CodeComment> =
posComments.filter { it.content.isNotBlank() && it.content.length >= DOC_THRESHOLD }.associateBy {
it.position.StopLine
}
val startLineCommentMap: Map<Int, CodeComment> = CodeComment.lineCommentMap(posComments)

val comments = mutableListOf<TypedCommentIns>()

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

val methodCommentIns =
dataStruct.Functions.filter { it.Name != "constructor" && it.Name != "PrimaryConstructor" }
.map { function ->
val functionComment = startLineCommentMap[function.Position.StartLine - 1] ?: return@map null
MethodCommentIns(docCommentInstruction, function, functionComment, dataStruct, language = language.name)
MethodCommentIns(
docCommentInstruction,
function,
functionComment,
dataStruct,
language = language.name
)
}

comments.addAll(methodCommentIns.filterNotNull())
Expand Down

0 comments on commit 707f53e

Please sign in to comment.