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)
}
@@ -6,12 +6,15 @@
package org.jetbrains.kotlin.idea.configuration.ui.notifications
import com.intellij.ide.util.PropertiesComponent
import com.intellij.notification.Notification
import com.intellij.notification.NotificationDisplayType
import com.intellij.notification.NotificationType
import com.intellij.notification.NotificationsConfiguration
import com.intellij.notification.*
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.runWriteAction
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.idea.formatter.canRestore
import org.jetbrains.kotlin.idea.formatter.ktCodeStyleSettings
import org.jetbrains.kotlin.idea.formatter.restore
import org.jetbrains.kotlin.idea.util.isDefaultOfficialCodeStyle
private const val KOTLIN_UPDATE_CODE_STYLE_GROUP_ID = "Update Kotlin code style"
@@ -49,4 +52,27 @@ private class KotlinCodeStyleChangedNotification(val project: Project) : Notific
""".trimIndent(),
NotificationType.WARNING,
null
)
) {
init {
val ktFormattingSettings = ktCodeStyleSettings(project)
if (ktFormattingSettings != null && ktFormattingSettings.canRestore()) {
addAction(object : NotificationAction("Restore old settings") {
override fun actionPerformed(e: AnActionEvent, notification: Notification) {
restore(project)
}
})
}
}
companion object {
val LOG = Logger.getInstance("KotlinCodeStyleChangedNotification")
private fun restore(project: Project) {
val ktSettings = ktCodeStyleSettings(project) ?: return
runWriteAction {
ktSettings.restore()
}
}
}
}