From 995b4b892b43444cb9e8389276e26edf4c703154 Mon Sep 17 00:00:00 2001 From: "Victoria.Petrakovich" Date: Mon, 30 Sep 2024 17:54:23 +0200 Subject: [PATCH] [kotlin] Divide checking for Groovy DSL block existence and creation of it KTIJ-30671 GradleBuildScriptSupport should migrate to use compilerOptions GitOrigin-RevId: 7dc7ac0cd726fec8e4e5ec3c631ec8b12a8d92c6 --- .../groovy/GroovyGradleBuildScriptSupport.kt | 54 ++++++++++++------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/plugins/kotlin/gradle/code-insight-groovy/src/org/jetbrains/kotlin/idea/groovy/GroovyGradleBuildScriptSupport.kt b/plugins/kotlin/gradle/code-insight-groovy/src/org/jetbrains/kotlin/idea/groovy/GroovyGradleBuildScriptSupport.kt index 87d5fecd5272..af806725198c 100644 --- a/plugins/kotlin/gradle/code-insight-groovy/src/org/jetbrains/kotlin/idea/groovy/GroovyGradleBuildScriptSupport.kt +++ b/plugins/kotlin/gradle/code-insight-groovy/src/org/jetbrains/kotlin/idea/groovy/GroovyGradleBuildScriptSupport.kt @@ -587,21 +587,26 @@ class GroovyBuildScriptManipulator( compilerOptions.languageVersion.set(KotlinVersion.KOTLIN_1_9) } */ - var replaced = outerDslBlock.findAndReplaceCompilerOption(parameterName, parameterValue, compilerOption, replaceIt = replaceIt) - if (!replaced) { + var foundAndReplaced = + outerDslBlock.findAndReplaceCompilerOption(parameterName, parameterValue, compilerOption, replaceIt = replaceIt) + if (!foundAndReplaced) { /* If we didn't find, we try to find it in the `compilerOptions {` */ - val compilerOptionsBlock = outerDslBlock.getBlockOrCreate("compilerOptions") - replaced = compilerOptionsBlock.findAndReplaceCompilerOption( - parameterName, - parameterValue, - compilerOption, - checkOptionToExist = false, - replaceIt = replaceIt - ) + var compilerOptionsBlock = outerDslBlock.getBlockByName("compilerOptions") + if (compilerOptionsBlock != null) { + foundAndReplaced = compilerOptionsBlock.findAndReplaceCompilerOption( + parameterName, + parameterValue, + compilerOption, + checkCompilerOptionPrefixToExist = false, + replaceIt = replaceIt + ) + } else { + compilerOptionsBlock = outerDslBlock.createBlock("compilerOptions") + } - if (!replaced) { + if (!foundAndReplaced) { val added = compilerOptionsBlock.addLastExpressionInBlockIfNeeded(compilerOption.expression) if (added) { compilerOption.classToImport?.let { @@ -616,7 +621,7 @@ class GroovyBuildScriptManipulator( parameterName: String, parameterValue: String, compilerOption: CompilerOption, - checkOptionToExist: Boolean = true, + checkCompilerOptionPrefixToExist: Boolean = true, replaceIt: GrStatement.(/* insideKotlinOptions = */ Boolean, /* precompiledReplacement = */ String?, /* insideCompilerOptions = */ Boolean @@ -627,7 +632,7 @@ class GroovyBuildScriptManipulator( val statementLeftPartText = (stmt as? GrAssignmentExpression)?.lValue?.text ?: (stmt as? GrMethodCallExpression)?.invokedExpression?.text val statementContainsParameterName = if (statementLeftPartText?.contains(parameterName) == true) { - if (checkOptionToExist && statementLeftPartText.contains("compilerOptions.$parameterName")) { + if (checkCompilerOptionPrefixToExist && statementLeftPartText.contains("compilerOptions.$parameterName")) { insideCompilerOptions = true } true @@ -836,16 +841,27 @@ class GroovyBuildScriptManipulator( ): GrClosableBlock { var block = getBlockByName(name) if (block == null) { - val factory = GroovyPsiElementFactory.getInstance(project) - val newBlock = factory.createExpressionFromText("$name{\n}\n") - if (!customInsert(newBlock)) { - addAfter(newBlock, statements.lastOrNull() ?: firstChild) - } - block = getBlockByName(name)!! + block = createBlock(name, customInsert) } return block } + /** + * Use with caution and only if you performed `getBlockByName(name)` right before calling this method! + * Otherwise, use org.jetbrains.kotlin.idea.groovy.GroovyBuildScriptManipulator.Companion.getBlockOrCreate + */ + private fun GrStatementOwner.createBlock( + name: String, + customInsert: GrStatementOwner.(newBlock: PsiElement) -> Boolean = { false } + ): GrClosableBlock { + val factory = GroovyPsiElementFactory.getInstance(project) + val newBlock = factory.createExpressionFromText("$name{\n}\n") + if (!customInsert(newBlock)) { + addAfter(newBlock, statements.lastOrNull() ?: firstChild) + } + return getBlockByName(name)!! + } + fun GrStatementOwner.getBlockOrPrepend(name: String) = getBlockOrCreate(name) { newBlock -> addAfter(newBlock, null) true