From 5aacd181facdd53d77a6ab499e7df8be2ceb5a22 Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Tue, 7 Aug 2018 17:18:06 +0300 Subject: [PATCH] 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. --- .../formatter/KotlinCodeStyleSettings.java | 4 +++ .../KotlinCommonCodeStyleSettings.java | 4 +++ .../idea/formatter/KtCodeStyleSettings.kt | 35 ++++++++++++++++++ .../notifications/NewCodeStyleNotification.kt | 36 ++++++++++++++++--- 4 files changed, 74 insertions(+), 5 deletions(-) create mode 100644 idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KtCodeStyleSettings.kt diff --git a/idea/formatter/src/org/jetbrains/kotlin/idea/core/formatter/KotlinCodeStyleSettings.java b/idea/formatter/src/org/jetbrains/kotlin/idea/core/formatter/KotlinCodeStyleSettings.java index d7a40c154e7..32f86d05bfa 100644 --- a/idea/formatter/src/org/jetbrains/kotlin/idea/core/formatter/KotlinCodeStyleSettings.java +++ b/idea/formatter/src/org/jetbrains/kotlin/idea/core/formatter/KotlinCodeStyleSettings.java @@ -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); diff --git a/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KotlinCommonCodeStyleSettings.java b/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KotlinCommonCodeStyleSettings.java index 92ceb633323..bb0e694e7c4 100644 --- a/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KotlinCommonCodeStyleSettings.java +++ b/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KotlinCommonCodeStyleSettings.java @@ -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); diff --git a/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KtCodeStyleSettings.kt b/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KtCodeStyleSettings.kt new file mode 100644 index 00000000000..443dfb7b614 --- /dev/null +++ b/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KtCodeStyleSettings.kt @@ -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) +} \ No newline at end of file diff --git a/idea/idea-jvm/src/org/jetbrains/kotlin/idea/configuration/ui/notifications/NewCodeStyleNotification.kt b/idea/idea-jvm/src/org/jetbrains/kotlin/idea/configuration/ui/notifications/NewCodeStyleNotification.kt index 50ffa1b928b..b8d0395212d 100644 --- a/idea/idea-jvm/src/org/jetbrains/kotlin/idea/configuration/ui/notifications/NewCodeStyleNotification.kt +++ b/idea/idea-jvm/src/org/jetbrains/kotlin/idea/configuration/ui/notifications/NewCodeStyleNotification.kt @@ -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 -) \ No newline at end of file +) { + 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() + } + } + } +} \ No newline at end of file