@@ -7,6 +7,9 @@ import com.intellij.psi.PsiComment
77import com.intellij.psi.PsiElement
88import org.jetbrains.kotlin.ir.IrElement
99import org.jetbrains.kotlin.ir.declarations.*
10+ import org.jetbrains.kotlin.ir.expressions.IrBody
11+ import org.jetbrains.kotlin.ir.expressions.IrExpression
12+ import org.jetbrains.kotlin.ir.util.parentClassOrNull
1013import org.jetbrains.kotlin.kdoc.psi.api.KDoc
1114import org.jetbrains.kotlin.lexer.KtTokens
1215import org.jetbrains.kotlin.psi.KtVisitor
@@ -102,7 +105,7 @@ class CommentExtractor(private val fileExtractor: KotlinFileExtractor, private v
102105 label = " variable ${ownerIr.name.asString()} "
103106 tw.getExistingVariableLabelFor(ownerIr)
104107 } else {
105- label = fileExtractor. getLabel(ownerIr) ? : continue
108+ label = getLabel(ownerIr) ? : continue
106109 tw.getExistingLabelFor<DbTop >(label)
107110 }
108111 if (existingLabel == null ) {
@@ -118,9 +121,42 @@ class CommentExtractor(private val fileExtractor: KotlinFileExtractor, private v
118121 private fun getKDocOwner (comment : KDoc ) : PsiElement ? {
119122 val owner = comment.owner
120123 if (owner == null ) {
121- logger.warn(" Couldn't get owner of KDoc." )
124+ logger.warn(" Couldn't get owner of KDoc. The comment is extracted without an owner. " )
122125 }
123126 return owner
124127 }
128+
129+ private fun getLabel (element : IrElement ) : String? {
130+ when (element) {
131+ is IrClass -> return fileExtractor.getClassLabel(element, listOf ()).classLabel
132+ is IrTypeParameter -> return fileExtractor.getTypeParameterLabel(element)
133+ is IrFunction -> return fileExtractor.getFunctionLabel(element, null )
134+ is IrValueParameter -> return fileExtractor.getValueParameterLabel(element, null )
135+ is IrProperty -> return fileExtractor.getPropertyLabel(element)
136+ is IrField -> return fileExtractor.getFieldLabel(element)
137+ is IrEnumEntry -> return fileExtractor.getEnumEntryLabel(element)
138+ is IrTypeAlias -> return fileExtractor.getTypeAliasLabel(element)
139+
140+ is IrAnonymousInitializer -> {
141+ val parentClass = element.parentClassOrNull
142+ if (parentClass == null ) {
143+ logger.errorElement(" Parent of anonymous initializer is not a class" , element)
144+ return null
145+ }
146+ // Assign the comment to the class. The content of the `init` blocks might be extracted in multiple constructors.
147+ return fileExtractor.getClassLabel(parentClass, listOf ()).classLabel
148+ }
149+
150+ // Fresh entities:
151+ is IrBody -> return null
152+ is IrExpression -> return null
153+
154+ // todo add others:
155+ else -> {
156+ logger.errorElement(" Unhandled element type: ${element::class } " , element)
157+ return null
158+ }
159+ }
125160 }
161+ }
126162}
0 commit comments