Support updating language/API level specified with compact options
This commit is contained in:
@@ -303,8 +303,15 @@ abstract class KotlinWithGradleConfigurator : KotlinProjectConfigurator {
|
|||||||
private fun changeVersion(gradleFile: GroovyFile, version: String, forTests: Boolean, versionParameter: String): PsiElement? {
|
private fun changeVersion(gradleFile: GroovyFile, version: String, forTests: Boolean, versionParameter: String): PsiElement? {
|
||||||
val snippet = "$versionParameter = \"$version\""
|
val snippet = "$versionParameter = \"$version\""
|
||||||
val kotlinBlock = getBlockOrCreate(gradleFile, if (forTests) "compileTestKotlin" else "compileKotlin")
|
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
|
(stmt as? GrAssignmentExpression)?.lValue?.text == versionParameter
|
||||||
}
|
}
|
||||||
return kotlinBlock.parent
|
return kotlinBlock.parent
|
||||||
@@ -323,14 +330,18 @@ abstract class KotlinWithGradleConfigurator : KotlinProjectConfigurator {
|
|||||||
|
|
||||||
private fun addOrReplaceExpression(block: GrClosableBlock, snippet: String, predicate: (GrStatement) -> Boolean) {
|
private fun addOrReplaceExpression(block: GrClosableBlock, snippet: String, predicate: (GrStatement) -> Boolean) {
|
||||||
block.statements.firstOrNull(predicate)?.let { stmt ->
|
block.statements.firstOrNull(predicate)?.let { stmt ->
|
||||||
val newStatement = GroovyPsiElementFactory.getInstance(block.project).createExpressionFromText(snippet)
|
stmt.replaceWithStatementFromText(snippet)
|
||||||
CodeStyleManager.getInstance(block.project).reformat(newStatement)
|
|
||||||
stmt.replaceWithStatement(newStatement)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
addLastExpressionInBlockIfNeeded(snippet, block)
|
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? {
|
fun getKotlinStdlibVersion(module: Module): String? {
|
||||||
val gradleFilePath = getModuleFilePath(module)
|
val gradleFilePath = getModuleFilePath(module)
|
||||||
val gradleFile = getBuildGradleFile(module.project, gradleFilePath) ?: return null
|
val gradleFile = getBuildGradleFile(module.project, gradleFilePath) ?: return null
|
||||||
|
|||||||
Reference in New Issue
Block a user