Implement transition project code style to predefined code style (KT-23400)
This commit is contained in:
@@ -7,16 +7,19 @@ package org.jetbrains.kotlin.idea.formatter
|
|||||||
|
|
||||||
import com.intellij.psi.codeStyle.CodeStyleSettings
|
import com.intellij.psi.codeStyle.CodeStyleSettings
|
||||||
import com.intellij.psi.codeStyle.CommonCodeStyleSettings
|
import com.intellij.psi.codeStyle.CommonCodeStyleSettings
|
||||||
import com.intellij.psi.codeStyle.PredefinedCodeStyle
|
|
||||||
import org.jetbrains.kotlin.idea.KotlinLanguage
|
import org.jetbrains.kotlin.idea.KotlinLanguage
|
||||||
import org.jetbrains.kotlin.idea.core.formatter.KotlinCodeStyleSettings
|
import org.jetbrains.kotlin.idea.core.formatter.KotlinCodeStyleSettings
|
||||||
|
|
||||||
class KotlinObsoleteCodeStyle : PredefinedCodeStyle(CODE_STYLE_TITLE, KotlinLanguage.INSTANCE) {
|
class KotlinObsoleteCodeStyle : KotlinPredefinedCodeStyle(CODE_STYLE_TITLE, KotlinLanguage.INSTANCE) {
|
||||||
|
override val codeStyleId: String = CODE_STYLE_ID
|
||||||
|
|
||||||
override fun apply(settings: CodeStyleSettings) {
|
override fun apply(settings: CodeStyleSettings) {
|
||||||
Companion.apply(settings)
|
Companion.apply(settings)
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
val INSTANCE = KotlinObsoleteCodeStyle()
|
||||||
|
|
||||||
const val CODE_STYLE_ID = "KOTLIN_OLD_DEFAULTS"
|
const val CODE_STYLE_ID = "KOTLIN_OLD_DEFAULTS"
|
||||||
const val CODE_STYLE_TITLE = "Kotlin obsolete IntelliJ IDEA codestyle"
|
const val CODE_STYLE_TITLE = "Kotlin obsolete IntelliJ IDEA codestyle"
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
/*
|
||||||
|
* 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.formatter
|
||||||
|
|
||||||
|
import com.intellij.lang.Language
|
||||||
|
import com.intellij.psi.codeStyle.PredefinedCodeStyle
|
||||||
|
|
||||||
|
abstract class KotlinPredefinedCodeStyle(name: String, language: Language) : PredefinedCodeStyle(name, language) {
|
||||||
|
abstract val codeStyleId: String
|
||||||
|
}
|
||||||
@@ -18,16 +18,19 @@ package org.jetbrains.kotlin.idea.formatter
|
|||||||
|
|
||||||
import com.intellij.psi.codeStyle.CodeStyleSettings
|
import com.intellij.psi.codeStyle.CodeStyleSettings
|
||||||
import com.intellij.psi.codeStyle.CommonCodeStyleSettings
|
import com.intellij.psi.codeStyle.CommonCodeStyleSettings
|
||||||
import com.intellij.psi.codeStyle.PredefinedCodeStyle
|
|
||||||
import org.jetbrains.kotlin.idea.KotlinLanguage
|
import org.jetbrains.kotlin.idea.KotlinLanguage
|
||||||
import org.jetbrains.kotlin.idea.core.formatter.KotlinCodeStyleSettings
|
import org.jetbrains.kotlin.idea.core.formatter.KotlinCodeStyleSettings
|
||||||
|
|
||||||
class KotlinStyleGuideCodeStyle : PredefinedCodeStyle("Kotlin style guide", KotlinLanguage.INSTANCE) {
|
class KotlinStyleGuideCodeStyle : KotlinPredefinedCodeStyle("Kotlin style guide", KotlinLanguage.INSTANCE) {
|
||||||
|
override val codeStyleId: String = CODE_STYLE_ID
|
||||||
|
|
||||||
override fun apply(settings: CodeStyleSettings) {
|
override fun apply(settings: CodeStyleSettings) {
|
||||||
Companion.apply(settings)
|
Companion.apply(settings)
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
val INSTANCE = KotlinStyleGuideCodeStyle()
|
||||||
|
|
||||||
const val CODE_STYLE_ID = "KOTLIN_OFFICIAL"
|
const val CODE_STYLE_ID = "KOTLIN_OFFICIAL"
|
||||||
const val CODE_STYLE_TITLE = "Kotlin Coding Conventions"
|
const val CODE_STYLE_TITLE = "Kotlin Coding Conventions"
|
||||||
|
|
||||||
@@ -36,7 +39,10 @@ class KotlinStyleGuideCodeStyle : PredefinedCodeStyle("Kotlin style guide", Kotl
|
|||||||
applyToCommonSettings(settings.kotlinCommonSettings)
|
applyToCommonSettings(settings.kotlinCommonSettings)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun applyToKotlinCustomSettings(kotlinCustomSettings: KotlinCodeStyleSettings, modifyCodeStyle: Boolean = true) {
|
fun applyToKotlinCustomSettings(
|
||||||
|
kotlinCustomSettings: KotlinCodeStyleSettings,
|
||||||
|
modifyCodeStyle: Boolean = true
|
||||||
|
) {
|
||||||
kotlinCustomSettings.apply {
|
kotlinCustomSettings.apply {
|
||||||
if (modifyCodeStyle) {
|
if (modifyCodeStyle) {
|
||||||
CODE_STYLE_DEFAULTS = CODE_STYLE_ID
|
CODE_STYLE_DEFAULTS = CODE_STYLE_ID
|
||||||
|
|||||||
@@ -0,0 +1,45 @@
|
|||||||
|
/*
|
||||||
|
* 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.formatter
|
||||||
|
|
||||||
|
import com.intellij.application.options.codeStyle.CodeStyleSchemesModel
|
||||||
|
import com.intellij.openapi.project.Project
|
||||||
|
import com.intellij.psi.codeStyle.CodeStyleSchemes
|
||||||
|
import com.intellij.psi.codeStyle.CodeStyleSettingsManager
|
||||||
|
import com.intellij.psi.impl.source.codeStyle.CodeStyleSchemesImpl
|
||||||
|
|
||||||
|
object ProjectCodeStyleImporter {
|
||||||
|
fun apply(project: Project, codeStyle: KotlinPredefinedCodeStyle) {
|
||||||
|
val schemeManager = CodeStyleSettingsManager.getInstance(project)
|
||||||
|
val schemesModel = CodeStyleSchemesModel(project)
|
||||||
|
|
||||||
|
val projectScheme = schemesModel.projectScheme
|
||||||
|
|
||||||
|
val currentScheme =
|
||||||
|
if (schemeManager.USE_PER_PROJECT_SETTINGS)
|
||||||
|
projectScheme
|
||||||
|
else
|
||||||
|
CodeStyleSchemes.getInstance().findPreferredScheme(schemeManager.PREFERRED_PROJECT_CODE_STYLE)
|
||||||
|
|
||||||
|
if (projectScheme != currentScheme) {
|
||||||
|
schemeManager.USE_PER_PROJECT_SETTINGS = true
|
||||||
|
schemeManager.PREFERRED_PROJECT_CODE_STYLE = null
|
||||||
|
|
||||||
|
CodeStyleSchemesImpl.getSchemeManager().setSchemes(listOf(), null, null)
|
||||||
|
}
|
||||||
|
|
||||||
|
val codeStyleSettings = projectScheme.codeStyleSettings
|
||||||
|
|
||||||
|
val kotlinCommonSettings = codeStyleSettings.kotlinCommonSettings
|
||||||
|
val kotlinCustomSettings = codeStyleSettings.kotlinCustomSettings
|
||||||
|
|
||||||
|
val defaults = kotlinCustomSettings.CODE_STYLE_DEFAULTS ?: kotlinCommonSettings.CODE_STYLE_DEFAULTS
|
||||||
|
if (defaults != codeStyle.codeStyleId) {
|
||||||
|
codeStyle.apply(codeStyleSettings)
|
||||||
|
schemeManager.mainProjectCodeStyle = codeStyleSettings
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user