Fix code style applier (KT-23400)
- Stop removing defined schemes and clone settings before modification - Clone code styles before modification - Don't bother users that already have needed code style with settings change
This commit is contained in:
+16
-28
@@ -5,11 +5,9 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.idea.formatter
|
package org.jetbrains.kotlin.idea.formatter
|
||||||
|
|
||||||
import com.intellij.application.options.codeStyle.CodeStyleSchemesModel
|
|
||||||
import com.intellij.openapi.project.Project
|
import com.intellij.openapi.project.Project
|
||||||
import com.intellij.psi.codeStyle.CodeStyleSchemes
|
import com.intellij.psi.codeStyle.CodeStyleSettings
|
||||||
import com.intellij.psi.codeStyle.CodeStyleSettingsManager
|
import com.intellij.psi.codeStyle.CodeStyleSettingsManager
|
||||||
import com.intellij.psi.impl.source.codeStyle.CodeStyleSchemesImpl
|
|
||||||
|
|
||||||
object ProjectCodeStyleImporter {
|
object ProjectCodeStyleImporter {
|
||||||
fun apply(project: Project, codeStyleStr: String?): Boolean {
|
fun apply(project: Project, codeStyleStr: String?): Boolean {
|
||||||
@@ -26,34 +24,24 @@ object ProjectCodeStyleImporter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun apply(project: Project, codeStyle: KotlinPredefinedCodeStyle) {
|
fun apply(project: Project, predefinedCodeStyle: KotlinPredefinedCodeStyle) {
|
||||||
val schemeManager = CodeStyleSettingsManager.getInstance(project)
|
val settingsManager = CodeStyleSettingsManager.getInstance(project)
|
||||||
val schemesModel = CodeStyleSchemesModel(project)
|
|
||||||
|
|
||||||
val projectScheme = schemesModel.projectScheme
|
val currentSettings = settingsManager.currentSettings
|
||||||
|
if (predefinedCodeStyle.codeStyleId == currentSettings.kotlinCodeStyleDefaults()) {
|
||||||
val currentScheme =
|
// Don't bother user that already have correct code style
|
||||||
if (schemeManager.USE_PER_PROJECT_SETTINGS)
|
return
|
||||||
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 projectSettingsUpdated: CodeStyleSettings = if (settingsManager.USE_PER_PROJECT_SETTINGS) {
|
||||||
|
settingsManager.currentSettings.clone()
|
||||||
val kotlinCommonSettings = codeStyleSettings.kotlinCommonSettings
|
} else {
|
||||||
val kotlinCustomSettings = codeStyleSettings.kotlinCustomSettings
|
CodeStyleSettings()
|
||||||
|
|
||||||
val defaults = kotlinCustomSettings.CODE_STYLE_DEFAULTS ?: kotlinCommonSettings.CODE_STYLE_DEFAULTS
|
|
||||||
if (defaults != codeStyle.codeStyleId) {
|
|
||||||
codeStyle.apply(codeStyleSettings)
|
|
||||||
schemeManager.mainProjectCodeStyle = codeStyleSettings
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
settingsManager.USE_PER_PROJECT_SETTINGS = true
|
||||||
|
|
||||||
|
predefinedCodeStyle.apply(projectSettingsUpdated)
|
||||||
|
settingsManager.mainProjectCodeStyle = projectSettingsUpdated
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user