Rewrite to Kotlin: DescriptorToDeclarationUtil

This commit is contained in:
Nikolay Krasko
2014-07-23 14:16:55 +04:00
parent 9eb1a38a25
commit 72c7d22d5f
6 changed files with 61 additions and 78 deletions
@@ -1,71 +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.codeInsight;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
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.psi.JetFile;
import org.jetbrains.jet.lang.resolve.DescriptorToSourceUtils;
import org.jetbrains.jet.plugin.libraries.DecompiledNavigationUtils;
import org.jetbrains.jet.plugin.references.BuiltInsReferenceResolver;
import java.util.Collection;
import java.util.Collections;
public final class DescriptorToDeclarationUtil {
private DescriptorToDeclarationUtil() {
}
@Nullable
public static PsiElement getDeclaration(@NotNull JetFile file, @NotNull DeclarationDescriptor descriptor) {
return getDeclaration(file.getProject(), descriptor);
}
@Nullable
public static PsiElement getDeclaration(@NotNull Project project, @NotNull DeclarationDescriptor descriptor) {
Collection<PsiElement> elements = DescriptorToSourceUtils.descriptorToDeclarations(descriptor);
if (elements.isEmpty()) {
elements = findDecompiledAndBuiltInDeclarations(project, descriptor);
}
if (!elements.isEmpty()) {
return elements.iterator().next();
}
return null;
}
@NotNull
public static Collection<PsiElement> findDecompiledAndBuiltInDeclarations(
@NotNull Project project,
@NotNull DeclarationDescriptor descriptor
) {
BuiltInsReferenceResolver libraryReferenceResolver = project.getComponent(BuiltInsReferenceResolver.class);
Collection<PsiElement> elements = libraryReferenceResolver.resolveBuiltInSymbol(descriptor);
if (!elements.isEmpty()) {
return elements;
}
JetDeclaration decompiledDeclaration = DecompiledNavigationUtils.findDeclarationForReference(project, descriptor);
if (decompiledDeclaration != null) {
return Collections.<PsiElement>singleton(decompiledDeclaration);
}
return Collections.emptySet();
}
}
@@ -0,0 +1,54 @@
/*
* 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.codeInsight
import org.jetbrains.jet.lang.psi.JetFile
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor
import com.intellij.psi.PsiElement
import com.intellij.openapi.project.Project
import org.jetbrains.jet.lang.resolve.DescriptorToSourceUtils
import org.jetbrains.jet.plugin.references.BuiltInsReferenceResolver
import org.jetbrains.jet.plugin.libraries.DecompiledNavigationUtils
object DescriptorToDeclarationUtil {
public fun getDeclaration(file: JetFile, descriptor: DeclarationDescriptor): PsiElement? {
return getDeclaration(file.getProject(), descriptor)
}
public fun getDeclaration(project: Project, descriptor: DeclarationDescriptor): PsiElement? {
var elements: Collection<PsiElement> = DescriptorToSourceUtils.descriptorToDeclarations(descriptor)
if (elements.isEmpty()) {
elements = findDecompiledAndBuiltInDeclarations(project, descriptor)
}
return elements.firstOrNull()
}
public fun findDecompiledAndBuiltInDeclarations(project: Project, descriptor: DeclarationDescriptor): Collection<PsiElement> {
val libraryReferenceResolver = project.getComponent(javaClass<BuiltInsReferenceResolver>())
val elements = libraryReferenceResolver!!.resolveBuiltInSymbol(descriptor)
if (!elements.isEmpty()) {
return elements
}
val decompiledDeclaration = DecompiledNavigationUtils.findDeclarationForReference(project, descriptor)
if (decompiledDeclaration != null) {
return setOf(decompiledDeclaration)
}
return setOf()
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
* 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.
@@ -80,7 +80,7 @@ public class JetAddFunctionToClassifierAction implements QuestionAction {
PsiDocumentManager.getInstance(project).commitAllDocuments();
final JetClass classifierDeclaration = (JetClass) DescriptorToDeclarationUtil.getDeclaration(project, typeDescriptor);
final JetClass classifierDeclaration = (JetClass) DescriptorToDeclarationUtil.INSTANCE$.getDeclaration(project, typeDescriptor);
CommandProcessor.getInstance().executeCommand(project, new Runnable() {
@Override
public void run() {
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
* 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.
@@ -63,7 +63,7 @@ public abstract class OverrideImplementMethodsHandler implements LanguageCodeIns
) {
List<DescriptorClassMember> members = new ArrayList<DescriptorClassMember>();
for (CallableMemberDescriptor memberDescriptor : missingImplementations) {
PsiElement declaration = DescriptorToDeclarationUtil.getDeclaration(file, memberDescriptor);
PsiElement declaration = DescriptorToDeclarationUtil.INSTANCE$.getDeclaration(file, memberDescriptor);
if (declaration == null) {
LOG.error("Can not find declaration for descriptor " + memberDescriptor);
}
@@ -137,7 +137,7 @@ public class JetRefactoringUtil {
Project project = declaration.getProject();
Map<PsiElement, CallableDescriptor> overriddenElementsToDescriptor = new HashMap<PsiElement, CallableDescriptor>();
for (CallableDescriptor overriddenDescriptor : OverrideResolver.getAllOverriddenDescriptors(declarationDescriptor)) {
PsiElement overriddenDeclaration = DescriptorToDeclarationUtil.getDeclaration(project, overriddenDescriptor);
PsiElement overriddenDeclaration = DescriptorToDeclarationUtil.INSTANCE$.getDeclaration(project, overriddenDescriptor);
if (PsiTreeUtil.instanceOf(overriddenDeclaration, JetNamedFunction.class, JetProperty.class, PsiMethod.class)) {
overriddenElementsToDescriptor.put(overriddenDeclaration, overriddenDescriptor);
}
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
* 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.
@@ -105,7 +105,7 @@ public final class JetChangeSignatureData implements JetMethodDescriptor {
@NotNull
private Collection<PsiElement> computeHierarchyFrom(@NotNull FunctionDescriptor baseDescriptor) {
PsiElement declaration = DescriptorToDeclarationUtil.getDeclaration(baseDeclaration.getProject(), baseDescriptor);
PsiElement declaration = DescriptorToDeclarationUtil.INSTANCE$.getDeclaration(baseDeclaration.getProject(), baseDescriptor);
Set<PsiElement> result = Sets.newHashSet();
result.add(declaration);
if (!(declaration instanceof JetNamedFunction)) {