Hello everyone, is it possible to read the comment...
# compiler
o
Hello everyone, is it possible to read the comment or KDoc for a certain IrProperty in the Transformer of the compiler plugin? I tried
findKDocString
, it always get null.Is it my code or the comment that is wrong?
Copy code
@OptIn(ObsoleteDescriptorBasedAPI::class)
 override fun visitPropertyNew(declaration: IrProperty): IrStatement {
     val kDocStr = declaration.descriptor.findKDocString()
     return super.visitPropertyNew(declaration)
 }
j
If you want the raw KDoc source you can do something like
Copy code
treeStructure.getChildrenArray(lighterASTNode)
  .firstOrNull { it?.tokenType == KDocTokens.KDOC }
  ?.let { treeStructure.toString(it).toString() }
There used to be a
KDeclaration
type which had a property which exposed a
KDoc
type, but I never had time to get that to work
I asked here a very similar question, but just haven't followed up on it. Would be interested if you can get the parsed KDoc as something like IntelliJ would render it. https://kotlinlang.slack.com/archives/C7L3JB43G/p1721328442969959
o
I see that findDocString in kotlin/compiler/fir does work like this:
Copy code
private 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`:
Copy code
IrDeclarationBase.descriptor.extractSerializedKdocString()
But whether it is
IrDeclarationBase.descriptor.extractSerializedKdocString()
or
IrDeclarationBase.descriptor.findPsi()
, I can only get null.
It seems that this can only be implemented in FIR, right?
p
Yes. On IR level no comments are preserved in any way. IrDeclarationBase.descriptor is not a real descriptor, but rather something pretending to be it reading data from IR.
o
Fortunately, I found the
sourceElement()
function in IrProperty, which can get
SourceRangeInfo
and
IrFileEntry
, and get the comment information by parsing the string:
Copy code
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()
 }