Kotlin: Comments: Small refactoring

This commit is contained in:
Ian Lynagh
2022-01-13 14:21:47 +00:00
parent b599ff2792
commit 4340fe7044

View File

@@ -19,93 +19,93 @@ class CommentExtractor(private val fileExtractor: KotlinSourceFileExtractor) {
private val logger = fileExtractor.logger
private val ktFile = Psi2Ir().getKtFile(file)
init {
fun extract() {
if (ktFile == null) {
logger.warn(Severity.Warn, "Comments are not being processed in ${file.path}.")
} else {
ktFile.accept(commentVisitor)
}
}
fun extract() {
ktFile?.accept(
object : KtVisitor<Unit, Unit>() {
override fun visitElement(element: PsiElement) {
element.acceptChildren(this)
private val commentVisitor =
object : KtVisitor<Unit, Unit>() {
override fun visitElement(element: PsiElement) {
element.acceptChildren(this)
// Slightly hacky, but `visitComment` doesn't seem to visit comments with `tokenType` `KtTokens.DOC_COMMENT`
if (element is PsiComment){
visitCommentElement(element)
}
// Slightly hacky, but `visitComment` doesn't seem to visit comments with `tokenType` `KtTokens.DOC_COMMENT`
if (element is PsiComment){
visitCommentElement(element)
}
}
private fun visitCommentElement(comment: PsiComment) {
val type: CommentType = when (comment.tokenType) {
KtTokens.EOL_COMMENT -> {
CommentType.SingleLine
}
KtTokens.BLOCK_COMMENT -> {
CommentType.Block
}
KtTokens.DOC_COMMENT -> {
CommentType.Doc
}
else -> {
logger.warn(Severity.Warn, "Unhandled comment token type: ${comment.tokenType}")
return
}
private fun visitCommentElement(comment: PsiComment) {
val type: CommentType = when (comment.tokenType) {
KtTokens.EOL_COMMENT -> {
CommentType.SingleLine
}
val commentLabel = tw.getFreshIdLabel<DbKtcomment>()
tw.writeKtComments(commentLabel, type.value, escapeTrapString(comment.text))
val locId = tw.getLocation(comment.startOffset, comment.endOffset)
tw.writeHasLocation(commentLabel, locId)
if (comment.tokenType != KtTokens.DOC_COMMENT) {
KtTokens.BLOCK_COMMENT -> {
CommentType.Block
}
KtTokens.DOC_COMMENT -> {
CommentType.Doc
}
else -> {
logger.warn(Severity.Warn, "Unhandled comment token type: ${comment.tokenType}")
return
}
}
if (comment !is KDoc) {
logger.warn(Severity.Warn, "Unexpected comment type with DocComment token type.")
return
val commentLabel = tw.getFreshIdLabel<DbKtcomment>()
tw.writeKtComments(commentLabel, type.value, escapeTrapString(comment.text))
val locId = tw.getLocation(comment.startOffset, comment.endOffset)
tw.writeHasLocation(commentLabel, locId)
if (comment.tokenType != KtTokens.DOC_COMMENT) {
return
}
if (comment !is KDoc) {
logger.warn(Severity.Warn, "Unexpected comment type with DocComment token type.")
return
}
for (sec in comment.getAllSections()) {
val commentSectionLabel = tw.getFreshIdLabel<DbKtcommentsection>()
tw.writeKtCommentSections(commentSectionLabel, commentLabel, escapeTrapString(sec.getContent()))
val name = sec.name
if (name != null) {
tw.writeKtCommentSectionNames(commentSectionLabel, escapeTrapString(name))
}
for (sec in comment.getAllSections()) {
val commentSectionLabel = tw.getFreshIdLabel<DbKtcommentsection>()
tw.writeKtCommentSections(commentSectionLabel, commentLabel, escapeTrapString(sec.getContent()))
val name = sec.name
if (name != null) {
tw.writeKtCommentSectionNames(commentSectionLabel, escapeTrapString(name))
}
val subjectName = sec.getSubjectName()
if (subjectName != null) {
tw.writeKtCommentSectionSubjectNames(commentSectionLabel, escapeTrapString(subjectName))
}
}
// Only storing the owner of doc comments:
val ownerPsi = getKDocOwner(comment) ?: return
val owners = mutableListOf<IrElement>()
file.accept(IrVisitorLookup(ownerPsi, file), owners)
for (ownerIr in owners) {
val label = fileExtractor.getLabel(ownerIr) ?: continue
val existingLabel = tw.getExistingLabelFor<DbTop>(label)
if (existingLabel == null) {
logger.warn(Severity.Warn, "Couldn't get existing label for $label")
continue
}
tw.writeKtCommentOwners(commentLabel, existingLabel)
val subjectName = sec.getSubjectName()
if (subjectName != null) {
tw.writeKtCommentSectionSubjectNames(commentSectionLabel, escapeTrapString(subjectName))
}
}
private fun getKDocOwner(comment: KDoc) : PsiElement? {
if (comment.owner == null) {
logger.warn(Severity.Warn, "Couldn't get owner of KDoc.")
return null
// Only storing the owner of doc comments:
val ownerPsi = getKDocOwner(comment) ?: return
val owners = mutableListOf<IrElement>()
file.accept(IrVisitorLookup(ownerPsi, file), owners)
for (ownerIr in owners) {
val label = fileExtractor.getLabel(ownerIr) ?: continue
val existingLabel = tw.getExistingLabelFor<DbTop>(label)
if (existingLabel == null) {
logger.warn(Severity.Warn, "Couldn't get existing label for $label")
continue
}
return comment.owner!!
tw.writeKtCommentOwners(commentLabel, existingLabel)
}
})
}
}
private fun getKDocOwner(comment: KDoc) : PsiElement? {
val owner = comment.owner
if (owner == null) {
logger.warn(Severity.Warn, "Couldn't get owner of KDoc.")
}
return owner
}
}
}