implement KtFile.getClasses() in plugin (required to have coverage work, among other things)
This commit is contained in:
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.psi;
|
|||||||
import com.intellij.extapi.psi.PsiFileBase;
|
import com.intellij.extapi.psi.PsiFileBase;
|
||||||
import com.intellij.lang.ASTNode;
|
import com.intellij.lang.ASTNode;
|
||||||
import com.intellij.lang.FileASTNode;
|
import com.intellij.lang.FileASTNode;
|
||||||
|
import com.intellij.openapi.components.ServiceManager;
|
||||||
import com.intellij.openapi.fileTypes.FileType;
|
import com.intellij.openapi.fileTypes.FileType;
|
||||||
import com.intellij.psi.*;
|
import com.intellij.psi.*;
|
||||||
import com.intellij.psi.stubs.StubElement;
|
import com.intellij.psi.stubs.StubElement;
|
||||||
@@ -163,6 +164,11 @@ public class KtFile extends PsiFileBase implements KtDeclarationContainer, KtAnn
|
|||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public PsiClass[] getClasses() {
|
public PsiClass[] getClasses() {
|
||||||
|
KtFileClassProvider fileClassProvider = ServiceManager.getService(getProject(), KtFileClassProvider.class);
|
||||||
|
// TODO We don't currently support finding light classes for scripts
|
||||||
|
if (fileClassProvider != null && !isScript()) {
|
||||||
|
return fileClassProvider.getFileClasses(this);
|
||||||
|
}
|
||||||
return PsiClass.EMPTY_ARRAY;
|
return PsiClass.EMPTY_ARRAY;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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.psi
|
||||||
|
|
||||||
|
import com.intellij.psi.PsiClass
|
||||||
|
|
||||||
|
interface KtFileClassProvider {
|
||||||
|
fun getFileClasses(file: KtFile): Array<PsiClass>
|
||||||
|
}
|
||||||
+19
-1
@@ -309,7 +309,7 @@ class IDELightClassGenerationSupport(private val project: Project) : LightClassG
|
|||||||
return getClassRelativeName(parent).child(name)
|
return getClassRelativeName(parent).child(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun createLightClassForDecompiledKotlinFile(file: KtFile): KtLightClassForDecompiledDeclaration? {
|
fun createLightClassForDecompiledKotlinFile(file: KtFile): KtLightClassForDecompiledDeclaration? {
|
||||||
val virtualFile = file.virtualFile ?: return null
|
val virtualFile = file.virtualFile ?: return null
|
||||||
|
|
||||||
val classOrObject = file.declarations.filterIsInstance<KtClassOrObject>().singleOrNull()
|
val classOrObject = file.declarations.filterIsInstance<KtClassOrObject>().singleOrNull()
|
||||||
@@ -351,3 +351,21 @@ class IDELightClassGenerationSupport(private val project: Project) : LightClassG
|
|||||||
private val LOG = Logger.getInstance(IDELightClassGenerationSupport::class.java)
|
private val LOG = Logger.getInstance(IDELightClassGenerationSupport::class.java)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class KtFileClassProviderImpl(val lightClassGenerationSupport: LightClassGenerationSupport) : KtFileClassProvider {
|
||||||
|
override fun getFileClasses(file: KtFile): Array<PsiClass> {
|
||||||
|
if (file.isCompiled) {
|
||||||
|
return arrayOf()
|
||||||
|
}
|
||||||
|
|
||||||
|
val result = arrayListOf<PsiClass>()
|
||||||
|
file.declarations.filterIsInstance<KtClassOrObject>().map { lightClassGenerationSupport.getPsiClass(it) }.filterNotNullTo(result)
|
||||||
|
|
||||||
|
val moduleInfo = file.getModuleInfo()
|
||||||
|
val fileClassFqName = file.javaFileFacadeFqName
|
||||||
|
lightClassGenerationSupport.getFacadeClasses(fileClassFqName, moduleInfo.contentScope()).filterTo(result) {
|
||||||
|
it is KtLightClassForFacade && file in it.files
|
||||||
|
}
|
||||||
|
return result.toTypedArray()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -224,6 +224,9 @@
|
|||||||
<projectService serviceInterface="org.jetbrains.kotlin.asJava.LightClassGenerationSupport"
|
<projectService serviceInterface="org.jetbrains.kotlin.asJava.LightClassGenerationSupport"
|
||||||
serviceImplementation="org.jetbrains.kotlin.idea.caches.resolve.IDELightClassGenerationSupport"/>
|
serviceImplementation="org.jetbrains.kotlin.idea.caches.resolve.IDELightClassGenerationSupport"/>
|
||||||
|
|
||||||
|
<projectService serviceInterface="org.jetbrains.kotlin.psi.KtFileClassProvider"
|
||||||
|
serviceImplementation="org.jetbrains.kotlin.idea.caches.resolve.KtFileClassProviderImpl"/>
|
||||||
|
|
||||||
<projectService serviceInterface="org.jetbrains.kotlin.resolve.CodeAnalyzerInitializer"
|
<projectService serviceInterface="org.jetbrains.kotlin.resolve.CodeAnalyzerInitializer"
|
||||||
serviceImplementation="org.jetbrains.kotlin.resolve.DummyCodeAnalyzerInitializer"/>
|
serviceImplementation="org.jetbrains.kotlin.resolve.DummyCodeAnalyzerInitializer"/>
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,41 @@
|
|||||||
|
/*
|
||||||
|
* 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.asJava
|
||||||
|
|
||||||
|
import com.intellij.testFramework.LightProjectDescriptor
|
||||||
|
import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase
|
||||||
|
import org.jetbrains.kotlin.idea.test.KotlinLightProjectDescriptor
|
||||||
|
import org.jetbrains.kotlin.psi.KtFile
|
||||||
|
|
||||||
|
class KtFileLightClassTest : KotlinLightCodeInsightFixtureTestCase() {
|
||||||
|
override fun getProjectDescriptor(): LightProjectDescriptor = KotlinLightProjectDescriptor.INSTANCE
|
||||||
|
|
||||||
|
fun testSimple() {
|
||||||
|
val file = myFixture.configureByText("A.kt", "class C {}\nobject O {}") as KtFile
|
||||||
|
val classes = file.classes
|
||||||
|
assertEquals(2, classes.size)
|
||||||
|
assertEquals("C", classes[0].qualifiedName)
|
||||||
|
assertEquals("O", classes[1].qualifiedName)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun testFileClass() {
|
||||||
|
val file = myFixture.configureByText("A.kt", "fun f() {}") as KtFile
|
||||||
|
val classes = file.classes
|
||||||
|
assertEquals(1, classes.size)
|
||||||
|
assertEquals("AKt", classes[0].qualifiedName)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user