Import code style if gradle.properties has kotlin.code.style option (KT-23400)
This commit is contained in:
@@ -21,6 +21,7 @@ class KotlinObsoleteCodeStyle : KotlinPredefinedCodeStyle(CODE_STYLE_TITLE, Kotl
|
|||||||
val INSTANCE = KotlinObsoleteCodeStyle()
|
val INSTANCE = KotlinObsoleteCodeStyle()
|
||||||
|
|
||||||
const val CODE_STYLE_ID = "KOTLIN_OLD_DEFAULTS"
|
const val CODE_STYLE_ID = "KOTLIN_OLD_DEFAULTS"
|
||||||
|
const val CODE_STYLE_SETTING = "obsolete"
|
||||||
const val CODE_STYLE_TITLE = "Kotlin obsolete IntelliJ IDEA codestyle"
|
const val CODE_STYLE_TITLE = "Kotlin obsolete IntelliJ IDEA codestyle"
|
||||||
|
|
||||||
fun apply(settings: CodeStyleSettings) {
|
fun apply(settings: CodeStyleSettings) {
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ class KotlinStyleGuideCodeStyle : KotlinPredefinedCodeStyle("Kotlin style guide"
|
|||||||
val INSTANCE = KotlinStyleGuideCodeStyle()
|
val INSTANCE = KotlinStyleGuideCodeStyle()
|
||||||
|
|
||||||
const val CODE_STYLE_ID = "KOTLIN_OFFICIAL"
|
const val CODE_STYLE_ID = "KOTLIN_OFFICIAL"
|
||||||
|
const val CODE_STYLE_SETTING = "official"
|
||||||
const val CODE_STYLE_TITLE = "Kotlin Coding Conventions"
|
const val CODE_STYLE_TITLE = "Kotlin Coding Conventions"
|
||||||
|
|
||||||
fun apply(settings: CodeStyleSettings) {
|
fun apply(settings: CodeStyleSettings) {
|
||||||
|
|||||||
+31
@@ -0,0 +1,31 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||||
|
* that can be found in the license/LICENSE.txt file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.idea.configuration
|
||||||
|
|
||||||
|
import com.intellij.openapi.project.Project
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
internal object GradlePropertiesFileUtils {
|
||||||
|
const val KOTLIN_CODE_STYLE_GRADLE_SETTING = "kotlin.code.style"
|
||||||
|
|
||||||
|
private const val GRADLE_PROPERTIES_FILE_NAME = "gradle.properties"
|
||||||
|
private const val GRADLE_PROPERTIES_LOCAL_FILE_NAME = "local.properties"
|
||||||
|
|
||||||
|
private val gradlePropertyFiles = listOf(GRADLE_PROPERTIES_LOCAL_FILE_NAME, GRADLE_PROPERTIES_FILE_NAME)
|
||||||
|
|
||||||
|
fun readProperty(project: Project, propertyName: String): String? {
|
||||||
|
for (propertyFileName in gradlePropertyFiles) {
|
||||||
|
val propertyFile = project.baseDir.findChild(propertyFileName) ?: continue
|
||||||
|
val properties = Properties()
|
||||||
|
properties.load(propertyFile.inputStream)
|
||||||
|
properties.getProperty(propertyName)?.let {
|
||||||
|
return it
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
+16
-12
@@ -42,6 +42,9 @@ import org.jetbrains.kotlin.gradle.ArgsInfo
|
|||||||
import org.jetbrains.kotlin.gradle.CompilerArgumentsBySourceSet
|
import org.jetbrains.kotlin.gradle.CompilerArgumentsBySourceSet
|
||||||
import org.jetbrains.kotlin.idea.compiler.configuration.KotlinCommonCompilerArgumentsHolder
|
import org.jetbrains.kotlin.idea.compiler.configuration.KotlinCommonCompilerArgumentsHolder
|
||||||
import org.jetbrains.kotlin.idea.facet.*
|
import org.jetbrains.kotlin.idea.facet.*
|
||||||
|
import org.jetbrains.kotlin.idea.formatter.KotlinObsoleteCodeStyle
|
||||||
|
import org.jetbrains.kotlin.idea.formatter.KotlinStyleGuideCodeStyle
|
||||||
|
import org.jetbrains.kotlin.idea.formatter.ProjectCodeStyleImporter
|
||||||
import org.jetbrains.kotlin.idea.framework.CommonLibraryKind
|
import org.jetbrains.kotlin.idea.framework.CommonLibraryKind
|
||||||
import org.jetbrains.kotlin.idea.framework.JSLibraryKind
|
import org.jetbrains.kotlin.idea.framework.JSLibraryKind
|
||||||
import org.jetbrains.kotlin.idea.framework.detectLibraryKind
|
import org.jetbrains.kotlin.idea.framework.detectLibraryKind
|
||||||
@@ -95,7 +98,7 @@ class KotlinGradleProjectSettingsDataService : AbstractProjectDataService<Projec
|
|||||||
if (settings.useProjectSettings) null else settings
|
if (settings.useProjectSettings) null else settings
|
||||||
}
|
}
|
||||||
val languageVersion = allSettings.asSequence().mapNotNullTo(LinkedHashSet()) { it.languageLevel }.singleOrNull()
|
val languageVersion = allSettings.asSequence().mapNotNullTo(LinkedHashSet()) { it.languageLevel }.singleOrNull()
|
||||||
val apiVersion = allSettings.asSequence().mapNotNullTo(LinkedHashSet()) { it.apiLevel}.singleOrNull()
|
val apiVersion = allSettings.asSequence().mapNotNullTo(LinkedHashSet()) { it.apiLevel }.singleOrNull()
|
||||||
KotlinCommonCompilerArgumentsHolder.getInstance(project).update {
|
KotlinCommonCompilerArgumentsHolder.getInstance(project).update {
|
||||||
if (languageVersion != null) {
|
if (languageVersion != null) {
|
||||||
this.languageVersion = languageVersion.versionString
|
this.languageVersion = languageVersion.versionString
|
||||||
@@ -145,6 +148,15 @@ class KotlinGradleProjectDataService : AbstractProjectDataService<ModuleData, Vo
|
|||||||
val kotlinFacet = configureFacetByGradleModule(ideModule, modelsProvider, moduleNode, null) ?: continue
|
val kotlinFacet = configureFacetByGradleModule(ideModule, modelsProvider, moduleNode, null) ?: continue
|
||||||
GradleProjectImportHandler.getInstances(project).forEach { it.importByModule(kotlinFacet, moduleNode) }
|
GradleProjectImportHandler.getInstances(project).forEach { it.importByModule(kotlinFacet, moduleNode) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val property = GradlePropertiesFileUtils.readProperty(project, GradlePropertiesFileUtils.KOTLIN_CODE_STYLE_GRADLE_SETTING)
|
||||||
|
when (property) {
|
||||||
|
KotlinObsoleteCodeStyle.CODE_STYLE_SETTING ->
|
||||||
|
ProjectCodeStyleImporter.apply(project, KotlinObsoleteCodeStyle.INSTANCE)
|
||||||
|
KotlinStyleGuideCodeStyle.CODE_STYLE_SETTING ->
|
||||||
|
ProjectCodeStyleImporter.apply(project, KotlinStyleGuideCodeStyle.INSTANCE)
|
||||||
|
else -> return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -260,7 +272,7 @@ fun configureFacetByGradleModule(
|
|||||||
}
|
}
|
||||||
|
|
||||||
val compilerVersion = moduleNode.findAll(BuildScriptClasspathData.KEY).firstOrNull()?.data?.let(::findKotlinPluginVersion)
|
val compilerVersion = moduleNode.findAll(BuildScriptClasspathData.KEY).firstOrNull()?.data?.let(::findKotlinPluginVersion)
|
||||||
?: return null
|
?: return null
|
||||||
val platformKind = detectPlatformKindByPlugin(moduleNode) ?: detectPlatformByLibrary(moduleNode)
|
val platformKind = detectPlatformKindByPlugin(moduleNode) ?: detectPlatformByLibrary(moduleNode)
|
||||||
|
|
||||||
// TODO there should be a way to figure out the correct platform version
|
// TODO there should be a way to figure out the correct platform version
|
||||||
@@ -326,15 +338,7 @@ internal fun adjustClasspath(kotlinFacet: KotlinFacet, dependencyClasspath: List
|
|||||||
arguments.classpath = if (newClasspath.isNotEmpty()) newClasspath.joinToString(File.pathSeparator) else null
|
arguments.classpath = if (newClasspath.isNotEmpty()) newClasspath.joinToString(File.pathSeparator) else null
|
||||||
}
|
}
|
||||||
|
|
||||||
private val gradlePropertyFiles = listOf("local.properties", "gradle.properties")
|
|
||||||
|
|
||||||
internal fun findKotlinCoroutinesProperty(project: Project): String {
|
internal fun findKotlinCoroutinesProperty(project: Project): String {
|
||||||
for (propertyFileName in gradlePropertyFiles) {
|
return GradlePropertiesFileUtils.readProperty(project, "kotlin.coroutines")
|
||||||
val propertyFile = project.baseDir.findChild(propertyFileName) ?: continue
|
?: CoroutineSupport.getCompilerArgument(LanguageFeature.Coroutines.defaultState)
|
||||||
val properties = Properties()
|
|
||||||
properties.load(propertyFile.inputStream)
|
|
||||||
properties.getProperty("kotlin.coroutines")?.let { return it }
|
|
||||||
}
|
|
||||||
|
|
||||||
return CoroutineSupport.getCompilerArgument(LanguageFeature.Coroutines.defaultState)
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user