Skip to content

Commit bb5f0d4

Browse files
committed
Improve code quality
1 parent 81cf4c3 commit bb5f0d4

File tree

1 file changed

+37
-30
lines changed

1 file changed

+37
-30
lines changed

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

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -93,33 +93,10 @@ class CommentExtractor(private val fileExtractor: KotlinFileExtractor, private v
9393
file.accept(IrVisitorLookup(psi2Ir, ownerPsi, file), owners)
9494

9595
for (ownerIr in owners) {
96-
val ownerLabel =
97-
if (ownerIr == file)
98-
fileLabel
99-
else {
100-
if (ownerIr is IrValueParameter && ownerIr.index == -1) {
101-
// Don't attribute comments to the implicit `this` parameter of a function.
102-
continue
103-
}
104-
val label: String
105-
val existingLabel = if (ownerIr is IrVariable) {
106-
label = "variable ${ownerIr.name.asString()}"
107-
tw.getExistingVariableLabelFor(ownerIr)
108-
} else if (ownerIr is IrFunction && ownerIr.isLocalFunction()) {
109-
label = "local function ${ownerIr.name.asString()}"
110-
fileExtractor.getExistingLocallyVisibleFunctionLabel(ownerIr)
111-
}
112-
else {
113-
label = getLabel(ownerIr) ?: continue
114-
tw.getExistingLabelFor<DbTop>(label)
115-
}
116-
if (existingLabel == null) {
117-
logger.warn("Couldn't get existing label for $label")
118-
continue
119-
}
120-
existingLabel
121-
}
122-
tw.writeKtCommentOwners(commentLabel, ownerLabel)
96+
val ownerLabel = getLabel(ownerIr)
97+
if (ownerLabel != null) {
98+
tw.writeKtCommentOwners(commentLabel, ownerLabel)
99+
}
123100
}
124101
}
125102

@@ -131,7 +108,37 @@ class CommentExtractor(private val fileExtractor: KotlinFileExtractor, private v
131108
return owner
132109
}
133110

134-
private fun getLabel(element: IrElement) : String? {
111+
private fun getLabel(element: IrElement): Label<out DbTop>? {
112+
if (element == file)
113+
return fileLabel
114+
115+
if (element is IrValueParameter && element.index == -1) {
116+
// Don't attribute comments to the implicit `this` parameter of a function.
117+
return null
118+
}
119+
120+
val label: String
121+
val existingLabel = if (element is IrVariable) {
122+
// local variables are not named globally, so we need to get them from the variable label cache
123+
label = "variable ${element.name.asString()}"
124+
tw.getExistingVariableLabelFor(element)
125+
} else if (element is IrFunction && element.isLocalFunction()) {
126+
// local functions are not named globally, so we need to get them from the local function label cache
127+
label = "local function ${element.name.asString()}"
128+
fileExtractor.getExistingLocallyVisibleFunctionLabel(element)
129+
}
130+
else {
131+
label = getLabelForNamedElement(element) ?: return null
132+
tw.getExistingLabelFor<DbTop>(label)
133+
}
134+
if (existingLabel == null) {
135+
logger.warn("Couldn't get existing label for $label")
136+
return null
137+
}
138+
return existingLabel
139+
}
140+
141+
private fun getLabelForNamedElement(element: IrElement) : String? {
135142
when (element) {
136143
is IrClass -> return fileExtractor.getClassLabel(element, listOf()).classLabel
137144
is IrTypeParameter -> return fileExtractor.getTypeParameterLabel(element)
@@ -155,10 +162,10 @@ class CommentExtractor(private val fileExtractor: KotlinFileExtractor, private v
155162
return null
156163
}
157164
// Assign the comment to the class. The content of the `init` blocks might be extracted in multiple constructors.
158-
return getLabel(parentClass)
165+
return getLabelForNamedElement(parentClass)
159166
}
160167

161-
// Fresh entities:
168+
// Fresh entities, not named elements:
162169
is IrBody -> return null
163170
is IrExpression -> return null
164171

0 commit comments

Comments
 (0)