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

This commit is contained in:
Dmitry Jemerov
2015-02-06 18:13:35 +01:00
parent fdfb843fdc
commit c366da95ac
7 changed files with 117 additions and 59 deletions
@@ -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
}
@@ -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<KDocTag> {
return getChildrenOfType<KDocTag>().filter { it.getName() == name }
}
@@ -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
@@ -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 = "<pre>" + DescriptorRenderer.HTML_NAMES_WITH_SHORT_TYPES.render(declarationDescriptor) + "</pre>";
}
KDocTag comment = KdocPackage.findKDoc(declarationDescriptor);
KDocTag comment = KDocFinder.INSTANCE$.findKDoc(declarationDescriptor);
if (comment != null) {
renderedDecl = renderedDecl + "<br/>" + KDocRenderer.INSTANCE$.renderKDoc(comment);
}
@@ -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
}
}
+6
View File
@@ -0,0 +1,6 @@
/**
* Doc for class C.
* @constructor Doc for constructor of class C.
*/
class C() {
}
@@ -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())
}
}