181: FakeFileForLightClass made return "virtualFile" from "ktFile"

This commit is contained in:
Nicolay Mitropolsky
2018-03-01 21:51:07 +03:00
committed by Nikolay Krasko
parent 44a37f5dad
commit 94d18581e3
2 changed files with 46 additions and 1 deletions
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.asJava.elements
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.pom.java.LanguageLevel
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiElementVisitor
@@ -33,6 +34,10 @@ open class FakeFileForLightClass(
private val stub: () -> PsiClassHolderFileStub<*>,
private val packageFqName: FqName = ktFile.packageFqName
) : ClsFileImpl(ktFile.viewProvider) {
override fun getVirtualFile(): VirtualFile =
ktFile.virtualFile ?: ktFile.originalFile.virtualFile ?: super.getVirtualFile()
override fun getPackageName() = packageFqName.asString()
override fun getStub() = stub()
@@ -17,9 +17,13 @@
package org.jetbrains.kotlin.asJava
import com.intellij.injected.editor.EditorWindow
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.psi.search.GlobalSearchScope
import com.intellij.testFramework.LightProjectDescriptor
import org.intellij.lang.annotations.Language
import junit.framework.TestCase
import org.jetbrains.kotlin.asJava.elements.KtLightElement
import org.jetbrains.kotlin.idea.core.copied
import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase
import org.jetbrains.kotlin.idea.test.KotlinLightProjectDescriptor
import org.jetbrains.kotlin.idea.test.PluginTestCaseBase
@@ -92,4 +96,40 @@ class KtFileLightClassTest : KotlinLightCodeInsightFixtureTestCase() {
assertEquals("Injected class should be `A`", "A", (injectedFile as KtFile).classes.single().name)
}
fun testSameVirtualFileForLightElement() {
val psiFile = myFixture.addFileToProject(
"pkg/AnnotatedClass.kt", """
package pkg
class AnnotatedClass {
@Deprecated("a")
fun bar(param: String) = null
}
""".trimIndent()
)
fun lightElement(file: PsiFile): PsiElement = (file as KtFile).classes.single()
.methods.first { it.name == "bar" }
.annotations.first { it.qualifiedName == "kotlin.Deprecated" }.also {
// Otherwise following asserts have no sense
TestCase.assertTrue("psi element should be light ", it is KtLightElement<*, *>)
}
val copied = psiFile.copied()
TestCase.assertNull("virtual file for copy should be null", copied.virtualFile)
TestCase.assertNotNull("psi element in copy", lightElement(copied))
TestCase.assertSame("copy.originalFile should be psiFile", copied.originalFile, psiFile)
// virtual files should be the same for light and non-light element,
// otherwise we will not be able to find proper module by file from light element
TestCase.assertSame(
"virtualFiles of element and file itself should be the same",
lightElement(copied).containingFile.originalFile.virtualFile,
copied.originalFile.virtualFile
)
}
}