diff --git a/idea/src/org/jetbrains/jet/plugin/references/AbstractPolyVariantJetReference.java b/idea/src/org/jetbrains/jet/plugin/references/AbstractPolyVariantJetReference.java new file mode 100644 index 00000000000..a59843b297c --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/references/AbstractPolyVariantJetReference.java @@ -0,0 +1,78 @@ +/* + * 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/JetPropertyDelegationMethodsReference.java b/idea/src/org/jetbrains/jet/plugin/references/JetPropertyDelegationMethodsReference.java index b7565fa8dab..fee39760992 100644 --- a/idea/src/org/jetbrains/jet/plugin/references/JetPropertyDelegationMethodsReference.java +++ b/idea/src/org/jetbrains/jet/plugin/references/JetPropertyDelegationMethodsReference.java @@ -20,37 +20,31 @@ import com.beust.jcommander.internal.Lists; import com.intellij.lang.ASTNode; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.TextRange; -import com.intellij.psi.*; +import com.intellij.psi.PsiReference; +import com.intellij.psi.ResolveResult; import com.intellij.psi.util.PsiTreeUtil; -import com.intellij.util.IncorrectOperationException; 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.JetDeclaration; 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.BindingContextUtils; import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall; -import org.jetbrains.jet.plugin.libraries.DecompiledNavigationUtils; import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache; -import java.util.Collection; import java.util.List; -public class JetPropertyDelegationMethodsReference implements PsiPolyVariantReference { +public class JetPropertyDelegationMethodsReference extends AbstractPolyVariantJetReference { public static PsiReference[] create(@NotNull JetPropertyDelegate delegate) { return new PsiReference[] { new JetPropertyDelegationMethodsReference(delegate) }; } - private final JetPropertyDelegate element; - - private JetPropertyDelegationMethodsReference(@NotNull JetPropertyDelegate element) { - this.element = element; + public JetPropertyDelegationMethodsReference(@NotNull JetPropertyDelegate element) { + super(element); } @NotNull @@ -81,37 +75,14 @@ public class JetPropertyDelegationMethodsReference implements PsiPolyVariantRefe Project project, BindingContext context, List results, - PropertyAccessorDescriptor accessor + @Nullable PropertyAccessorDescriptor accessor ) { if (accessor == null) return; ResolvedCall resolvedCall = context.get(BindingContext.DELEGATED_PROPERTY_RESOLVED_CALL, accessor); if (resolvedCall == null) return; - FunctionDescriptor resultingDescriptor = resolvedCall.getResultingDescriptor().getOriginal(); - - List declarations = BindingContextUtils.descriptorToDeclarations(context, resultingDescriptor); - for (PsiElement declaration : declarations) { - results.add(new PsiElementResolveResult(declaration, true)); - } - - JetDeclaration declarationInDecompiledFile = - DecompiledNavigationUtils.findDeclarationForReference(project, resultingDescriptor); - if (declarationInDecompiledFile != null) { - results.add(new PsiElementResolveResult(declarationInDecompiledFile)); - } - - Collection builtInSymbols = - project.getComponent(BuiltInsReferenceResolver.class).resolveBuiltInSymbol(resultingDescriptor); - - for (PsiElement symbol : builtInSymbols) { - results.add(new PsiElementResolveResult(symbol)); - } - } - - @Override - public PsiElement getElement() { - return element; + JetReferenceUtil.findPsiElements(project, context, results, resolvedCall.getResultingDescriptor()); } @Override @@ -121,49 +92,9 @@ public class JetPropertyDelegationMethodsReference implements PsiPolyVariantRefe return new TextRange(offset, offset + byKeywordNode.getTextLength()); } - @Nullable - @Override - public PsiElement resolve() { - ResolveResult[] results = multiResolve(false); - if (results.length == 1) return results[0].getElement(); - return null; - } - @NotNull @Override public String getCanonicalText() { return ""; } - - @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/JetReferenceUtil.java b/idea/src/org/jetbrains/jet/plugin/references/JetReferenceUtil.java new file mode 100644 index 00000000000..ce67e382df2 --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/references/JetReferenceUtil.java @@ -0,0 +1,65 @@ +/* + * 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.psi.JetDeclaration; +import org.jetbrains.jet.lang.resolve.BindingContext; +import org.jetbrains.jet.lang.resolve.BindingContextUtils; +import org.jetbrains.jet.plugin.libraries.DecompiledNavigationUtils; + +import java.util.Collection; +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(); + + List declarations = BindingContextUtils.descriptorToDeclarations(context, original); + for (PsiElement declaration : declarations) { + results.add(new PsiElementResolveResult(declaration, true)); + } + + JetDeclaration declarationInDecompiledFile = + DecompiledNavigationUtils.findDeclarationForReference(project, original); + if (declarationInDecompiledFile != null) { + results.add(new PsiElementResolveResult(declarationInDecompiledFile)); + } + + Collection builtInSymbols = + project.getComponent(BuiltInsReferenceResolver.class).resolveBuiltInSymbol(original); + + for (PsiElement symbol : builtInSymbols) { + results.add(new PsiElementResolveResult(symbol)); + } + } +}