From 871f9fc5807d38759aecbeff61d29207661084b4 Mon Sep 17 00:00:00 2001 From: James Strachan Date: Wed, 22 Feb 2012 17:57:59 +0000 Subject: [PATCH] initial support for extracting comments --- .../kotlin/org/jetbrains/kotlin/doc/KDoc.kt | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/KDoc.kt b/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/KDoc.kt index adea34360da..bd244dbd6a0 100644 --- a/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/KDoc.kt +++ b/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/KDoc.kt @@ -139,16 +139,23 @@ class KDoc(val outputDir: File) : KDocSupport() { } } - protected fun commentsFor(psiElement: DeclarationDescriptor): String { - if (psiElement is PsiElement) { - // This method is a hack. Doc comments should be easily accessible, but they aren't for now. + protected fun commentsFor(descriptor: DeclarationDescriptor): String { + val psiElement = try { + context.get(BindingContext.DESCRIPTOR_TO_DECLARATION, descriptor) + } catch (e: Throwable) { + // ignore exceptions on fake descriptors + null + } + + // This method is a hack. Doc comments should be easily accessible, but they aren't for now. + if (psiElement != null) { var node = psiElement.getNode()?.getTreePrev() while (node != null && (node?.getElementType() == JetTokens.WHITE_SPACE || node?.getElementType() == JetTokens.BLOCK_COMMENT)) { - node = node?.getTreePrev(); + node = node?.getTreePrev() } - if (node == null) return ""; - if (node?.getElementType() != JetTokens.DOC_COMMENT) return ""; - return node?.getText() ?: ""; + if (node == null) return "" + if (node?.getElementType() != JetTokens.DOC_COMMENT) return "" + return node?.getText() ?: "" } return "" }