-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: update comment builder design
- Loading branch information
Showing
4 changed files
with
66 additions
and
61 deletions.
There are no files selected for viewing
53 changes: 52 additions & 1 deletion
53
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 |
---|---|---|
@@ -1,10 +1,61 @@ | ||
package cc.unitmesh.core.comment | ||
|
||
import cc.unitmesh.core.SupportedLang | ||
import chapi.domain.core.CodePosition | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class CodeComment( | ||
val content: String, | ||
val position: CodePosition | ||
) | ||
) { | ||
companion object { | ||
/** | ||
* Re-indent the comment to remove the leading spaces. | ||
* | ||
* @param content the comment content to be re-indented | ||
* @return the re-indented comment content | ||
*/ | ||
private fun reIndentComment(content: String): String { | ||
val lines = content.split("\n") | ||
val indent = lines[1].takeWhile { it == ' ' } | ||
val linesWithoutIndent = lines.map { it.removePrefix(indent) } | ||
|
||
// except the first line, every line should have one leading space | ||
val linesWithLeadingSpace = linesWithoutIndent.mapIndexed { index, line -> | ||
if (index == 0) { | ||
line | ||
} else { | ||
" $line" | ||
} | ||
} | ||
|
||
return linesWithLeadingSpace.joinToString("\n") | ||
} | ||
|
||
/** | ||
* 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, language: SupportedLang): List<CodeComment> { | ||
val pattern = Regex("""/\*\*[^*]*\*+([^/*][^*]*\*+)*/""") | ||
|
||
val matches = pattern.findAll(code) | ||
|
||
return matches.map { match -> | ||
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 content = Companion.reIndentComment(commentContent) | ||
|
||
CodeComment(content, position) | ||
}.toList() | ||
} | ||
} | ||
} |
6 changes: 3 additions & 3 deletions
6
unit-picker/src/main/kotlin/cc/unitmesh/pick/builder/comment/DocumentationTypedInsBuilder.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
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
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