Platform header annotator: implementation + simple multi-platform highlighting tests #KT-14905 Fixed
This commit is contained in:
+5
-4
@@ -43,7 +43,7 @@ import org.jetbrains.kotlin.types.typeUtil.asTypeProjection
|
|||||||
import org.jetbrains.kotlin.utils.SmartList
|
import org.jetbrains.kotlin.utils.SmartList
|
||||||
import org.jetbrains.kotlin.utils.keysToMap
|
import org.jetbrains.kotlin.utils.keysToMap
|
||||||
|
|
||||||
class HeaderImplDeclarationChecker : DeclarationChecker {
|
class HeaderImplDeclarationChecker(val moduleToCheck: ModuleDescriptor? = null) : DeclarationChecker {
|
||||||
override fun check(
|
override fun check(
|
||||||
declaration: KtDeclaration,
|
declaration: KtDeclaration,
|
||||||
descriptor: DeclarationDescriptor,
|
descriptor: DeclarationDescriptor,
|
||||||
@@ -64,7 +64,7 @@ class HeaderImplDeclarationChecker : DeclarationChecker {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun checkHeaderDeclarationHasImplementation(
|
fun checkHeaderDeclarationHasImplementation(
|
||||||
reportOn: KtDeclaration, descriptor: MemberDescriptor, diagnosticHolder: DiagnosticSink, checkImpl: Boolean
|
reportOn: KtDeclaration, descriptor: MemberDescriptor, diagnosticHolder: DiagnosticSink, checkImpl: Boolean
|
||||||
) {
|
) {
|
||||||
val compatibility = when (descriptor) {
|
val compatibility = when (descriptor) {
|
||||||
@@ -132,7 +132,7 @@ class HeaderImplDeclarationChecker : DeclarationChecker {
|
|||||||
|
|
||||||
private fun CallableMemberDescriptor.findNamesakesFromTheSameModule(): Collection<CallableMemberDescriptor> {
|
private fun CallableMemberDescriptor.findNamesakesFromTheSameModule(): Collection<CallableMemberDescriptor> {
|
||||||
val packageFqName = (containingDeclaration as? PackageFragmentDescriptor)?.fqName ?: return emptyList()
|
val packageFqName = (containingDeclaration as? PackageFragmentDescriptor)?.fqName ?: return emptyList()
|
||||||
val myModule = this.module
|
val myModule = moduleToCheck ?: module
|
||||||
val scope = myModule.getPackage(packageFqName).memberScope
|
val scope = myModule.getPackage(packageFqName).memberScope
|
||||||
|
|
||||||
return when (this) {
|
return when (this) {
|
||||||
@@ -145,8 +145,9 @@ class HeaderImplDeclarationChecker : DeclarationChecker {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun ClassifierDescriptor.findClassifiersFromTheSameModule(): Collection<ClassifierDescriptor> {
|
private fun ClassifierDescriptor.findClassifiersFromTheSameModule(): Collection<ClassifierDescriptor> {
|
||||||
|
val myModule = moduleToCheck ?: module
|
||||||
// TODO: support nested classes
|
// TODO: support nested classes
|
||||||
return module.getPackage(fqNameSafe.parent()).memberScope
|
return myModule.getPackage(fqNameSafe.parent()).memberScope
|
||||||
.getDescriptorsFiltered(DescriptorKindFilter.CLASSIFIERS) { it == name }
|
.getDescriptorsFiltered(DescriptorKindFilter.CLASSIFIERS) { it == name }
|
||||||
.filterIsInstance<ClassifierDescriptor>()
|
.filterIsInstance<ClassifierDescriptor>()
|
||||||
}
|
}
|
||||||
|
|||||||
+72
@@ -0,0 +1,72 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2016 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.idea.highlighter
|
||||||
|
|
||||||
|
import com.intellij.lang.annotation.AnnotationHolder
|
||||||
|
import com.intellij.lang.annotation.Annotator
|
||||||
|
import com.intellij.psi.PsiElement
|
||||||
|
import org.jetbrains.kotlin.descriptors.MemberDescriptor
|
||||||
|
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
|
||||||
|
import org.jetbrains.kotlin.descriptors.PlatformKind
|
||||||
|
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||||
|
import org.jetbrains.kotlin.diagnostics.DiagnosticSink
|
||||||
|
import org.jetbrains.kotlin.idea.caches.resolve.findModuleDescriptor
|
||||||
|
import org.jetbrains.kotlin.idea.core.toDescriptor
|
||||||
|
import org.jetbrains.kotlin.idea.project.TargetPlatformDetector
|
||||||
|
import org.jetbrains.kotlin.lexer.KtTokens
|
||||||
|
import org.jetbrains.kotlin.psi.KtDeclaration
|
||||||
|
import org.jetbrains.kotlin.resolve.checkers.HeaderImplDeclarationChecker
|
||||||
|
import org.jetbrains.kotlin.resolve.diagnostics.Diagnostics
|
||||||
|
import org.jetbrains.kotlin.resolve.diagnostics.SimpleDiagnostics
|
||||||
|
|
||||||
|
class PlatformHeaderAnnotator : Annotator {
|
||||||
|
|
||||||
|
override fun annotate(element: PsiElement, holder: AnnotationHolder) {
|
||||||
|
val declaration = element as? KtDeclaration ?: return
|
||||||
|
if (!declaration.hasModifier(KtTokens.HEADER_KEYWORD)) return
|
||||||
|
|
||||||
|
val platform = TargetPlatformDetector.getPlatform(declaration.containingKtFile)
|
||||||
|
if (platform.kind != PlatformKind.DEFAULT) return
|
||||||
|
|
||||||
|
val defaultModuleDescriptor = declaration.findModuleDescriptor()
|
||||||
|
val dependentDescriptors = defaultModuleDescriptor.allImplementingModules
|
||||||
|
if (dependentDescriptors.isEmpty()) return
|
||||||
|
|
||||||
|
val diagnostics = validate(declaration, dependentDescriptors)
|
||||||
|
KotlinPsiChecker().annotateElement(declaration, holder, diagnostics)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun validate(declaration: KtDeclaration, modulesToCheck: Collection<ModuleDescriptor>): Diagnostics {
|
||||||
|
val descriptor = declaration.toDescriptor() as? MemberDescriptor ?: return Diagnostics.EMPTY
|
||||||
|
if (!descriptor.isHeader) return Diagnostics.EMPTY
|
||||||
|
|
||||||
|
val checkers = modulesToCheck.map(::HeaderImplDeclarationChecker)
|
||||||
|
val diagnosticList = mutableListOf<Diagnostic>()
|
||||||
|
val diagnosticSink = object : DiagnosticSink {
|
||||||
|
override fun report(diagnostic: Diagnostic) {
|
||||||
|
diagnosticList += diagnostic
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun wantsDiagnostics() = true
|
||||||
|
}
|
||||||
|
for (checker in checkers) {
|
||||||
|
checker.checkHeaderDeclarationHasImplementation(declaration, descriptor, diagnosticSink, checkImpl = false)
|
||||||
|
}
|
||||||
|
|
||||||
|
return if (diagnosticList.isNotEmpty()) SimpleDiagnostics(diagnosticList) else Diagnostics.EMPTY
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -505,6 +505,7 @@
|
|||||||
|
|
||||||
<annotator language="kotlin" implementationClass="org.jetbrains.kotlin.idea.highlighter.DebugInfoAnnotator"/>
|
<annotator language="kotlin" implementationClass="org.jetbrains.kotlin.idea.highlighter.DebugInfoAnnotator"/>
|
||||||
<annotator language="kotlin" implementationClass="org.jetbrains.kotlin.idea.highlighter.DuplicateJvmSignatureAnnotator"/>
|
<annotator language="kotlin" implementationClass="org.jetbrains.kotlin.idea.highlighter.DuplicateJvmSignatureAnnotator"/>
|
||||||
|
<annotator language="kotlin" implementationClass="org.jetbrains.kotlin.idea.highlighter.PlatformHeaderAnnotator"/>
|
||||||
<problemHighlightFilter implementation="org.jetbrains.kotlin.idea.highlighter.KotlinProblemHighlightFilter"/>
|
<problemHighlightFilter implementation="org.jetbrains.kotlin.idea.highlighter.KotlinProblemHighlightFilter"/>
|
||||||
|
|
||||||
<extendWordSelectionHandler implementation="org.jetbrains.kotlin.idea.editor.wordSelection.KotlinStatementGroupSelectioner"/>
|
<extendWordSelectionHandler implementation="org.jetbrains.kotlin.idea.editor.wordSelection.KotlinStatementGroupSelectioner"/>
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
header class <error descr="[HEADER_WITHOUT_IMPLEMENTATION] No implementation is found for header declaration 'My'">My</error> {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
<error descr="[IMPLEMENTATION_WITHOUT_HEADER] Modifier 'impl' is only applicable to members that are initially declared in platform-independent code">impl</error> class Your {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
header class <error descr="[HEADER_WITHOUT_IMPLEMENTATION] No implementation is found for header declaration 'His'">His</error> {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
header class Their {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
impl class Their {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
header class My {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
impl class My {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
header class <error descr="[HEADER_WITHOUT_IMPLEMENTATION] No implementation is found for header declaration 'My'"><error descr="[HEADER_WITHOUT_IMPLEMENTATION] No implementation is found for header declaration 'My'">My</error></error> {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
header class <error>My</error> {
|
||||||
|
|
||||||
|
fun foo(): Int
|
||||||
|
|
||||||
|
fun bar(arg: Int): Boolean
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
header class <error>Your</error> {
|
||||||
|
|
||||||
|
fun foo(): Int
|
||||||
|
|
||||||
|
fun bar(arg: Int): Boolean
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
header class His {
|
||||||
|
|
||||||
|
fun foo(): Int
|
||||||
|
|
||||||
|
fun bar(arg: Int): Boolean
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
<error descr="[IMPLEMENTATION_WITHOUT_HEADER] Modifier 'impl' is only applicable to members that are initially declared in platform-independent code">impl</error> class My {
|
||||||
|
|
||||||
|
<error descr="[IMPLEMENTATION_WITHOUT_HEADER] Modifier 'impl' is only applicable to members that are initially declared in platform-independent code">impl</error> fun foo() = 42
|
||||||
|
}
|
||||||
|
|
||||||
|
<error descr="[IMPLEMENTATION_WITHOUT_HEADER] Modifier 'impl' is only applicable to members that are initially declared in platform-independent code">impl</error> class Your {
|
||||||
|
|
||||||
|
<error descr="[IMPLEMENTATION_WITHOUT_HEADER] Modifier 'impl' is only applicable to members that are initially declared in platform-independent code">impl</error> fun foo() = 13
|
||||||
|
|
||||||
|
<error descr="[IMPLEMENTATION_WITHOUT_HEADER] Modifier 'impl' is only applicable to members that are initially declared in platform-independent code">impl</error> fun bar(arg: Int) = arg
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
impl class His {
|
||||||
|
|
||||||
|
impl fun foo() = 7
|
||||||
|
|
||||||
|
impl fun bar(arg: Int) = arg == foo()
|
||||||
|
|
||||||
|
}
|
||||||
+39
-2
@@ -21,14 +21,19 @@ import com.intellij.openapi.module.StdModuleTypes
|
|||||||
import com.intellij.openapi.roots.ModuleRootModificationUtil
|
import com.intellij.openapi.roots.ModuleRootModificationUtil
|
||||||
import org.jetbrains.kotlin.idea.project.PluginJetFilesProvider
|
import org.jetbrains.kotlin.idea.project.PluginJetFilesProvider
|
||||||
import com.intellij.codeInsight.daemon.DaemonAnalyzerTestCase
|
import com.intellij.codeInsight.daemon.DaemonAnalyzerTestCase
|
||||||
|
import com.intellij.openapi.application.WriteAction
|
||||||
import com.intellij.openapi.module.Module
|
import com.intellij.openapi.module.Module
|
||||||
import com.intellij.openapi.vfs.LocalFileSystem
|
import com.intellij.openapi.vfs.LocalFileSystem
|
||||||
import com.intellij.openapi.command.WriteCommandAction
|
import com.intellij.openapi.command.WriteCommandAction
|
||||||
|
import com.intellij.openapi.externalSystem.service.project.IdeModifiableModelsProviderImpl
|
||||||
import com.intellij.testFramework.PsiTestUtil
|
import com.intellij.testFramework.PsiTestUtil
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import com.intellij.openapi.util.io.FileUtil
|
import com.intellij.openapi.util.io.FileUtil
|
||||||
import com.intellij.openapi.roots.DependencyScope
|
import com.intellij.openapi.roots.DependencyScope
|
||||||
import com.intellij.testFramework.IdeaTestUtil
|
import com.sampullara.cli.Argument
|
||||||
|
import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments
|
||||||
|
import org.jetbrains.kotlin.config.*
|
||||||
|
import org.jetbrains.kotlin.idea.facet.getOrCreateFacet
|
||||||
import org.jetbrains.kotlin.idea.test.ConfigLibraryUtil
|
import org.jetbrains.kotlin.idea.test.ConfigLibraryUtil
|
||||||
import org.junit.Assert
|
import org.junit.Assert
|
||||||
|
|
||||||
@@ -48,7 +53,7 @@ abstract class AbstractMultiModuleHighlightingTest : DaemonAnalyzerTestCase() {
|
|||||||
|
|
||||||
protected fun module(name: String, hasTestRoot: Boolean = false, useFullJdk: Boolean = false): Module {
|
protected fun module(name: String, hasTestRoot: Boolean = false, useFullJdk: Boolean = false): Module {
|
||||||
val srcDir = TEST_DATA_PATH + "${getTestName(true)}/$name"
|
val srcDir = TEST_DATA_PATH + "${getTestName(true)}/$name"
|
||||||
val moduleWithSrcRootSet = createModuleFromTestData(srcDir, "$name", StdModuleTypes.JAVA, true)!!
|
val moduleWithSrcRootSet = createModuleFromTestData(srcDir, name, StdModuleTypes.JAVA, true)!!
|
||||||
if (hasTestRoot) {
|
if (hasTestRoot) {
|
||||||
setTestRoot(moduleWithSrcRootSet, name)
|
setTestRoot(moduleWithSrcRootSet, name)
|
||||||
}
|
}
|
||||||
@@ -78,4 +83,36 @@ abstract class AbstractMultiModuleHighlightingTest : DaemonAnalyzerTestCase() {
|
|||||||
dependencyScope: DependencyScope = DependencyScope.COMPILE,
|
dependencyScope: DependencyScope = DependencyScope.COMPILE,
|
||||||
exported: Boolean = false
|
exported: Boolean = false
|
||||||
) = ModuleRootModificationUtil.addDependency(this, other, dependencyScope, exported)
|
) = ModuleRootModificationUtil.addDependency(this, other, dependencyScope, exported)
|
||||||
|
|
||||||
|
private fun Module.createFacet() {
|
||||||
|
val accessToken = WriteAction.start()
|
||||||
|
try {
|
||||||
|
val modelsProvider = IdeModifiableModelsProviderImpl(project)
|
||||||
|
getOrCreateFacet(modelsProvider)
|
||||||
|
modelsProvider.commit()
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
accessToken.finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected fun Module.setPlatformKind(platformKind: TargetPlatformKind<*>) {
|
||||||
|
createFacet()
|
||||||
|
val facetSettings = KotlinFacetSettingsProvider.getInstance(project).getSettings(this)
|
||||||
|
val versionInfo = facetSettings.versionInfo
|
||||||
|
versionInfo.targetPlatformKind = platformKind
|
||||||
|
}
|
||||||
|
|
||||||
|
protected fun Module.enableMultiPlatform() {
|
||||||
|
createFacet()
|
||||||
|
val facetSettings = KotlinFacetSettingsProvider.getInstance(project).getSettings(this)
|
||||||
|
val compilerInfo = facetSettings.compilerInfo
|
||||||
|
val compilerSettings = CompilerSettings()
|
||||||
|
compilerSettings.additionalArguments += " -$multiPlatformArg"
|
||||||
|
compilerInfo.compilerSettings = compilerSettings
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private val multiPlatformArg = CommonCompilerArguments::multiPlatform.annotations.filterIsInstance<Argument>().single().value
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,6 +17,7 @@
|
|||||||
package org.jetbrains.kotlin.idea.caches.resolve
|
package org.jetbrains.kotlin.idea.caches.resolve
|
||||||
|
|
||||||
import com.intellij.openapi.roots.DependencyScope
|
import com.intellij.openapi.roots.DependencyScope
|
||||||
|
import org.jetbrains.kotlin.config.TargetPlatformKind
|
||||||
|
|
||||||
class MultiModuleHighlightingTest : AbstractMultiModuleHighlightingTest() {
|
class MultiModuleHighlightingTest : AbstractMultiModuleHighlightingTest() {
|
||||||
fun testVisibility() {
|
fun testVisibility() {
|
||||||
@@ -58,4 +59,57 @@ class MultiModuleHighlightingTest : AbstractMultiModuleHighlightingTest() {
|
|||||||
|
|
||||||
checkHighlightingInAllFiles()
|
checkHighlightingInAllFiles()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun testPlatform1() {
|
||||||
|
val header = module("header")
|
||||||
|
header.setPlatformKind(TargetPlatformKind.Default)
|
||||||
|
|
||||||
|
val jvm = module("jvm")
|
||||||
|
jvm.setPlatformKind(TargetPlatformKind.Jvm.JVM_1_6)
|
||||||
|
jvm.enableMultiPlatform()
|
||||||
|
jvm.addDependency(header)
|
||||||
|
|
||||||
|
checkHighlightingInAllFiles()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun testPlatform2() {
|
||||||
|
val header = module("header")
|
||||||
|
header.setPlatformKind(TargetPlatformKind.Default)
|
||||||
|
|
||||||
|
val jvm = module("jvm")
|
||||||
|
jvm.setPlatformKind(TargetPlatformKind.Jvm.JVM_1_6)
|
||||||
|
jvm.enableMultiPlatform()
|
||||||
|
jvm.addDependency(header)
|
||||||
|
|
||||||
|
checkHighlightingInAllFiles()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun testPlatform3() {
|
||||||
|
val header = module("header")
|
||||||
|
header.setPlatformKind(TargetPlatformKind.Default)
|
||||||
|
|
||||||
|
val jvm = module("jvm")
|
||||||
|
jvm.setPlatformKind(TargetPlatformKind.Jvm.JVM_1_6)
|
||||||
|
jvm.enableMultiPlatform()
|
||||||
|
jvm.addDependency(header)
|
||||||
|
|
||||||
|
val js = module("js")
|
||||||
|
js.setPlatformKind(TargetPlatformKind.JavaScript)
|
||||||
|
js.enableMultiPlatform()
|
||||||
|
js.addDependency(header)
|
||||||
|
|
||||||
|
checkHighlightingInAllFiles()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun testPlatform4() {
|
||||||
|
val header = module("header")
|
||||||
|
header.setPlatformKind(TargetPlatformKind.Default)
|
||||||
|
|
||||||
|
val jvm = module("jvm")
|
||||||
|
jvm.setPlatformKind(TargetPlatformKind.Jvm.JVM_1_6)
|
||||||
|
jvm.enableMultiPlatform()
|
||||||
|
jvm.addDependency(header)
|
||||||
|
|
||||||
|
checkHighlightingInAllFiles()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user