add references to kdoc; support resolve and rename
This commit is contained in:
@@ -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";
|
||||
|
||||
@@ -669,6 +669,10 @@ fun main(args: Array<String>) {
|
||||
testClass(javaClass<AbstractKotlinCoverageOutputFilesTest>()) {
|
||||
model("coverage/outputFiles")
|
||||
}
|
||||
|
||||
testClass(javaClass<AbstractReferenceResolveTest>(), "org.jetbrains.kotlin.idea.kdoc.KdocResolveTestGenerated") {
|
||||
model("kdoc/resolve")
|
||||
}
|
||||
}
|
||||
|
||||
testGroup("idea/tests", "compiler/testData") {
|
||||
@@ -733,10 +737,14 @@ private class TestGroup(val testsRoot: String, val testDataRoot: String) {
|
||||
val testClass = TestClass()
|
||||
testClass.init()
|
||||
|
||||
val lastDot = suiteTestClass.lastIndexOf('.')
|
||||
val suiteTestClassName = if (lastDot == -1) suiteTestClass else suiteTestClass.substring(lastDot+1)
|
||||
val suiteTestClassPackage = if (lastDot == -1) baseTestClass.getPackage().getName() else suiteTestClass.substring(0, lastDot)
|
||||
|
||||
TestGenerator(
|
||||
testsRoot,
|
||||
baseTestClass.getPackage()!!.getName()!!,
|
||||
suiteTestClass,
|
||||
suiteTestClassPackage,
|
||||
suiteTestClassName,
|
||||
baseTestClass,
|
||||
testClass.testModels
|
||||
).generateAndSave()
|
||||
|
||||
@@ -48,6 +48,7 @@ public class TestGenerator {
|
||||
private static final Set<String> GENERATED_FILES = ContainerUtil.newHashSet();
|
||||
private static final Class RUNNER = JUnit3RunnerWithInners.class;
|
||||
|
||||
private final String baseTestClassPackage;
|
||||
private final String suiteClassPackage;
|
||||
private final String suiteClassName;
|
||||
private final String baseTestClassName;
|
||||
@@ -63,6 +64,7 @@ public class TestGenerator {
|
||||
) {
|
||||
this.suiteClassPackage = suiteClassPackage;
|
||||
this.suiteClassName = suiteClassName;
|
||||
this.baseTestClassPackage = baseTestClass.getPackage().getName();
|
||||
this.baseTestClassName = baseTestClass.getSimpleName();
|
||||
this.testClassModels = Lists.newArrayList(testClassModels);
|
||||
|
||||
@@ -84,6 +86,9 @@ public class TestGenerator {
|
||||
p.println("import " + InnerTestClasses.class.getCanonicalName() + ";");
|
||||
p.println("import ", RUNNER.getCanonicalName(), ";");
|
||||
p.println("import " + JetTestUtils.class.getCanonicalName() + ";");
|
||||
if (!suiteClassPackage.equals(baseTestClassPackage)) {
|
||||
p.println("import " + baseTestClassPackage + "." + baseTestClassName + ";");
|
||||
}
|
||||
p.println("import " + TestMetadata.class.getCanonicalName() + ";");
|
||||
p.println("import " + RunWith.class.getCanonicalName() + ";");
|
||||
p.println();
|
||||
|
||||
@@ -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.idea.kdoc
|
||||
|
||||
import com.intellij.openapi.project.Project
|
||||
import org.jetbrains.kotlin.kdoc.psi.api.KDoc
|
||||
import org.jetbrains.kotlin.psi.JetPsiFactory
|
||||
import org.jetbrains.kotlin.psi.JetFunction
|
||||
import com.intellij.psi.util.PsiTreeUtil
|
||||
import org.jetbrains.kotlin.kdoc.psi.impl.KDocName
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getChildOfType
|
||||
|
||||
class KDocElementFactory(val project: Project) {
|
||||
public fun createKDocFromText(text: String): KDoc {
|
||||
val fileText = text + " fun foo { }"
|
||||
val function = JetPsiFactory(project).createDeclaration<JetFunction>(fileText)
|
||||
return PsiTreeUtil.findChildOfType(function, javaClass<KDoc>())!!
|
||||
}
|
||||
|
||||
public fun createNameFromText(text: String): KDocName {
|
||||
val kdoc = createKDocFromText("/** @param $text foo*/")
|
||||
val section = kdoc.getDefaultSection()
|
||||
val tag = section.findTagByName("param")
|
||||
val link = tag!!.getSubjectLink()!!
|
||||
return link.getChildOfType<KDocName>()!!
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,172 @@
|
||||
/*
|
||||
* 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.kdoc.psi.impl.KDocLink
|
||||
import com.intellij.openapi.util.TextRange
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
||||
import org.jetbrains.kotlin.idea.references.JetMultiReference
|
||||
import org.jetbrains.kotlin.kdoc.psi.impl.KDocTag
|
||||
import org.jetbrains.kotlin.descriptors.CallableDescriptor
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptorNonRoot
|
||||
import org.jetbrains.kotlin.resolve.scopes.JetScope
|
||||
import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.PackageViewDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
|
||||
import org.jetbrains.kotlin.resolve.scopes.RedeclarationHandler
|
||||
import org.jetbrains.kotlin.resolve.scopes.WritableScopeImpl
|
||||
import org.jetbrains.kotlin.resolve.scopes.WritableScope
|
||||
import org.jetbrains.kotlin.resolve.scopes.ChainedScope
|
||||
import org.jetbrains.kotlin.resolve.FunctionDescriptorUtil
|
||||
import org.jetbrains.kotlin.resolve.scopes.JetScopeUtils
|
||||
import org.jetbrains.kotlin.kdoc.psi.impl.KDocName
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
|
||||
import org.jetbrains.kotlin.descriptors.ClassifierDescriptor
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.KotlinCacheService
|
||||
import org.jetbrains.kotlin.resolve.lazy.KotlinCodeAnalyzer
|
||||
import org.jetbrains.kotlin.resolve.source.PsiSourceElement
|
||||
import org.jetbrains.kotlin.psi.JetFile
|
||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptorWithSource
|
||||
|
||||
public class KDocReference(element: KDocName): JetMultiReference<KDocName>(element) {
|
||||
override fun getTargetDescriptors(context: BindingContext): Collection<DeclarationDescriptor> {
|
||||
val cacheService = KotlinCacheService.getInstance(getElement().getProject())
|
||||
val session = cacheService.getLazyResolveSession(getElement())
|
||||
val declaration = getElement().getContainingDoc().getOwner()
|
||||
if (declaration == null) {
|
||||
return arrayListOf()
|
||||
}
|
||||
val declarationDescriptor = context.get(BindingContext.DECLARATION_TO_DESCRIPTOR, declaration)
|
||||
val kdocLink = getElement().getStrictParentOfType<KDocLink>()!!
|
||||
return resolveKDocLink(session, declarationDescriptor, kdocLink.getTagIfSubject(), getElement().getQualifiedName())
|
||||
}
|
||||
|
||||
override fun getRangeInElement(): TextRange = getElement().getNameTextRange()
|
||||
|
||||
override fun canRename(): Boolean = true
|
||||
|
||||
override fun handleElementRename(newElementName: String?): PsiElement? {
|
||||
val textRange = getElement().getNameTextRange()
|
||||
val newText = textRange.replace(getElement().getText(), newElementName)
|
||||
val newLink = KDocElementFactory(getElement().getProject()).createNameFromText(newText)
|
||||
return getElement().replace(newLink)
|
||||
}
|
||||
}
|
||||
|
||||
public fun resolveKDocLink(session: KotlinCodeAnalyzer,
|
||||
fromDescriptor: DeclarationDescriptor,
|
||||
fromSubjectOfTag: KDocTag?,
|
||||
qualifiedName: List<String>): Collection<DeclarationDescriptor> {
|
||||
if (fromSubjectOfTag?.getName() == "param") {
|
||||
return resolveParamLink(fromDescriptor, qualifiedName)
|
||||
}
|
||||
|
||||
var result: Collection<DeclarationDescriptor> = listOf(fromDescriptor)
|
||||
qualifiedName.forEach { nameComponent ->
|
||||
if (result.size() != 1) return listOf()
|
||||
val scope = getResolutionScope(session, result.first())
|
||||
result = scope.getDescriptors().filter { it.getName().asString() == nameComponent }
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
private fun resolveParamLink(fromDescriptor: DeclarationDescriptor, qualifiedName: List<String>): List<DeclarationDescriptor> {
|
||||
if (qualifiedName.size() != 1) {
|
||||
return listOf()
|
||||
}
|
||||
if (fromDescriptor is CallableDescriptor) {
|
||||
val valueParams = fromDescriptor.getValueParameters().filter { it.getName().asString() == qualifiedName[0] }
|
||||
if (valueParams.isNotEmpty()) {
|
||||
return valueParams
|
||||
}
|
||||
}
|
||||
if (fromDescriptor is ClassifierDescriptor) {
|
||||
val typeParams = fromDescriptor.getTypeConstructor().getParameters().filter { it.getName().asString() == qualifiedName[0] }
|
||||
if (typeParams.isNotEmpty()) {
|
||||
return typeParams
|
||||
}
|
||||
}
|
||||
|
||||
return listOf()
|
||||
}
|
||||
|
||||
private fun getPackageInnerScope(descriptor: PackageFragmentDescriptor): JetScope {
|
||||
val module = descriptor.getContainingDeclaration()
|
||||
val packageView = module.getPackage(descriptor.fqName)
|
||||
val packageScope = packageView!!.getMemberScope()
|
||||
return packageScope
|
||||
}
|
||||
|
||||
private fun getClassInnerScope(outerScope: JetScope, descriptor: ClassDescriptor): JetScope {
|
||||
val redeclarationHandler = RedeclarationHandler.DO_NOTHING
|
||||
|
||||
val headerScope = WritableScopeImpl(outerScope, descriptor, redeclarationHandler, "Class ${descriptor.getName()} header scope")
|
||||
for (typeParameter in descriptor.getTypeConstructor().getParameters()) {
|
||||
headerScope.addTypeParameterDescriptor(typeParameter)
|
||||
}
|
||||
for (constructor in descriptor.getConstructors()) {
|
||||
headerScope.addFunctionDescriptor(constructor)
|
||||
}
|
||||
headerScope.addLabeledDeclaration(descriptor)
|
||||
headerScope.changeLockLevel(WritableScope.LockLevel.READING)
|
||||
|
||||
val classScope = ChainedScope(descriptor, "Class ${descriptor.getName()} scope", descriptor.getDefaultType().getMemberScope(), headerScope)
|
||||
return classScope
|
||||
}
|
||||
|
||||
private fun getResolutionScope(session: KotlinCodeAnalyzer, descriptor: DeclarationDescriptor): JetScope {
|
||||
when (descriptor) {
|
||||
is PackageFragmentDescriptor ->
|
||||
return getPackageInnerScope(descriptor)
|
||||
|
||||
is PackageViewDescriptor ->
|
||||
return descriptor.getMemberScope()
|
||||
|
||||
is ClassDescriptor ->
|
||||
return getClassInnerScope(getOuterScope(descriptor, session), descriptor)
|
||||
|
||||
is FunctionDescriptor ->
|
||||
return FunctionDescriptorUtil.getFunctionInnerScope(getOuterScope(descriptor, session),
|
||||
descriptor, RedeclarationHandler.DO_NOTHING)
|
||||
|
||||
is PropertyDescriptor ->
|
||||
return JetScopeUtils.getPropertyDeclarationInnerScope(descriptor,
|
||||
getOuterScope(descriptor, session),
|
||||
RedeclarationHandler.DO_NOTHING)
|
||||
}
|
||||
|
||||
if (descriptor is DeclarationDescriptorNonRoot)
|
||||
return getOuterScope(descriptor, session)
|
||||
|
||||
throw IllegalArgumentException("Cannot find resolution scope for root $descriptor")
|
||||
}
|
||||
|
||||
private fun getOuterScope(descriptor: DeclarationDescriptorWithSource, session: KotlinCodeAnalyzer): JetScope {
|
||||
val parent = descriptor.getContainingDeclaration()
|
||||
if (parent is PackageFragmentDescriptor) {
|
||||
val containingFile = (descriptor.getSource() as? PsiSourceElement)?.psi?.getContainingFile() as? JetFile
|
||||
if (containingFile != null) {
|
||||
return session.getScopeProvider().getFileScope(containingFile)
|
||||
}
|
||||
}
|
||||
return getResolutionScope(session, parent)
|
||||
}
|
||||
@@ -20,6 +20,8 @@ import com.intellij.psi.*
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import com.intellij.util.ProcessingContext
|
||||
import com.intellij.patterns.PlatformPatterns
|
||||
import org.jetbrains.kotlin.idea.kdoc.KDocReference
|
||||
import org.jetbrains.kotlin.kdoc.psi.impl.KDocName
|
||||
|
||||
public class JetReferenceContributor() : PsiReferenceContributor() {
|
||||
public override fun registerReferenceProviders(registrar: PsiReferenceRegistrar) {
|
||||
@@ -45,6 +47,9 @@ public class JetReferenceContributor() : PsiReferenceContributor() {
|
||||
registerProvider(javaClass<JetMultiDeclaration>()) {
|
||||
JetMultiDeclarationReference(it)
|
||||
}
|
||||
registerProvider(javaClass<KDocName>()) {
|
||||
KDocReference(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
/**
|
||||
* This is the same as [bar]
|
||||
*/
|
||||
fun foo() {}
|
||||
|
||||
fun <caret>bar() {}
|
||||
@@ -0,0 +1,6 @@
|
||||
/**
|
||||
* This is the same as [xyzzy]
|
||||
*/
|
||||
fun foo() {}
|
||||
|
||||
fun xyzzy() {}
|
||||
@@ -0,0 +1,4 @@
|
||||
/**
|
||||
* @param s this is a string
|
||||
*/
|
||||
fun f(<caret>s: String) { }
|
||||
@@ -0,0 +1,4 @@
|
||||
/**
|
||||
* @param bar this is a string
|
||||
*/
|
||||
fun f(bar: String) { }
|
||||
@@ -0,0 +1,8 @@
|
||||
/**
|
||||
* This is the same as [<caret>bar]
|
||||
*/
|
||||
fun foo() {}
|
||||
|
||||
fun bar() {}
|
||||
|
||||
// REF: (in <root>).bar()
|
||||
@@ -0,0 +1,10 @@
|
||||
import java.util.Properties
|
||||
|
||||
/**
|
||||
* This is almost like [Propert<caret>ies]
|
||||
*/
|
||||
class C {
|
||||
|
||||
}
|
||||
|
||||
// REF: (java.util).Properties
|
||||
@@ -0,0 +1,6 @@
|
||||
/**
|
||||
* @param <caret>s this is a string
|
||||
*/
|
||||
fun f(s: String) { }
|
||||
|
||||
// REF: s
|
||||
@@ -0,0 +1,6 @@
|
||||
/**
|
||||
* The type [<caret>T] is the type on which the foo property is defined.
|
||||
*/
|
||||
val <T> T.foo: Int get() = 0
|
||||
|
||||
// REF: T
|
||||
@@ -0,0 +1,10 @@
|
||||
/**
|
||||
* This is the same as [C.<caret>bar]
|
||||
*/
|
||||
fun foo() {}
|
||||
|
||||
class C {
|
||||
fun bar() {}
|
||||
}
|
||||
|
||||
// REF: (in C).bar()
|
||||
@@ -0,0 +1,6 @@
|
||||
/**
|
||||
* @param <caret>T this is the type
|
||||
*/
|
||||
class Foo<T> {}
|
||||
|
||||
// REF: T
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* 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 com.intellij.codeInsight.TargetElementUtilBase;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.refactoring.rename.RenameProcessor;
|
||||
import com.intellij.testFramework.LightCodeInsightTestCase;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.kotlin.idea.PluginTestCaseBase;
|
||||
|
||||
public class KdocRenameTest extends LightCodeInsightTestCase {
|
||||
@NotNull
|
||||
@Override
|
||||
protected String getTestDataPath() {
|
||||
return PluginTestCaseBase.getTestDataPathBase() + "/kdoc/rename/";
|
||||
}
|
||||
|
||||
public void testParamReference() throws Exception {
|
||||
doTest("bar");
|
||||
}
|
||||
|
||||
public void testCodeReference() throws Exception {
|
||||
doTest("xyzzy");
|
||||
}
|
||||
|
||||
private void doTest(String newName) throws Exception {
|
||||
configureByFile(getTestName(false) + ".kt");
|
||||
PsiElement element = TargetElementUtilBase
|
||||
.findTargetElement(myEditor,
|
||||
TargetElementUtilBase.ELEMENT_NAME_ACCEPTED | TargetElementUtilBase.REFERENCED_ELEMENT_ACCEPTED);
|
||||
assertNotNull(element);
|
||||
new RenameProcessor(getProject(), element, newName, true, true).run();
|
||||
checkResultByFile(getTestName(false) + ".kt.after");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* 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 com.intellij.testFramework.TestDataPath;
|
||||
import org.jetbrains.kotlin.idea.resolve.AbstractReferenceResolveTest;
|
||||
import org.jetbrains.kotlin.test.JUnit3RunnerWithInners;
|
||||
import org.jetbrains.kotlin.test.JetTestUtils;
|
||||
import org.jetbrains.kotlin.test.TestMetadata;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */
|
||||
@SuppressWarnings("all")
|
||||
@TestMetadata("idea/testData/kdoc/resolve")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public class KdocResolveTestGenerated extends AbstractReferenceResolveTest {
|
||||
public void testAllFilesPresentInResolve() throws Exception {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/kdoc/resolve"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("CodeReference.kt")
|
||||
public void testCodeReference() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/kdoc/resolve/CodeReference.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("ImportedClassReference.kt")
|
||||
public void testImportedClassReference() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/kdoc/resolve/ImportedClassReference.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("ParamReference.kt")
|
||||
public void testParamReference() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/kdoc/resolve/ParamReference.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("PropertyTypeParamReference.kt")
|
||||
public void testPropertyTypeParamReference() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/kdoc/resolve/PropertyTypeParamReference.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("QualifiedCodeReference.kt")
|
||||
public void testQualifiedCodeReference() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/kdoc/resolve/QualifiedCodeReference.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("TypeParamReference.kt")
|
||||
public void testTypeParamReference() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/kdoc/resolve/TypeParamReference.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user