Ousc
09/13/2024, 2:18 AMfindKDocString
, it always get null.Is it my code or the comment that is wrong?
@OptIn(ObsoleteDescriptorBasedAPI::class)
override fun visitPropertyNew(declaration: IrProperty): IrStatement {
val kDocStr = declaration.descriptor.findKDocString()
return super.visitPropertyNew(declaration)
}
jw
09/13/2024, 2:46 AMtreeStructure.getChildrenArray(lighterASTNode)
.firstOrNull { it?.tokenType == KDocTokens.KDOC }
?.let { treeStructure.toString(it).toString() }
jw
09/13/2024, 2:49 AMKDeclaration
type which had a property which exposed a KDoc
type, but I never had time to get that to workjw
09/13/2024, 2:52 AMOusc
09/13/2024, 4:11 AMprivate fun FirDeclaration.findKDocString(): String? =
source?.let {
val kidsRef = Ref<Array<LighterASTNode?>>()
it.treeStructure.getChildren(it.lighterASTNode, kidsRef)
kidsRef.get().singleOrNull { it?.tokenType == KtTokens.DOC_COMMENT }?.toString()
}
But this is not the same as how my code works. I can only find the following through `IrDeclarationBase`:
IrDeclarationBase.descriptor.extractSerializedKdocString()
But whether it is IrDeclarationBase.descriptor.extractSerializedKdocString()
or IrDeclarationBase.descriptor.findPsi()
, I can only get null.Ousc
09/13/2024, 6:05 AMPavel Kunyavskiy [JB]
09/13/2024, 9:01 AMOusc
09/14/2024, 7:04 AMsourceElement()
function in IrProperty, which can get SourceRangeInfo
and IrFileEntry
, and get the comment information by parsing the string:
private val sourceFileCache: LRUCache<String, List<String>> = LRUCache(128)
context(IrBuilderWithScope, IrPluginContext)
fun IrProperty.getCommentString(): IrExpression {
val sourceOffsets = sourceElement()
if (sourceOffsets != null) {
val startOffset = sourceOffsets.startOffset
val endOffset = sourceOffsets.endOffset
val fileEntry = file.fileEntry
val sourceRange = fileEntry.getSourceRangeInfo(startOffset, endOffset)
val source = sourceFileCache.getOrPut(fileEntry.name) {
File(sourceRange.filePath).readLines(UTF_8)
}
val comment =
extractPropertyComment(source, sourceRange.startLineNumber..sourceRange.endLineNumber)
if (comment != null) {
return irString(comment)
}
}
return irNull()
}