diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/FindDocComment.kt b/compiler/frontend/src/org/jetbrains/jet/lang/psi/FindDocComment.kt new file mode 100644 index 00000000000..0252d5a9b41 --- /dev/null +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/FindDocComment.kt @@ -0,0 +1,29 @@ +/* + * Copyright 2010-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.jet.lang.psi.findDocComment + +import org.jetbrains.jet.lang.psi.JetDeclaration +import org.jetbrains.jet.kdoc.psi.api.KDoc +import org.jetbrains.jet.lang.psi.psiUtil.siblings +import com.intellij.psi.PsiWhiteSpace +import com.intellij.psi.PsiComment + +fun findDocComment(declaration: JetDeclaration): KDoc? { + return declaration.getFirstChild().siblings() + .dropWhile { it !is KDoc && (it is PsiWhiteSpace || it is PsiComment) } + .first() as? KDoc +} \ No newline at end of file diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetDeclaration.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetDeclaration.java index 99b87041e73..17eee99004f 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetDeclaration.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetDeclaration.java @@ -18,6 +18,8 @@ package org.jetbrains.jet.lang.psi; import com.intellij.util.ArrayFactory; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.jet.kdoc.psi.api.KDoc; public interface JetDeclaration extends JetExpression, JetModifierListOwner { JetDeclaration[] EMPTY_ARRAY = new JetDeclaration[0]; @@ -29,4 +31,7 @@ public interface JetDeclaration extends JetExpression, JetModifierListOwner { return count == 0 ? EMPTY_ARRAY : new JetDeclaration[count]; } }; + + @Nullable + public KDoc getDocComment(); } diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetDeclarationImpl.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetDeclarationImpl.java index daded86cf84..3d97f7fdcaf 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetDeclarationImpl.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetDeclarationImpl.java @@ -20,7 +20,9 @@ import com.intellij.lang.ASTNode; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.JetNodeTypes; +import org.jetbrains.jet.kdoc.psi.api.KDoc; import org.jetbrains.jet.lang.psi.addRemoveModifier.AddRemoveModifierPackage; +import org.jetbrains.jet.lang.psi.findDocComment.FindDocCommentPackage; import org.jetbrains.jet.lexer.JetModifierKeywordToken; import org.jetbrains.jet.lexer.JetTokens; @@ -69,4 +71,10 @@ abstract class JetDeclarationImpl extends JetExpressionImpl implements JetDeclar if (modifierList == null) return Collections.emptyList(); return modifierList.getAnnotations(); } + + @Nullable + @Override + public KDoc getDocComment() { + return FindDocCommentPackage.findDocComment(this); + } } diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetDeclarationStub.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetDeclarationStub.java index 44260b40c1c..859479be421 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetDeclarationStub.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetDeclarationStub.java @@ -21,11 +21,8 @@ import com.intellij.psi.stubs.IStubElementType; import com.intellij.psi.stubs.StubElement; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import org.jetbrains.jet.lexer.JetModifierKeywordToken; -import org.jetbrains.jet.lang.psi.stubs.elements.JetStubElementTypes; - -import java.util.Collections; -import java.util.List; +import org.jetbrains.jet.kdoc.psi.api.KDoc; +import org.jetbrains.jet.lang.psi.findDocComment.FindDocCommentPackage; abstract class JetDeclarationStub extends JetModifierListOwnerStub implements JetDeclaration { public JetDeclarationStub(@NotNull T stub, @NotNull IStubElementType nodeType) { @@ -35,4 +32,10 @@ abstract class JetDeclarationStub extends JetModifierList public JetDeclarationStub(@NotNull ASTNode node) { super(node); } + + @Nullable + @Override + public KDoc getDocComment() { + return FindDocCommentPackage.findDocComment(this); + } } diff --git a/idea/src/org/jetbrains/jet/plugin/JetQuickDocumentationProvider.java b/idea/src/org/jetbrains/jet/plugin/JetQuickDocumentationProvider.java index b3df779f8b9..09df5d33d3a 100644 --- a/idea/src/org/jetbrains/jet/plugin/JetQuickDocumentationProvider.java +++ b/idea/src/org/jetbrains/jet/plugin/JetQuickDocumentationProvider.java @@ -25,14 +25,14 @@ import com.intellij.psi.PsiWhiteSpace; import com.intellij.psi.tree.IElementType; import com.intellij.psi.util.PsiTreeUtil; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.asJava.KotlinLightMethod; import org.jetbrains.jet.kdoc.lexer.KDocTokens; import org.jetbrains.jet.kdoc.psi.api.KDoc; import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; -import org.jetbrains.jet.lang.psi.*; +import org.jetbrains.jet.lang.psi.JetDeclaration; +import org.jetbrains.jet.lang.psi.JetPackageDirective; +import org.jetbrains.jet.lang.psi.JetReferenceExpression; import org.jetbrains.jet.lang.resolve.BindingContext; -import org.jetbrains.jet.lexer.JetTokens; import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache; import org.jetbrains.jet.renderer.DescriptorRenderer; @@ -92,7 +92,7 @@ public class JetQuickDocumentationProvider extends AbstractDocumentationProvider renderedDecl = "
" + DescriptorRenderer.HTML_NAMES_WITH_SHORT_TYPES.render(declarationDescriptor) + "
"; } - KDoc comment = findElementKDoc(declaration); + KDoc comment = declaration.getDocComment(); if (comment != null) { renderedDecl = renderedDecl + "
" + kDocToHtml(comment); } @@ -140,11 +140,6 @@ public class JetQuickDocumentationProvider extends AbstractDocumentationProvider return builder.toString(); } - @Nullable - private static KDoc findElementKDoc(@NotNull JetElement element) { - return (KDoc) JetPsiUtil.findChildByType(element, JetTokens.DOC_COMMENT); - } - private static String kDocToHtml(@NotNull KDoc comment) { // TODO: Parse and show markdown comments as html String content = getKDocContent(comment);