Skip to content

Commit 5418c95

Browse files
committed
Kotlin: minor refactoring in comment extraction
1 parent 9ced146 commit 5418c95

File tree

2 files changed

+38
-38
lines changed

2 files changed

+38
-38
lines changed

java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -159,42 +159,6 @@ open class KotlinFileExtractor(
159159
}
160160
}
161161

162-
163-
164-
fun getLabel(element: IrElement) : String? {
165-
when (element) {
166-
is IrClass -> return getClassLabel(element, listOf()).classLabel
167-
is IrTypeParameter -> return getTypeParameterLabel(element)
168-
is IrFunction -> return getFunctionLabel(element, null)
169-
is IrValueParameter -> return getValueParameterLabel(element, null)
170-
is IrProperty -> return getPropertyLabel(element)
171-
is IrField -> return getFieldLabel(element)
172-
is IrEnumEntry -> return getEnumEntryLabel(element)
173-
is IrTypeAlias -> return getTypeAliasLabel(element)
174-
175-
is IrAnonymousInitializer -> {
176-
val parentClass = element.parentClassOrNull
177-
if (parentClass == null) {
178-
logger.errorElement("Parent of anonymous initializer is not a class", element)
179-
return null
180-
}
181-
// Assign the comment to the class. The content of the `init` blocks might be extracted in multiple constructors.
182-
return getClassLabel(parentClass, listOf()).classLabel
183-
}
184-
185-
186-
// Fresh entities:
187-
is IrBody -> return null
188-
is IrExpression -> return null
189-
190-
// todo add others:
191-
else -> {
192-
logger.errorElement("Unhandled element type: ${element::class}", element)
193-
return null
194-
}
195-
}
196-
}
197-
198162
private fun extractTypeParameter(tp: IrTypeParameter, apparentIndex: Int, javaTypeParameter: JavaTypeParameter?): Label<out DbTypevariable>? {
199163
with("type parameter", tp) {
200164
val parentId = getTypeParameterParentLabel(tp) ?: return null

java/kotlin-extractor/src/main/kotlin/comments/CommentExtractor.kt

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import com.intellij.psi.PsiComment
77
import com.intellij.psi.PsiElement
88
import org.jetbrains.kotlin.ir.IrElement
99
import 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
1013
import org.jetbrains.kotlin.kdoc.psi.api.KDoc
1114
import org.jetbrains.kotlin.lexer.KtTokens
1215
import 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

Comments
 (0)