add references to kdoc; support resolve and rename

This commit is contained in:
Dmitry Jemerov
2015-02-03 17:24:45 +01:00
parent 40f459306a
commit c0e7d1faf2
27 changed files with 575 additions and 70 deletions
@@ -18,8 +18,10 @@ package org.jetbrains.kotlin.kdoc.psi.api;
import com.intellij.psi.PsiComment;
import org.jetbrains.kotlin.kdoc.psi.impl.KDocSection;
import org.jetbrains.kotlin.psi.JetDeclaration;
// Don't implement JetElement (or it will be treated as statement)
public interface KDoc extends PsiComment {
JetDeclaration getOwner();
KDocSection getDefaultSection();
}
@@ -1,54 +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.psi.impl;
import com.intellij.lang.Language;
import com.intellij.psi.impl.source.tree.LazyParseablePsiElement;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.idea.JetLanguage;
import org.jetbrains.kotlin.kdoc.lexer.KDocTokens;
import org.jetbrains.kotlin.kdoc.psi.api.KDoc;
import org.jetbrains.kotlin.lexer.JetTokens;
public class KDocImpl extends LazyParseablePsiElement implements KDoc {
public KDocImpl(CharSequence buffer) {
super(KDocTokens.KDOC, buffer);
}
@NotNull
@Override
public Language getLanguage() {
return JetLanguage.INSTANCE;
}
@Override
public String toString() {
return getNode().getElementType().toString();
}
@Override
public IElementType getTokenType() {
return JetTokens.DOC_COMMENT;
}
@Override
public KDocSection getDefaultSection() {
return PsiTreeUtil.getChildOfType(this, KDocSection.class);
}
}
@@ -0,0 +1,41 @@
/*
* 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.psi.impl
import com.intellij.lang.Language
import com.intellij.psi.impl.source.tree.LazyParseablePsiElement
import com.intellij.psi.tree.IElementType
import org.jetbrains.kotlin.idea.JetLanguage
import org.jetbrains.kotlin.kdoc.lexer.KDocTokens
import org.jetbrains.kotlin.kdoc.psi.api.KDoc
import org.jetbrains.kotlin.lexer.JetTokens
import org.jetbrains.kotlin.psi.JetDeclaration
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
import org.jetbrains.kotlin.psi.psiUtil.getChildOfType
public class KDocImpl(buffer: CharSequence?) : LazyParseablePsiElement(KDocTokens.KDOC, buffer), KDoc {
override fun getLanguage(): Language = JetLanguage.INSTANCE
override fun toString(): String = getNode().getElementType().toString()
override fun getTokenType(): IElementType = JetTokens.DOC_COMMENT
override fun getOwner(): JetDeclaration = getParentOfType<JetDeclaration>(true)!!
override fun getDefaultSection(): KDocSection = getChildOfType<KDocSection>()!!
}
@@ -17,13 +17,32 @@
package org.jetbrains.kotlin.kdoc.psi.impl
import com.intellij.lang.ASTNode
import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiReference
import com.intellij.psi.impl.source.resolve.reference.ReferenceProvidersRegistry
import org.jetbrains.kotlin.psi.JetElementImpl
import org.jetbrains.kotlin.kdoc.psi.api.KDoc
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
public class KDocLink(node: ASTNode) : KDocElementImpl(node) {
fun getLinkText(): String {
public class KDocLink(node: ASTNode) : JetElementImpl(node) {
public fun getLinkText(): String = getLinkTextRange().substring(getText())
public fun getLinkTextRange(): TextRange {
val text = getText()
if (text.startsWith('[') && text.endsWith(']')) {
return text.substring(1, text.length() - 1)
return TextRange(1, text.length()-1)
}
return text
return TextRange(0, text.length())
}
/**
* If this link is the subject of a tag, returns the tag. Otherwise, returns null.
*/
public fun getTagIfSubject(): KDocTag? {
val tag = getStrictParentOfType<KDocTag>()
return if (tag != null && tag.getSubjectLink() == this) tag else null
}
override fun getReferences(): Array<out PsiReference>? =
ReferenceProvidersRegistry.getReferencesFromProviders(this)
}
@@ -18,10 +18,38 @@ package org.jetbrains.kotlin.kdoc.psi.impl
import com.intellij.lang.ASTNode
import org.jetbrains.kotlin.psi.JetElementImpl
import org.jetbrains.kotlin.kdoc.psi.api.KDoc
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
import com.intellij.openapi.util.TextRange
import org.jetbrains.kotlin.lexer.JetTokens
import org.jetbrains.kotlin.psi.psiUtil.getChildOfType
/**
* A single part of a qualified name in the tag subject or link.
*/
public class KDocName(node: ASTNode): JetElementImpl(node) {
public fun getContainingDoc(): KDoc {
val kdoc = getStrictParentOfType<KDoc>()
if (kdoc == null) {
throw IllegalStateException("KDocName must be inside a KDoc")
}
return kdoc
}
public fun getQualifier(): KDocName? = getChildOfType()
public fun getNameTextRange(): TextRange {
val dot = getNode().findChildByType(JetTokens.DOT)
val textRange = getTextRange()
val nameStart = if (dot != null) dot.getTextRange().getEndOffset() - textRange.getStartOffset() else 0
return TextRange(nameStart, textRange.getLength())
}
public fun getNameText(): String = getNameTextRange().substring(getText())
public fun getQualifiedName(): List<String> {
val qualifier = getQualifier()
val nameAsList = listOf(getNameText())
return if (qualifier != null) qualifier.getQualifiedName() + nameAsList else nameAsList
}
}
@@ -17,7 +17,7 @@
package org.jetbrains.kotlin.kdoc.psi.impl
import com.intellij.lang.ASTNode
import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.kotlin.psi.psiUtil.getChildrenOfType
/**
* The part of a doc comment which describes a single class, method or property
@@ -27,11 +27,7 @@ import com.intellij.psi.util.PsiTreeUtil
*/
public class KDocSection(node: ASTNode) : KDocTag(node) {
public fun findTagsByName(name: String): List<KDocTag> {
val tags = PsiTreeUtil.getChildrenOfType<KDocTag>(this, javaClass<KDocTag>())
if (tags == null) {
return listOf()
}
return tags.filter { it.getName() == name }
return getChildrenOfType<KDocTag>().filter { it.getName() == name }
}
public fun findTagByName(name: String): KDocTag?
@@ -44,10 +44,12 @@ public open class KDocTag(node: ASTNode) : KDocElementImpl(node) {
* Returns the name of the entity documented by this tag (for example, the name of the parameter
* for the @param tag), or null if this tag does not document any specific entity.
*/
public fun getSubjectName(): String? {
public fun getSubjectName(): String? = getSubjectLink()?.getLinkText()
public fun getSubjectLink(): KDocLink? {
val children = childrenAfterTagName()
if (hasSubject(children)) {
return (children.firstOrNull()?.getPsi() as? KDocLink)?.getLinkText()
return children.firstOrNull()?.getPsi() as? KDocLink
}
return null
}
@@ -66,6 +68,10 @@ public open class KDocTag(node: ASTNode) : KDocElementImpl(node) {
.dropWhile { it.getElementType() == KDocTokens.TAG_NAME }
.dropWhile { it.getElementType() == TokenType.WHITE_SPACE }
/**
* Returns the contents of this tag (all text following the tag name and the subject if present,
* with leading asterisks removed).
*/
public fun getContent(): String {
val builder = StringBuilder()
@@ -87,6 +87,14 @@ inline public fun PsiElement.getNonStrictParentOfType<reified T: PsiElement>():
return PsiTreeUtil.getParentOfType(this, javaClass<T>(), false)
}
inline public fun PsiElement.getChildOfType<reified T: PsiElement>(): T? {
return PsiTreeUtil.getChildOfType(this, javaClass<T>())
}
inline public fun PsiElement.getChildrenOfType<reified T: PsiElement>(): Array<T> {
return PsiTreeUtil.getChildrenOfType(this, javaClass<T>()) ?: array()
}
public fun PsiElement?.isAncestor(element: PsiElement, strict: Boolean = false): Boolean {
return PsiTreeUtil.isAncestor(this, element, strict)
}
@@ -25,6 +25,7 @@ import org.jetbrains.kotlin.descriptors.impl.SimpleFunctionDescriptorImpl;
import org.jetbrains.kotlin.descriptors.impl.TypeParameterDescriptorImpl;
import org.jetbrains.kotlin.descriptors.impl.ValueParameterDescriptorImpl;
import org.jetbrains.kotlin.resolve.scopes.JetScope;
import org.jetbrains.kotlin.resolve.scopes.RedeclarationHandler;
import org.jetbrains.kotlin.resolve.scopes.WritableScope;
import org.jetbrains.kotlin.resolve.scopes.WritableScopeImpl;
import org.jetbrains.kotlin.types.*;
@@ -69,7 +70,17 @@ public class FunctionDescriptorUtil {
@NotNull
public static JetScope getFunctionInnerScope(@NotNull JetScope outerScope, @NotNull FunctionDescriptor descriptor, @NotNull BindingTrace trace) {
WritableScope parameterScope = new WritableScopeImpl(outerScope, descriptor, new TraceBasedRedeclarationHandler(trace), "Function inner scope");
TraceBasedRedeclarationHandler redeclarationHandler = new TraceBasedRedeclarationHandler(trace);
return getFunctionInnerScope(outerScope, descriptor, redeclarationHandler);
}
@NotNull
public static JetScope getFunctionInnerScope(
@NotNull JetScope outerScope,
@NotNull FunctionDescriptor descriptor,
RedeclarationHandler redeclarationHandler
) {
WritableScope parameterScope = new WritableScopeImpl(outerScope, descriptor, redeclarationHandler, "Function inner scope");
ReceiverParameterDescriptor receiver = descriptor.getExtensionReceiverParameter();
if (receiver != null) {
parameterScope.setImplicitReceiver(receiver);
@@ -67,6 +67,19 @@ public final class JetScopeUtils {
return accessorScope;
}
public static JetScope getPropertyDeclarationInnerScope(
@NotNull PropertyDescriptor propertyDescriptor,
@NotNull JetScope outerScope,
RedeclarationHandler redeclarationHandler
) {
return getPropertyDeclarationInnerScope(propertyDescriptor,
outerScope,
propertyDescriptor.getTypeParameters(),
propertyDescriptor.getExtensionReceiverParameter(),
redeclarationHandler,
true);
}
public static JetScope getPropertyDeclarationInnerScope(
@NotNull PropertyDescriptor propertyDescriptor,
@NotNull JetScope outerScope,
@@ -94,9 +107,23 @@ public final class JetScopeUtils {
@Nullable ReceiverParameterDescriptor receiver,
BindingTrace trace,
boolean addLabelForProperty
) {
TraceBasedRedeclarationHandler redeclarationHandler = new TraceBasedRedeclarationHandler(trace);
return getPropertyDeclarationInnerScope(propertyDescriptor, outerScope, typeParameters, receiver, redeclarationHandler,
addLabelForProperty);
}
@NotNull
private static JetScope getPropertyDeclarationInnerScope(
@Nullable PropertyDescriptor propertyDescriptor,
@NotNull JetScope outerScope,
@NotNull List<? extends TypeParameterDescriptor> typeParameters,
@Nullable ReceiverParameterDescriptor receiver,
RedeclarationHandler redeclarationHandler,
boolean addLabelForProperty
) {
WritableScopeImpl result = new WritableScopeImpl(
outerScope, outerScope.getContainingDeclaration(), new TraceBasedRedeclarationHandler(trace),
outerScope, outerScope.getContainingDeclaration(), redeclarationHandler,
"Property declaration inner scope");
if (addLabelForProperty) {
assert propertyDescriptor != null : "PropertyDescriptor can be null for property scope which hasn't label to property";