diff --git a/idea/src/org/jetbrains/kotlin/idea/configuration/KotlinWithGradleConfigurator.kt b/idea/src/org/jetbrains/kotlin/idea/configuration/KotlinWithGradleConfigurator.kt index d97f88864ff..7fc367b82c3 100644 --- a/idea/src/org/jetbrains/kotlin/idea/configuration/KotlinWithGradleConfigurator.kt +++ b/idea/src/org/jetbrains/kotlin/idea/configuration/KotlinWithGradleConfigurator.kt @@ -303,8 +303,15 @@ abstract class KotlinWithGradleConfigurator : KotlinProjectConfigurator { private fun changeVersion(gradleFile: GroovyFile, version: String, forTests: Boolean, versionParameter: String): PsiElement? { val snippet = "$versionParameter = \"$version\"" val kotlinBlock = getBlockOrCreate(gradleFile, if (forTests) "compileTestKotlin" else "compileKotlin") - val experimentalBlock = getBlockOrCreate(kotlinBlock, "kotlinOptions") - addOrReplaceExpression(experimentalBlock, snippet) { stmt -> + + for (stmt in kotlinBlock.statements) { + if ((stmt as? GrAssignmentExpression)?.lValue?.text == "kotlinOptions." + versionParameter) { + return stmt.replaceWithStatementFromText("kotlinOptions." + snippet) + } + } + + val kotlinOptionsBlock = getBlockOrCreate(kotlinBlock, "kotlinOptions") + addOrReplaceExpression(kotlinOptionsBlock, snippet) { stmt -> (stmt as? GrAssignmentExpression)?.lValue?.text == versionParameter } return kotlinBlock.parent @@ -323,14 +330,18 @@ abstract class KotlinWithGradleConfigurator : KotlinProjectConfigurator { private fun addOrReplaceExpression(block: GrClosableBlock, snippet: String, predicate: (GrStatement) -> Boolean) { block.statements.firstOrNull(predicate)?.let { stmt -> - val newStatement = GroovyPsiElementFactory.getInstance(block.project).createExpressionFromText(snippet) - CodeStyleManager.getInstance(block.project).reformat(newStatement) - stmt.replaceWithStatement(newStatement) + stmt.replaceWithStatementFromText(snippet) return } addLastExpressionInBlockIfNeeded(snippet, block) } + private fun GrStatement.replaceWithStatementFromText(snippet: String): GrStatement { + val newStatement = GroovyPsiElementFactory.getInstance(project).createExpressionFromText(snippet) + CodeStyleManager.getInstance(project).reformat(newStatement) + return replaceWithStatement(newStatement) + } + fun getKotlinStdlibVersion(module: Module): String? { val gradleFilePath = getModuleFilePath(module) val gradleFile = getBuildGradleFile(module.project, gradleFilePath) ?: return null