Skip to content

Commit

Permalink
refactor: extract code comment to quality to analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Jan 7, 2024
1 parent 3cfac59 commit 01e3e30
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package cc.unitmesh.core.comment
package cc.unitmesh.quality.documentation

import cc.unitmesh.core.SupportedLang
import chapi.domain.core.CodePosition
import kotlinx.serialization.Serializable

Expand Down Expand Up @@ -33,24 +32,7 @@ data class CodeComment(
return linesWithLeadingSpace.joinToString("\n")
}

/**
* 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 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> {
fun extractKotlinComment(code: String): List<CodeComment> {
val pattern = Regex("""/\*\*[^*]*\*+([^/*][^*]*\*+)*/""")

val matches = pattern.findAll(code)
Expand Down
1 change: 1 addition & 0 deletions unit-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ plugins {
}

dependencies {
implementation(libs.clikt)
implementation(libs.clikt)
implementation(libs.serialization.json)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import chapi.domain.core.CodeContainer
*
* @see CodeContainer
* @see TypedIns
* @see DocInstruction
* @see DocCommentInstruction
*/
interface CommentBuilder {
/// for generate instruction
val docInstruction: DocInstruction
val docCommentInstruction: DocCommentInstruction
fun build(code: String, container: CodeContainer): List<TypedIns>
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cc.unitmesh.core.comment

enum class DocInstruction(val value: String) {
enum class DocCommentInstruction(val value: String) {
CPP("doxygen"),
JAVA("javadoc"),
JAVASCRIPT("JSDoc"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cc.unitmesh.pick.builder.comment

import cc.unitmesh.core.SupportedLang
import cc.unitmesh.quality.documentation.CodeComment

/**
* 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 extractComments(code: String, language: SupportedLang): List<CodeComment> {
return when (language) {
SupportedLang.KOTLIN -> CodeComment.extractKotlinComment(code)
SupportedLang.JAVA -> CodeComment.extractKotlinComment(code)
else -> {
println("Unsupported language: $language")
emptyList()
}
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
package cc.unitmesh.pick.builder.comment

import cc.unitmesh.core.SupportedLang
import cc.unitmesh.core.SupportedLang.*
import cc.unitmesh.core.comment.DocInstruction
import cc.unitmesh.core.comment.DocCommentInstruction
import cc.unitmesh.core.completion.TypedIns
import cc.unitmesh.core.completion.TypedInsBuilder
import cc.unitmesh.pick.worker.job.JobContext
import chapi.domain.core.CodeContainer

class DocumentationTypedInsBuilder(val context: JobContext) : TypedInsBuilder {
private val kotlinCommentBuilder = JvmCommentBuilder(SupportedLang.KOTLIN)
private val javaCommentBuilder = JvmCommentBuilder(SupportedLang.JAVA, DocInstruction.JAVA)
private val javaCommentBuilder = JvmCommentBuilder(SupportedLang.JAVA, DocCommentInstruction.JAVA)

override fun build(container: CodeContainer): List<TypedIns> {
val language = context.project.language
return when (language) {
JAVA -> javaCommentBuilder.build(context.job.code, container)
KOTLIN -> {
SupportedLang.JAVA -> javaCommentBuilder.build(context.job.code, container)
SupportedLang.KOTLIN -> {
kotlinCommentBuilder.build(context.job.code, container)
}

TYPESCRIPT -> TODO()
RUST -> TODO()
SupportedLang.TYPESCRIPT -> TODO()
SupportedLang.RUST -> TODO()
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package cc.unitmesh.pick.builder.comment

import cc.unitmesh.core.SupportedLang
import cc.unitmesh.core.comment.CodeComment
import cc.unitmesh.quality.documentation.CodeComment
import cc.unitmesh.core.comment.CommentBuilder
import cc.unitmesh.core.comment.DocInstruction
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 chapi.domain.core.CodeContainer

private const val DOC_THRESHOLD = 5

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

/**
Expand All @@ -23,7 +23,7 @@ class JvmCommentBuilder(val language: SupportedLang, override val docInstruction
*/
override fun build(code: String, container: CodeContainer): List<TypedCommentIns> {
val posComments = try {
CodeComment.extractComments(code, language)
extractComments(code, language)
} catch (e: Exception) {
emptyList()
}
Expand All @@ -39,13 +39,13 @@ class JvmCommentBuilder(val language: SupportedLang, override val docInstruction

container.DataStructures.forEach { dataStruct ->
val classComment = startLineCommentMap[dataStruct.Position.StartLine - 1]
classComment?.let { comments.add(ClassCommentIns(docInstruction, 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(docInstruction, function, functionComment, dataStruct, language = language.name)
MethodCommentIns(docCommentInstruction, function, functionComment, dataStruct, language = language.name)
}

comments.addAll(methodCommentIns.filterNotNull())
Expand All @@ -54,3 +54,4 @@ class JvmCommentBuilder(val language: SupportedLang, override val docInstruction
return comments
}
}

Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package cc.unitmesh.pick.builder.comment.ins

import cc.unitmesh.core.Instruction
import cc.unitmesh.core.comment.CodeComment
import cc.unitmesh.quality.documentation.CodeComment
import cc.unitmesh.core.comment.CommentBuilderType
import cc.unitmesh.core.comment.DocInstruction
import cc.unitmesh.core.comment.DocCommentInstruction
import cc.unitmesh.core.comment.TypedCommentIns
import chapi.domain.core.CodeDataStruct
import kotlinx.serialization.Serializable

@Serializable
data class ClassCommentIns(
val docInstruction: DocInstruction,
val docInstruction: DocCommentInstruction,
val dataStructure: CodeDataStruct,
val comment: CodeComment,
val language: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package cc.unitmesh.pick.builder.comment.ins

import cc.unitmesh.core.Instruction
import cc.unitmesh.core.comment.CodeComment
import cc.unitmesh.quality.documentation.CodeComment
import cc.unitmesh.core.comment.CommentBuilderType
import cc.unitmesh.core.comment.DocInstruction
import cc.unitmesh.core.comment.DocCommentInstruction
import cc.unitmesh.core.comment.TypedCommentIns
import cc.unitmesh.pick.ext.toUml
import chapi.domain.core.CodeDataStruct
Expand All @@ -12,7 +12,7 @@ import kotlinx.serialization.Serializable

@Serializable
data class MethodCommentIns(
val docInstruction: DocInstruction,
val docInstruction: DocCommentInstruction,
val function: CodeFunction,
val comment: CodeComment,
val currentDataStruct: CodeDataStruct,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cc.unitmesh.pick.builder.comment

import cc.unitmesh.core.SupportedLang
import cc.unitmesh.core.comment.CodeComment
import cc.unitmesh.quality.documentation.CodeComment
import chapi.ast.kotlinast.KotlinAnalyser
import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Test
Expand Down 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.extractComments(kotlinCode, SupportedLang.KOTLIN)
val result = extractComments(kotlinCode, SupportedLang.KOTLIN)

// Then
result.size shouldBe 3
Expand Down

0 comments on commit 01e3e30

Please sign in to comment.