diff --git a/idea/src/org/jetbrains/jet/plugin/references/AbstractPolyVariantJetReference.java b/idea/src/org/jetbrains/jet/plugin/references/AbstractPolyVariantJetReference.java deleted file mode 100644 index a59843b297c..00000000000 --- a/idea/src/org/jetbrains/jet/plugin/references/AbstractPolyVariantJetReference.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Copyright 2010-2013 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.jet.plugin.references; - -import com.intellij.psi.PsiElement; -import com.intellij.psi.PsiPolyVariantReference; -import com.intellij.psi.ResolveResult; -import com.intellij.util.IncorrectOperationException; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.jet.lang.psi.JetElement; - -public abstract class AbstractPolyVariantJetReference implements PsiPolyVariantReference { - protected final T element; - - public AbstractPolyVariantJetReference(@NotNull T element) { - this.element = element; - } - - @Override - public T getElement() { - return element; - } - - @Nullable - @Override - public PsiElement resolve() { - ResolveResult[] results = multiResolve(false); - if (results.length == 1) return results[0].getElement(); - return null; - } - - @Override - public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException { - throw new IncorrectOperationException(); - } - - @Override - public PsiElement bindToElement(@NotNull PsiElement element) throws IncorrectOperationException { - throw new IncorrectOperationException(); - } - - @Override - public boolean isReferenceTo(PsiElement element) { - if (element == null) return false; - - ResolveResult[] results = multiResolve(false); - for (ResolveResult result : results) { - if (element.equals(result.getElement())) return true; - } - return false; - } - - @NotNull - @Override - public Object[] getVariants() { - return EMPTY_ARRAY; - } - - @Override - public boolean isSoft() { - return false; - } -} diff --git a/idea/src/org/jetbrains/jet/plugin/references/JetArrayAccessReference.java b/idea/src/org/jetbrains/jet/plugin/references/JetArrayAccessReference.java index 9c1f4167688..899b3c48360 100644 --- a/idea/src/org/jetbrains/jet/plugin/references/JetArrayAccessReference.java +++ b/idea/src/org/jetbrains/jet/plugin/references/JetArrayAccessReference.java @@ -20,9 +20,7 @@ import com.google.common.collect.Lists; import com.intellij.lang.ASTNode; import com.intellij.openapi.util.TextRange; import com.intellij.psi.MultiRangeReference; -import com.intellij.psi.PsiReference; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; import org.jetbrains.jet.lang.descriptors.FunctionDescriptor; import org.jetbrains.jet.lang.psi.JetArrayAccessExpression; @@ -38,11 +36,7 @@ import java.util.List; import static org.jetbrains.jet.lang.resolve.BindingContext.INDEXED_LVALUE_GET; import static org.jetbrains.jet.lang.resolve.BindingContext.INDEXED_LVALUE_SET; -class JetArrayAccessReference extends AbstractJetReference implements MultiRangeReference { - - public static PsiReference[] create(JetArrayAccessExpression expression) { - return new PsiReference[] { new JetArrayAccessReference(expression) }; - } +class JetArrayAccessReference extends JetSimpleReference implements MultiRangeReference { public JetArrayAccessReference(@NotNull JetArrayAccessExpression expression) { super(expression); @@ -53,19 +47,19 @@ class JetArrayAccessReference extends AbstractJetReference getTargetDescriptors(@NotNull BindingContext context) { List result = Lists.newArrayList(); ResolvedCall getFunction = context.get(INDEXED_LVALUE_GET, getExpression()); if (getFunction != null) { - result.add(getFunction.getResultingDescriptor()); + result.add(getFunction.getCandidateDescriptor()); } ResolvedCall setFunction = context.get(INDEXED_LVALUE_SET, getExpression()); if (setFunction != null) { - result.add(setFunction.getResultingDescriptor()); + result.add(setFunction.getCandidateDescriptor()); } return result; diff --git a/idea/src/org/jetbrains/jet/plugin/references/JetForLoopInReference.java b/idea/src/org/jetbrains/jet/plugin/references/JetForLoopInReference.java deleted file mode 100644 index fedf28d36e9..00000000000 --- a/idea/src/org/jetbrains/jet/plugin/references/JetForLoopInReference.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Copyright 2010-2013 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.jet.plugin.references; - -import com.google.common.collect.Lists; -import com.intellij.lang.ASTNode; -import com.intellij.openapi.util.TextRange; -import com.intellij.psi.PsiReference; -import com.intellij.psi.ResolveResult; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.jet.lang.descriptors.FunctionDescriptor; -import org.jetbrains.jet.lang.psi.JetExpression; -import org.jetbrains.jet.lang.psi.JetForExpression; -import org.jetbrains.jet.lang.resolve.BindingContext; -import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall; -import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache; -import org.jetbrains.jet.util.slicedmap.WritableSlice; - -import java.util.List; - -public class JetForLoopInReference extends AbstractPolyVariantJetReference { - - public static PsiReference[] create(@NotNull JetForExpression delegate) { - return new PsiReference[] { new JetForLoopInReference(delegate) }; - } - - private JetForLoopInReference(@NotNull JetForExpression element) { - super(element); - } - - @NotNull - @Override - public ResolveResult[] multiResolve(boolean incompleteCode) { - BindingContext context = AnalyzerFacadeWithCache.getContextForElement(element); - - JetExpression loopRange = element.getLoopRange(); - if (loopRange == null) return ResolveResult.EMPTY_ARRAY; - - List results = Lists.newArrayList(); - - addResultsForKey(context, results, loopRange, BindingContext.LOOP_RANGE_ITERATOR_RESOLVED_CALL); - addResultsForKey(context, results, loopRange, BindingContext.LOOP_RANGE_NEXT_RESOLVED_CALL); - addResultsForKey(context, results, loopRange, BindingContext.LOOP_RANGE_HAS_NEXT_RESOLVED_CALL); - - return results.toArray(new ResolveResult[results.size()]); - } - - private void addResultsForKey( - BindingContext context, - List results, - JetExpression loopRange, - WritableSlice> key - ) { - ResolvedCall resolvedCall = context.get(key, loopRange); - if (resolvedCall == null) return; - - JetReferenceUtil.findPsiElements(element.getProject(), context, results, resolvedCall.getResultingDescriptor()); - } - - @Override - public TextRange getRangeInElement() { - ASTNode inKeywordNode = element.getInKeywordNode(); - if (inKeywordNode == null) return TextRange.EMPTY_RANGE; - - int offset = inKeywordNode.getPsi().getStartOffsetInParent(); - return new TextRange(offset, offset + inKeywordNode.getTextLength()); - } - - @NotNull - @Override - public String getCanonicalText() { - return ""; - } -} diff --git a/idea/src/org/jetbrains/jet/plugin/references/JetForLoopInReference.kt b/idea/src/org/jetbrains/jet/plugin/references/JetForLoopInReference.kt new file mode 100644 index 00000000000..22dae9aa683 --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/references/JetForLoopInReference.kt @@ -0,0 +1,51 @@ +/* + * Copyright 2010-2014 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.jet.plugin.references + +import com.intellij.openapi.util.TextRange +import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor +import org.jetbrains.jet.lang.psi.JetForExpression +import org.jetbrains.jet.lang.resolve.BindingContext +import java.util.Collections + +public class JetForLoopInReference(element: JetForExpression) : JetMultiReference(element) { + + override fun getRangeInElement(): TextRange { + val inKeywordNode = expression.getInKeywordNode() + if (inKeywordNode == null) + return TextRange.EMPTY_RANGE + + val offset = inKeywordNode.getPsi()!!.getStartOffsetInParent() + return TextRange(offset, offset + inKeywordNode.getTextLength()) + } + + override fun getTargetDescriptors(context: BindingContext): Collection { + val loopRange = expression.getLoopRange() + if (loopRange == null) { + return Collections.emptyList() + } + return LOOP_RANGE_KEYS.map { key -> context.get(key, loopRange)?.getCandidateDescriptor() }.filterNotNull() + } + + class object { + private val LOOP_RANGE_KEYS = array( + BindingContext.LOOP_RANGE_ITERATOR_RESOLVED_CALL, + BindingContext.LOOP_RANGE_NEXT_RESOLVED_CALL, + BindingContext.LOOP_RANGE_HAS_NEXT_RESOLVED_CALL + ) + } +} diff --git a/idea/src/org/jetbrains/jet/plugin/references/JetInvokeFunctionReference.java b/idea/src/org/jetbrains/jet/plugin/references/JetInvokeFunctionReference.java index 63cb5cb98b0..5fec3f128a4 100644 --- a/idea/src/org/jetbrains/jet/plugin/references/JetInvokeFunctionReference.java +++ b/idea/src/org/jetbrains/jet/plugin/references/JetInvokeFunctionReference.java @@ -19,9 +19,7 @@ package org.jetbrains.jet.plugin.references; import com.intellij.lang.ASTNode; import com.intellij.openapi.util.TextRange; import com.intellij.psi.MultiRangeReference; -import com.intellij.psi.PsiReference; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.lang.descriptors.CallableDescriptor; import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; import org.jetbrains.jet.lang.psi.*; @@ -37,11 +35,7 @@ import java.util.List; import static org.jetbrains.jet.lang.resolve.BindingContext.RESOLVED_CALL; -class JetInvokeFunctionReference extends AbstractJetReference implements MultiRangeReference { - - public static PsiReference[] create(@NotNull JetCallExpression expression) { - return new PsiReference[] { new JetInvokeFunctionReference(expression) }; - } +class JetInvokeFunctionReference extends JetSimpleReference implements MultiRangeReference { public JetInvokeFunctionReference(@NotNull JetCallExpression expression) { super(expression); @@ -52,15 +46,14 @@ class JetInvokeFunctionReference extends AbstractJetReference return getElement().getTextRange().shiftRight(-getElement().getTextOffset()); } - @Nullable @Override + @NotNull protected Collection getTargetDescriptors(@NotNull BindingContext context) { ResolvedCall resolvedCall = context.get(RESOLVED_CALL, getExpression().getCalleeExpression()); - if (resolvedCall instanceof VariableAsFunctionResolvedCall) { - return Collections.singleton(((VariableAsFunctionResolvedCall) resolvedCall).getResultingDescriptor()); + return Collections.singleton(((VariableAsFunctionResolvedCall) resolvedCall).getCandidateDescriptor()); } - return null; + return Collections.emptyList(); } @Override diff --git a/idea/src/org/jetbrains/jet/plugin/references/JetPropertyDelegationMethodsReference.java b/idea/src/org/jetbrains/jet/plugin/references/JetPropertyDelegationMethodsReference.java deleted file mode 100644 index fbbd001435e..00000000000 --- a/idea/src/org/jetbrains/jet/plugin/references/JetPropertyDelegationMethodsReference.java +++ /dev/null @@ -1,100 +0,0 @@ -/* - * Copyright 2010-2013 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.jet.plugin.references; - -import com.google.common.collect.Lists; -import com.intellij.lang.ASTNode; -import com.intellij.openapi.project.Project; -import com.intellij.openapi.util.TextRange; -import com.intellij.psi.PsiReference; -import com.intellij.psi.ResolveResult; -import com.intellij.psi.util.PsiTreeUtil; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; -import org.jetbrains.jet.lang.descriptors.FunctionDescriptor; -import org.jetbrains.jet.lang.descriptors.PropertyAccessorDescriptor; -import org.jetbrains.jet.lang.descriptors.PropertyDescriptor; -import org.jetbrains.jet.lang.psi.JetProperty; -import org.jetbrains.jet.lang.psi.JetPropertyDelegate; -import org.jetbrains.jet.lang.resolve.BindingContext; -import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall; -import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache; - -import java.util.List; - -public class JetPropertyDelegationMethodsReference extends AbstractPolyVariantJetReference { - - public static PsiReference[] create(@NotNull JetPropertyDelegate delegate) { - return new PsiReference[] { new JetPropertyDelegationMethodsReference(delegate) }; - } - - public JetPropertyDelegationMethodsReference(@NotNull JetPropertyDelegate element) { - super(element); - } - - @NotNull - @Override - public ResolveResult[] multiResolve(boolean incompleteCode) { - JetProperty property = PsiTreeUtil.getParentOfType(element, JetProperty.class); - if (property == null) { - return ResolveResult.EMPTY_ARRAY; - } - - BindingContext context = AnalyzerFacadeWithCache.getContextForElement(element); - - DeclarationDescriptor descriptor = context.get(BindingContext.DECLARATION_TO_DESCRIPTOR, property); - if (!(descriptor instanceof PropertyDescriptor)) { - return ResolveResult.EMPTY_ARRAY; - } - - List results = Lists.newArrayList(); - - PropertyDescriptor propertyDescriptor = (PropertyDescriptor) descriptor; - addResultsForAccessor(property.getProject(), context, results, propertyDescriptor.getGetter()); - addResultsForAccessor(property.getProject(), context, results, propertyDescriptor.getSetter()); - - return results.toArray(new ResolveResult[results.size()]); - } - - private static void addResultsForAccessor( - Project project, - BindingContext context, - List results, - @Nullable PropertyAccessorDescriptor accessor - ) { - if (accessor == null) return; - - ResolvedCall resolvedCall = context.get(BindingContext.DELEGATED_PROPERTY_RESOLVED_CALL, accessor); - if (resolvedCall == null) return; - - JetReferenceUtil.findPsiElements(project, context, results, resolvedCall.getResultingDescriptor()); - } - - @Override - public TextRange getRangeInElement() { - ASTNode byKeywordNode = element.getByKeywordNode(); - int offset = byKeywordNode.getPsi().getStartOffsetInParent(); - return new TextRange(offset, offset + byKeywordNode.getTextLength()); - } - - @NotNull - @Override - public String getCanonicalText() { - return ""; - } -} diff --git a/idea/src/org/jetbrains/jet/plugin/references/JetPropertyDelegationMethodsReference.kt b/idea/src/org/jetbrains/jet/plugin/references/JetPropertyDelegationMethodsReference.kt new file mode 100644 index 00000000000..ddb076b80e7 --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/references/JetPropertyDelegationMethodsReference.kt @@ -0,0 +1,53 @@ +/* + * Copyright 2010-2014 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.jet.plugin.references + +import com.intellij.openapi.util.TextRange +import com.intellij.psi.util.PsiTreeUtil +import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor +import org.jetbrains.jet.lang.descriptors.PropertyDescriptor +import org.jetbrains.jet.lang.psi.JetProperty +import org.jetbrains.jet.lang.psi.JetPropertyDelegate +import org.jetbrains.jet.lang.resolve.BindingContext +import java.util.Collections + +public class JetPropertyDelegationMethodsReference(element: JetPropertyDelegate) : JetMultiReference(element) { + + override fun getRangeInElement(): TextRange { + val byKeywordNode = expression.getByKeywordNode() + val offset = byKeywordNode.getPsi()!!.getStartOffsetInParent() + return TextRange(offset, offset + byKeywordNode.getTextLength()) + } + + override fun getTargetDescriptors(context: BindingContext): Collection { + val property = PsiTreeUtil.getParentOfType(expression, javaClass()) + if (property == null) { + return Collections.emptyList() + } + val descriptor = context.get(BindingContext.DECLARATION_TO_DESCRIPTOR, property) + if (descriptor !is PropertyDescriptor) { + return Collections.emptyList() + } + return descriptor.getAccessors().map { + accessor -> + val candidateDescriptor = context.get(BindingContext.DELEGATED_PROPERTY_RESOLVED_CALL, accessor)?.getCandidateDescriptor() + //TODO: should not getOriginal here, because candidate descriptor should not have substituted type parameters + // remove after problem is solved + candidateDescriptor?.getOriginal() + }.filterNotNull() + } +} diff --git a/idea/src/org/jetbrains/jet/plugin/references/JetReference.kt b/idea/src/org/jetbrains/jet/plugin/references/JetReference.kt index 80c0940b96c..3ceda7f763b 100644 --- a/idea/src/org/jetbrains/jet/plugin/references/JetReference.kt +++ b/idea/src/org/jetbrains/jet/plugin/references/JetReference.kt @@ -36,13 +36,20 @@ import com.intellij.psi.search.GlobalSearchScope import org.jetbrains.jet.plugin.codeInsight.DescriptorToDeclarationUtil import com.intellij.util.containers.ContainerUtil import org.jetbrains.jet.asJava.* +import org.jetbrains.jet.lang.psi.JetElement +import org.jetbrains.jet.utils.keysToMap public trait JetReference : PsiPolyVariantReference { public fun resolveToDescriptors(): Collection + public fun resolveMap(): Map> } -abstract class AbstractJetReference(val expression: T) -: PsiPolyVariantReferenceBase(expression), JetReference { +abstract class AbstractJetReference(element: T) +: PsiPolyVariantReferenceBase(element), JetReference { + + val expression: T + get() = getElement()!! + override fun multiResolve(incompleteCode: Boolean): Array { return PsiElementResolveResult.createResults(resolveToPsiElements()) } @@ -61,13 +68,7 @@ abstract class AbstractJetReference(val expression: override fun bindToElement(element: PsiElement): PsiElement = throw IncorrectOperationException() - public override fun isReferenceTo(element: PsiElement?): Boolean { - val resolvedElement = resolve() - if (resolvedElement == null) { - return false - } - return element?.namedUnwrappedElement == resolvedElement.namedUnwrappedElement - } + protected fun areElementsEquivalent(el1: PsiElement, el2: PsiElement): Boolean = el1.namedUnwrappedElement == el2.namedUnwrappedElement [suppress("CAST_NEVER_SUCCEEDS")] override fun getVariants(): Array = PsiReference.EMPTY_ARRAY as Array @@ -81,27 +82,17 @@ abstract class AbstractJetReference(val expression: override fun resolveToDescriptors(): Collection { val context = AnalyzerFacadeWithCache.getContextForElement(expression) - val targetDescriptors = getTargetDescriptors(context) - return targetDescriptors ?: Collections.emptyList() + return getTargetDescriptors(context) } - private fun resolveToPsiElements(context: BindingContext, targetDescriptors: Collection?): Collection { - if (targetDescriptors != null) { - assert(!(targetDescriptors.isEmpty())) { "targetDescriptors is not null, but empty, for " + expression.getText() } - val result = HashSet() - val project = expression.getProject() - for (target in targetDescriptors) { - result.addAll(BindingContextUtils.descriptorToDeclarations(context, target)) - result.addAll(DescriptorToDeclarationUtil.findDeclarationsForDescriptorWithoutTrace(project, target)) + override fun resolveMap(): Map> { + val context = AnalyzerFacadeWithCache.getContextForElement(expression) + return getTargetDescriptors(context) keysToMap { resolveToPsiElements(context, it) } + } - if (target is PackageViewDescriptor) { - val psiFacade = JavaPsiFacade.getInstance(project) - val fqName = (target as PackageViewDescriptor).getFqName().asString() - ContainerUtil.addIfNotNull(result, psiFacade.findPackage(fqName)) - ContainerUtil.addIfNotNull(result, psiFacade.findClass(fqName, GlobalSearchScope.allScope(project))) - } - } - return result + private fun resolveToPsiElements(context: BindingContext, targetDescriptors: Collection): Collection { + if (targetDescriptors.isNotEmpty()) { + return targetDescriptors flatMap { target -> resolveToPsiElements(context, target) } } val labelTargets = getLabelTargets(context) @@ -112,19 +103,59 @@ abstract class AbstractJetReference(val expression: return Collections.emptySet() } - protected open fun getTargetDescriptors(context: BindingContext): Collection? { + private fun resolveToPsiElements(context: BindingContext, targetDescriptor: DeclarationDescriptor): Collection { + val result = HashSet() + val project = expression.getProject() + result.addAll(BindingContextUtils.descriptorToDeclarations(context, targetDescriptor)) + result.addAll(DescriptorToDeclarationUtil.findDeclarationsForDescriptorWithoutTrace(project, targetDescriptor)) + + if (targetDescriptor is PackageViewDescriptor) { + val psiFacade = JavaPsiFacade.getInstance(project) + val fqName = (targetDescriptor as PackageViewDescriptor).getFqName().asString() + ContainerUtil.addIfNotNull(result, psiFacade.findPackage(fqName)) + ContainerUtil.addIfNotNull(result, psiFacade.findClass(fqName, GlobalSearchScope.allScope(project))) + } + return result + } + + protected abstract fun getTargetDescriptors(context: BindingContext): Collection + + private fun getLabelTargets(context: BindingContext): Collection? { + val reference = expression + if (reference !is JetReferenceExpression) { + return null + } + val labelTarget = context.get(BindingContext.LABEL_TARGET, reference) + if (labelTarget != null) { + return listOf(labelTarget) + } + return context.get(BindingContext.AMBIGUOUS_LABEL_TARGET, reference) + } +} + +public abstract class JetSimpleReference(expression: T) : AbstractJetReference(expression) { + override fun getTargetDescriptors(context: BindingContext): Collection { val targetDescriptor = context.get(BindingContext.REFERENCE_TARGET, expression) if (targetDescriptor != null) { return listOf(targetDescriptor) } - return context.get(BindingContext.AMBIGUOUS_REFERENCE_TARGET, expression) + return context.get(BindingContext.AMBIGUOUS_REFERENCE_TARGET, expression).orEmpty() } - private fun getLabelTargets(context: BindingContext): Collection? { - val labelTarget = context.get(BindingContext.LABEL_TARGET, expression) - if (labelTarget != null) { - return listOf(labelTarget) + override fun isReferenceTo(element: PsiElement?): Boolean { + val resolvedElement = resolve() + if (resolvedElement == null || element == null) { + return false } - return context.get(BindingContext.AMBIGUOUS_LABEL_TARGET, expression) + return areElementsEquivalent(element, resolvedElement) + } +} + +public abstract class JetMultiReference(expression: T) : AbstractJetReference(expression) { + override fun isReferenceTo(element: PsiElement?): Boolean { + if (element == null) { + return false + } + return multiResolve(false).map { it.getElement() }.filterNotNull().any { areElementsEquivalent(it, element) } } } \ No newline at end of file diff --git a/idea/src/org/jetbrains/jet/plugin/references/JetReferenceContributor.java b/idea/src/org/jetbrains/jet/plugin/references/JetReferenceContributor.java index ca73f0e3560..3fe2176ee0f 100644 --- a/idea/src/org/jetbrains/jet/plugin/references/JetReferenceContributor.java +++ b/idea/src/org/jetbrains/jet/plugin/references/JetReferenceContributor.java @@ -31,7 +31,7 @@ public class JetReferenceContributor extends PsiReferenceContributor { @NotNull @Override public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext processingContext) { - return new PsiReference[] { new JetSimpleNameReference((JetSimpleNameExpression) element) }; + return toArray(new JetSimpleNameReference((JetSimpleNameExpression) element)); } }); @@ -40,7 +40,7 @@ public class JetReferenceContributor extends PsiReferenceContributor { @NotNull @Override public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext processingContext) { - return new PsiReference[] { new JetThisReference((JetThisReferenceExpression) element) }; + return toArray(new JetThisReference((JetThisReferenceExpression) element)); } }); @@ -49,7 +49,7 @@ public class JetReferenceContributor extends PsiReferenceContributor { @NotNull @Override public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext processingContext) { - return JetArrayAccessReference.create((JetArrayAccessExpression) element); + return toArray(new JetArrayAccessReference((JetArrayAccessExpression) element)); } }); @@ -58,7 +58,7 @@ public class JetReferenceContributor extends PsiReferenceContributor { @NotNull @Override public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext processingContext) { - return JetInvokeFunctionReference.create((JetCallExpression) element); + return toArray(new JetInvokeFunctionReference((JetCallExpression) element)); } }); @@ -67,7 +67,8 @@ public class JetReferenceContributor extends PsiReferenceContributor { @NotNull @Override public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext processingContext) { - return JetPropertyDelegationMethodsReference.create((JetPropertyDelegate) element); + return toArray( + new JetPropertyDelegationMethodsReference((JetPropertyDelegate) element)); } }); @@ -76,8 +77,13 @@ public class JetReferenceContributor extends PsiReferenceContributor { @NotNull @Override public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext processingContext) { - return JetForLoopInReference.create((JetForExpression) element); + return toArray(new JetForLoopInReference((JetForExpression) element)); } }); } + + @NotNull + private static JetReference[] toArray(@NotNull JetReference reference) { + return new JetReference[] {reference}; + } } diff --git a/idea/src/org/jetbrains/jet/plugin/references/JetReferenceUtil.java b/idea/src/org/jetbrains/jet/plugin/references/JetReferenceUtil.java deleted file mode 100644 index 0e2d8b9bcdd..00000000000 --- a/idea/src/org/jetbrains/jet/plugin/references/JetReferenceUtil.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright 2010-2013 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.jet.plugin.references; - -import com.intellij.openapi.project.Project; -import com.intellij.psi.PsiElement; -import com.intellij.psi.PsiElementResolveResult; -import com.intellij.psi.ResolveResult; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; -import org.jetbrains.jet.lang.resolve.BindingContext; -import org.jetbrains.jet.lang.resolve.BindingContextUtils; -import org.jetbrains.jet.plugin.codeInsight.DescriptorToDeclarationUtil; - -import java.util.List; - -public class JetReferenceUtil { - private JetReferenceUtil() {} - - public static void findPsiElements( - @NotNull Project project, - @NotNull BindingContext context, - @NotNull List results, - @Nullable DeclarationDescriptor resultingDescriptor - ) { - if (resultingDescriptor == null) return; - - DeclarationDescriptor original = resultingDescriptor.getOriginal(); - - for (PsiElement declaration : BindingContextUtils.descriptorToDeclarations(context, original)) { - results.add(new PsiElementResolveResult(declaration)); - } - - for (PsiElement declaration : DescriptorToDeclarationUtil.findDeclarationsForDescriptorWithoutTrace(project, original)) { - results.add(new PsiElementResolveResult(declaration)); - } - } -} diff --git a/idea/src/org/jetbrains/jet/plugin/references/JetSimpleNameReference.java b/idea/src/org/jetbrains/jet/plugin/references/JetSimpleNameReference.java index 258ad91089c..98112b99c96 100644 --- a/idea/src/org/jetbrains/jet/plugin/references/JetSimpleNameReference.java +++ b/idea/src/org/jetbrains/jet/plugin/references/JetSimpleNameReference.java @@ -26,7 +26,7 @@ import org.jetbrains.jet.lang.psi.JetPsiFactory; import org.jetbrains.jet.lang.psi.JetSimpleNameExpression; import org.jetbrains.jet.lexer.JetTokens; -public class JetSimpleNameReference extends AbstractJetReference { +public class JetSimpleNameReference extends JetSimpleReference { public JetSimpleNameReference(@NotNull JetSimpleNameExpression jetSimpleNameExpression) { super(jetSimpleNameExpression); diff --git a/idea/src/org/jetbrains/jet/plugin/references/JetThisReference.java b/idea/src/org/jetbrains/jet/plugin/references/JetThisReference.java index 676f2b46713..275735ccb1a 100644 --- a/idea/src/org/jetbrains/jet/plugin/references/JetThisReference.java +++ b/idea/src/org/jetbrains/jet/plugin/references/JetThisReference.java @@ -19,7 +19,7 @@ package org.jetbrains.jet.plugin.references; import com.intellij.openapi.util.TextRange; import org.jetbrains.jet.lang.psi.JetThisReferenceExpression; -public class JetThisReference extends AbstractJetReference { +public class JetThisReference extends JetSimpleReference { public JetThisReference(JetThisReferenceExpression expression) { super(expression); }