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