Add the ability to get containing source file from SourceElement

This commit is contained in:
Zalim Bashorov
2015-09-30 16:28:56 +03:00
parent c7e5eb9ab8
commit 94b110936e
7 changed files with 64 additions and 1 deletions
@@ -17,8 +17,17 @@
package org.jetbrains.kotlin.resolve.source
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import org.jetbrains.kotlin.descriptors.SourceElement
import org.jetbrains.kotlin.descriptors.SourceFile
public interface PsiSourceElement : SourceElement {
public val psi: PsiElement?
override fun getContainingFile(): SourceFile = psi?.containingFile?.let { PsiSourceFile(it) } ?: SourceFile.NO_SOURCE_FILE
}
public class PsiSourceFile(private val psiFile: PsiFile): SourceFile {
override fun equals(other: Any?): Boolean = other is PsiSourceFile && psiFile == other.psiFile
override fun hashCode(): Int = psiFile.hashCode()
}
@@ -17,7 +17,9 @@
package org.jetbrains.kotlin.load.kotlin
import org.jetbrains.kotlin.descriptors.SourceElement
import org.jetbrains.kotlin.descriptors.SourceFile
public class KotlinJvmBinarySourceElement(public val binaryClass: KotlinJvmBinaryClass) : SourceElement {
override fun toString() = javaClass.name + ": " + binaryClass.toString()
override fun getContainingFile(): SourceFile = SourceFile.NO_SOURCE_FILE
}
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.load.java.components
import org.jetbrains.kotlin.descriptors.SourceFile
import org.jetbrains.kotlin.load.java.sources.JavaSourceElement
import org.jetbrains.kotlin.load.java.sources.JavaSourceElementFactory
import org.jetbrains.kotlin.load.java.structure.JavaElement
@@ -24,6 +25,7 @@ import org.jetbrains.kotlin.load.java.structure.reflect.ReflectJavaElement
public object RuntimeSourceElementFactory : JavaSourceElementFactory {
private class RuntimeSourceElement(override val javaElement: ReflectJavaElement) : JavaSourceElement {
override fun toString() = javaClass.name + ": " + javaElement.toString()
override fun getContainingFile(): SourceFile = SourceFile.NO_SOURCE_FILE
}
override fun source(javaElement: JavaElement) = RuntimeSourceElement(javaElement as ReflectJavaElement)
@@ -17,5 +17,8 @@
package org.jetbrains.kotlin.load.kotlin.reflect
import org.jetbrains.kotlin.descriptors.SourceElement
import org.jetbrains.kotlin.descriptors.SourceFile
public class ReflectAnnotationSource(public val annotation: Annotation) : SourceElement
public class ReflectAnnotationSource(public val annotation: Annotation) : SourceElement {
override fun getContainingFile(): SourceFile = SourceFile.NO_SOURCE_FILE
}
@@ -16,11 +16,22 @@
package org.jetbrains.kotlin.descriptors;
import org.jetbrains.annotations.NotNull;
public interface SourceElement {
SourceElement NO_SOURCE = new SourceElement() {
@Override
public String toString() {
return "NO_SOURCE";
}
@NotNull
@Override
public SourceFile getContainingFile() {
return SourceFile.NO_SOURCE_FILE;
}
};
@NotNull
SourceFile getContainingFile();
}
@@ -0,0 +1,23 @@
/*
* 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.descriptors
public interface SourceFile {
companion object {
val NO_SOURCE_FILE = object : SourceFile {}
}
}
@@ -589,6 +589,19 @@ public class DescriptorUtils {
return getJvmNameAnnotation(annotated.getAnnotations());
}
@NotNull
public static SourceFile getContainingSourceFile(@NotNull DeclarationDescriptor descriptor) {
if (descriptor instanceof PropertySetterDescriptor) {
descriptor = ((PropertySetterDescriptor) descriptor).getCorrespondingProperty();
}
if (descriptor instanceof DeclarationDescriptorWithSource) {
return ((DeclarationDescriptorWithSource) descriptor).getSource().getContainingFile();
}
return SourceFile.NO_SOURCE_FILE;
}
private static void getSubPackagesFqNames(PackageViewDescriptor packageView, Set<FqName> result) {
FqName fqName = packageView.getFqName();
if (!fqName.isRoot()) {