diff --git a/idea/idea-test-framework/test/org/jetbrains/kotlin/idea/test/KotlinLightCodeInsightFixtureTestCase.kt b/idea/idea-test-framework/test/org/jetbrains/kotlin/idea/test/KotlinLightCodeInsightFixtureTestCase.kt index 2898eff6bd3..22858c1ec79 100644 --- a/idea/idea-test-framework/test/org/jetbrains/kotlin/idea/test/KotlinLightCodeInsightFixtureTestCase.kt +++ b/idea/idea-test-framework/test/org/jetbrains/kotlin/idea/test/KotlinLightCodeInsightFixtureTestCase.kt @@ -40,9 +40,7 @@ import org.jetbrains.kotlin.config.LanguageVersion import org.jetbrains.kotlin.idea.KotlinFileType import org.jetbrains.kotlin.idea.compiler.configuration.KotlinCommonCompilerArgumentsHolder import org.jetbrains.kotlin.idea.compiler.configuration.KotlinCompilerSettings -import org.jetbrains.kotlin.idea.facet.KotlinFacet -import org.jetbrains.kotlin.idea.facet.configureFacet -import org.jetbrains.kotlin.idea.facet.getOrCreateFacet +import org.jetbrains.kotlin.idea.facet.* import org.jetbrains.kotlin.idea.inspections.UnusedSymbolInspection import org.jetbrains.kotlin.idea.test.CompilerTestDirectives.COMPILER_ARGUMENTS_DIRECTIVE import org.jetbrains.kotlin.idea.test.CompilerTestDirectives.JVM_TARGET_DIRECTIVE @@ -237,12 +235,13 @@ object CompilerTestDirectives { } fun withCustomCompilerOptions(fileText: String, project: Project, module: Module, body: () -> T): T { + val removeFacet = !module.hasKotlinFacet() val configured = configureCompilerOptions(fileText, project, module) try { return body() } finally { if (configured) { - rollbackCompilerOptions(project, module) + rollbackCompilerOptions(project, module, removeFacet) } } } @@ -296,7 +295,15 @@ fun configureRegistryAndRun(fileText: String, body: () -> T) { } } -private fun rollbackCompilerOptions(project: Project, module: Module) { +private fun rollbackCompilerOptions(project: Project, module: Module, removeFacet: Boolean) { + KotlinCompilerSettings.getInstance(project).update { this.additionalArguments = DEFAULT_ADDITIONAL_ARGUMENTS } + KotlinCommonCompilerArgumentsHolder.getInstance(project).update { this.languageVersion = LanguageVersion.LATEST_STABLE.versionString } + + if (removeFacet) { + module.removeKotlinFacet(IdeModifiableModelsProviderImpl(project), commitModel = true) + return + } + configureLanguageAndApiVersion(project, module, LanguageVersion.LATEST_STABLE.versionString) val facetSettings = KotlinFacet.get(module)!!.configuration.settings @@ -307,7 +314,6 @@ private fun rollbackCompilerOptions(project: Project, module: Module) { } compilerSettings.additionalArguments = DEFAULT_ADDITIONAL_ARGUMENTS facetSettings.updateMergedArguments() - KotlinCompilerSettings.getInstance(project).update { this.additionalArguments = DEFAULT_ADDITIONAL_ARGUMENTS } } fun withCustomLanguageAndApiVersion( @@ -317,11 +323,18 @@ fun withCustomLanguageAndApiVersion( apiVersion: String?, body: () -> Unit ) { + val removeFacet = !module.hasKotlinFacet() configureLanguageAndApiVersion(project, module, languageVersion, apiVersion) try { body() } finally { - configureLanguageAndApiVersion(project, module, LanguageVersion.LATEST_STABLE.versionString, null) + if (removeFacet) { + KotlinCommonCompilerArgumentsHolder.getInstance(project) + .update { this.languageVersion = LanguageVersion.LATEST_STABLE.versionString } + module.removeKotlinFacet(IdeModifiableModelsProviderImpl(project), commitModel = true) + } else { + configureLanguageAndApiVersion(project, module, LanguageVersion.LATEST_STABLE.versionString, null) + } } } diff --git a/idea/src/org/jetbrains/kotlin/idea/facet/facetUtils.kt b/idea/src/org/jetbrains/kotlin/idea/facet/facetUtils.kt index 66d7366e1b5..8b9d9785e16 100644 --- a/idea/src/org/jetbrains/kotlin/idea/facet/facetUtils.kt +++ b/idea/src/org/jetbrains/kotlin/idea/facet/facetUtils.kt @@ -5,6 +5,7 @@ package org.jetbrains.kotlin.idea.facet +import com.intellij.facet.FacetManager import com.intellij.openapi.externalSystem.service.project.IdeModifiableModelsProvider import com.intellij.openapi.module.Module import com.intellij.openapi.projectRoots.JavaSdk @@ -133,6 +134,22 @@ fun Module.getOrCreateFacet( return facet } +fun Module.hasKotlinFacet() = FacetManager.getInstance(this).getFacetByType(KotlinFacetType.TYPE_ID) != null + +fun Module.removeKotlinFacet( + modelsProvider: IdeModifiableModelsProvider, + commitModel: Boolean = false +) { + val facetModel = modelsProvider.getModifiableFacetModel(this) + val facet = facetModel.findFacet(KotlinFacetType.TYPE_ID, KotlinFacetType.INSTANCE.defaultFacetName) ?: return + facetModel.removeFacet(facet) + if (commitModel) { + runWriteAction { + facetModel.commit() + } + } +} + //method used for non-mpp modules fun KotlinFacet.configureFacet( compilerVersion: String?,