Restore old code style settings action (KT-23400)

Can't use `KotlinCodeStyleSettings` instead `KtCodeStyleSettings`,
because current `KotlinCodeStyleSettings` class is already used in third
party plugin.
This commit is contained in:
Nikolay Krasko
2018-08-07 17:18:06 +03:00
parent 6920e3bd08
commit 5aacd181fa
4 changed files with 74 additions and 5 deletions
@@ -170,6 +170,10 @@ public class KotlinCodeStyleSettings extends CustomCodeStyleSettings {
return tempSettings;
}
public boolean canRestore() {
return settingsAgainstPreviousDefaults != null;
}
public void restore() {
if (settingsAgainstPreviousDefaults != null) {
copyFrom(settingsAgainstPreviousDefaults);
@@ -231,6 +231,10 @@ public class KotlinCommonCodeStyleSettings extends CommonCodeStyleSettings {
return provider == null ? null : provider.getSupportedFields();
}
public boolean canRestore() {
return settingsAgainstPreviousDefaults != null;
}
public void restore() {
if (settingsAgainstPreviousDefaults != null) {
CompatibilityKt.copyFromEx(this, settingsAgainstPreviousDefaults);
@@ -0,0 +1,35 @@
/*
* 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.openapi.project.Project
import com.intellij.psi.codeStyle.CodeStyleSettingsManager
import org.jetbrains.kotlin.idea.KotlinLanguage
import org.jetbrains.kotlin.idea.core.formatter.KotlinCodeStyleSettings
data class KtCodeStyleSettings(
val custom: KotlinCodeStyleSettings,
val common: KotlinCommonCodeStyleSettings
)
fun KtCodeStyleSettings.canRestore(): Boolean {
return custom.canRestore() || common.canRestore()
}
fun KtCodeStyleSettings.restore() {
custom.restore()
common.restore()
}
fun ktCodeStyleSettings(project: Project): KtCodeStyleSettings? {
@Suppress("DEPRECATION") // Suggested update is not supported in 173. BUNCH: 181
val settings = CodeStyleSettingsManager.getSettings(project)
val ktCommonSettings = settings.getCommonSettings(KotlinLanguage.INSTANCE) as KotlinCommonCodeStyleSettings
val ktCustomSettings = settings.getCustomSettings(KotlinCodeStyleSettings::class.java)
return KtCodeStyleSettings(ktCustomSettings, ktCommonSettings)
}