From c366da95ac3ddfb1f713b687c5d60049f176e462 Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Fri, 6 Feb 2015 18:13:35 +0100 Subject: [PATCH] move KDocFinder to plugin because it's not used in compiler; wrap it in an object to avoid package facade conflicts; add test to findKDoc() and fix it so that it actually works --- .../org/jetbrains/kotlin/kdoc/KDocFinder.kt | 56 ------------------ .../kotlin/kdoc/psi/impl/KDocSection.kt | 11 ++++ .../jetbrains/kotlin/kdoc/psi/impl/KDocTag.kt | 2 +- .../idea/JetQuickDocumentationProvider.java | 4 +- .../jetbrains/kotlin/idea/kdoc/KDocFinder.kt | 58 +++++++++++++++++++ idea/testData/kdoc/finder/Constructor.kt | 6 ++ .../kotlin/idea/kdoc/KDocFinderTest.kt | 39 +++++++++++++ 7 files changed, 117 insertions(+), 59 deletions(-) delete mode 100644 compiler/frontend/src/org/jetbrains/kotlin/kdoc/KDocFinder.kt create mode 100644 idea/src/org/jetbrains/kotlin/idea/kdoc/KDocFinder.kt create mode 100644 idea/testData/kdoc/finder/Constructor.kt create mode 100644 idea/tests/org/jetbrains/kotlin/idea/kdoc/KDocFinderTest.kt diff --git a/compiler/frontend/src/org/jetbrains/kotlin/kdoc/KDocFinder.kt b/compiler/frontend/src/org/jetbrains/kotlin/kdoc/KDocFinder.kt deleted file mode 100644 index 7ee2e08d951..00000000000 --- a/compiler/frontend/src/org/jetbrains/kotlin/kdoc/KDocFinder.kt +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 2010-2015 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.kotlin.kdoc - -import org.jetbrains.kotlin.descriptors.DeclarationDescriptor -import org.jetbrains.kotlin.descriptors.DeclarationDescriptorWithSource -import org.jetbrains.kotlin.resolve.source.PsiSourceElement -import org.jetbrains.kotlin.psi.JetDeclaration -import org.jetbrains.kotlin.kdoc.psi.impl.KDocTag -import org.jetbrains.kotlin.descriptors.CallableDescriptor -import org.jetbrains.kotlin.descriptors.ConstructorDescriptor -import org.jetbrains.kotlin.kdoc.parser.KDocKnownTag - -fun findKDoc(declaration: DeclarationDescriptor): KDocTag? { - if (declaration is DeclarationDescriptorWithSource) { - val psiDeclaration = (declaration.getSource() as? PsiSourceElement)?.psi?.getNavigationElement() - if (psiDeclaration is JetDeclaration) { - val kdoc = psiDeclaration.getDocComment() - if (kdoc != null) { - if (declaration is ConstructorDescriptor) { - // ConstructorDescriptor resolves to the same JetDeclaration - val constructorSection = kdoc.findSectionByTag(KDocKnownTag.CONSTRUCTOR) - if (constructorSection != null) { - return constructorSection - } - } - return kdoc.getDefaultSection() - } - } - } - - if (declaration is CallableDescriptor) { - for (baseDescriptor in declaration.getOverriddenDescriptors()) { - val baseKDoc = findKDoc(baseDescriptor) - if (baseKDoc != null) { - return baseKDoc - } - } - } - - return null -} diff --git a/compiler/frontend/src/org/jetbrains/kotlin/kdoc/psi/impl/KDocSection.kt b/compiler/frontend/src/org/jetbrains/kotlin/kdoc/psi/impl/KDocSection.kt index c7e52775df3..b1eeaba5066 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/kdoc/psi/impl/KDocSection.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/kdoc/psi/impl/KDocSection.kt @@ -18,6 +18,7 @@ package org.jetbrains.kotlin.kdoc.psi.impl import com.intellij.lang.ASTNode import org.jetbrains.kotlin.psi.psiUtil.getChildrenOfType +import org.jetbrains.kotlin.psi.psiUtil.getChildOfType /** * The part of a doc comment which describes a single class, method or property @@ -26,6 +27,16 @@ import org.jetbrains.kotlin.psi.psiUtil.getChildrenOfType * properties defined in the primary constructor. */ public class KDocSection(node: ASTNode) : KDocTag(node) { + /** + * Returns the name of the section (the name of the doc tag introducing the section, + * or null for the default section). + */ + override fun getName(): String? = + (getFirstChild() as? KDocTag)?.getName() + + override fun getContent(): String = + (getFirstChild() as? KDocTag)?.getContent() ?: super.getContent() + public fun findTagsByName(name: String): List { return getChildrenOfType().filter { it.getName() == name } } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/kdoc/psi/impl/KDocTag.kt b/compiler/frontend/src/org/jetbrains/kotlin/kdoc/psi/impl/KDocTag.kt index 9f1417c6d62..f4f097c38b9 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/kdoc/psi/impl/KDocTag.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/kdoc/psi/impl/KDocTag.kt @@ -76,7 +76,7 @@ public open class KDocTag(node: ASTNode) : KDocElementImpl(node) { * Returns the content of this tag (all text following the tag name and the subject if present, * with leading asterisks removed). */ - public fun getContent(): String { + public open fun getContent(): String { val builder = StringBuilder() var contentStarted = false diff --git a/idea/src/org/jetbrains/kotlin/idea/JetQuickDocumentationProvider.java b/idea/src/org/jetbrains/kotlin/idea/JetQuickDocumentationProvider.java index ce40acac926..bd262f09ac6 100644 --- a/idea/src/org/jetbrains/kotlin/idea/JetQuickDocumentationProvider.java +++ b/idea/src/org/jetbrains/kotlin/idea/JetQuickDocumentationProvider.java @@ -26,8 +26,8 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.kotlin.asJava.KotlinLightMethod; import org.jetbrains.kotlin.descriptors.DeclarationDescriptor; import org.jetbrains.kotlin.idea.caches.resolve.ResolvePackage; +import org.jetbrains.kotlin.idea.kdoc.KDocFinder; import org.jetbrains.kotlin.idea.kdoc.KDocRenderer; -import org.jetbrains.kotlin.kdoc.KdocPackage; import org.jetbrains.kotlin.kdoc.psi.impl.KDocTag; import org.jetbrains.kotlin.psi.JetDeclaration; import org.jetbrains.kotlin.psi.JetPackageDirective; @@ -91,7 +91,7 @@ public class JetQuickDocumentationProvider extends AbstractDocumentationProvider renderedDecl = "
" + DescriptorRenderer.HTML_NAMES_WITH_SHORT_TYPES.render(declarationDescriptor) + "
"; } - KDocTag comment = KdocPackage.findKDoc(declarationDescriptor); + KDocTag comment = KDocFinder.INSTANCE$.findKDoc(declarationDescriptor); if (comment != null) { renderedDecl = renderedDecl + "
" + KDocRenderer.INSTANCE$.renderKDoc(comment); } diff --git a/idea/src/org/jetbrains/kotlin/idea/kdoc/KDocFinder.kt b/idea/src/org/jetbrains/kotlin/idea/kdoc/KDocFinder.kt new file mode 100644 index 00000000000..e3315c83578 --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/kdoc/KDocFinder.kt @@ -0,0 +1,58 @@ +/* + * Copyright 2010-2015 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.kotlin.idea.kdoc + +import org.jetbrains.kotlin.descriptors.DeclarationDescriptor +import org.jetbrains.kotlin.kdoc.psi.impl.KDocTag +import org.jetbrains.kotlin.descriptors.DeclarationDescriptorWithSource +import org.jetbrains.kotlin.resolve.source.PsiSourceElement +import org.jetbrains.kotlin.psi.JetDeclaration +import org.jetbrains.kotlin.descriptors.ConstructorDescriptor +import org.jetbrains.kotlin.descriptors.CallableDescriptor +import org.jetbrains.kotlin.kdoc.parser.KDocKnownTag + +object KDocFinder { + fun findKDoc(declaration: DeclarationDescriptor): KDocTag? { + if (declaration is DeclarationDescriptorWithSource) { + val psiDeclaration = (declaration.getSource() as? PsiSourceElement)?.psi?.getNavigationElement() + if (psiDeclaration is JetDeclaration) { + val kdoc = psiDeclaration.getDocComment() + if (kdoc != null) { + if (declaration is ConstructorDescriptor) { + // ConstructorDescriptor resolves to the same JetDeclaration + val constructorSection = kdoc.findSectionByTag(KDocKnownTag.CONSTRUCTOR) + if (constructorSection != null) { + return constructorSection + } + } + return kdoc.getDefaultSection() + } + } + } + + if (declaration is CallableDescriptor) { + for (baseDescriptor in declaration.getOverriddenDescriptors()) { + val baseKDoc = findKDoc(baseDescriptor) + if (baseKDoc != null) { + return baseKDoc + } + } + } + + return null + } +} diff --git a/idea/testData/kdoc/finder/Constructor.kt b/idea/testData/kdoc/finder/Constructor.kt new file mode 100644 index 00000000000..badef1afd62 --- /dev/null +++ b/idea/testData/kdoc/finder/Constructor.kt @@ -0,0 +1,6 @@ +/** + * Doc for class C. + * @constructor Doc for constructor of class C. + */ +class C() { +} diff --git a/idea/tests/org/jetbrains/kotlin/idea/kdoc/KDocFinderTest.kt b/idea/tests/org/jetbrains/kotlin/idea/kdoc/KDocFinderTest.kt new file mode 100644 index 00000000000..fdb6313fbd1 --- /dev/null +++ b/idea/tests/org/jetbrains/kotlin/idea/kdoc/KDocFinderTest.kt @@ -0,0 +1,39 @@ +/* + * Copyright 2010-2015 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.kotlin.idea.kdoc + +import org.jetbrains.kotlin.idea.PluginTestCaseBase +import com.intellij.testFramework.fixtures.LightPlatformCodeInsightFixtureTestCase +import org.jetbrains.kotlin.psi.JetFile +import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptor +import org.junit.Assert +import org.jetbrains.kotlin.descriptors.ClassDescriptor + +public class KDocFinderTest() : LightPlatformCodeInsightFixtureTestCase() { + override fun getTestDataPath(): String { + return PluginTestCaseBase.getTestDataPathBase() + "/kdoc/finder/" + } + + public fun testConstructor() { + myFixture.configureByFile(getTestName(false) + ".kt") + val declaration = (myFixture.getFile() as JetFile).getDeclarations()[0] + val descriptor = declaration.resolveToDescriptor() as ClassDescriptor + val constructorDescriptor = descriptor.getUnsubstitutedPrimaryConstructor() + val doc = KDocFinder.findKDoc(constructorDescriptor) + Assert.assertEquals("Doc for constructor of class C.", doc!!.getContent()) + } +}