[Kotlin] Added notification to alert user of the new default Kotlin Code Style

KTIJ-20988

GitOrigin-RevId: 16f46f18645ce8cf6b6101e8f2a8c77dafe2fa00
This commit is contained in:
Frederik Haselmeier
2024-01-12 15:04:11 +01:00
committed by intellij-monorepo-bot
parent 330289ae80
commit 298c8920a6
4 changed files with 98 additions and 15 deletions

View File

@@ -1,16 +1,39 @@
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.kotlin.idea
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.ModalityState
import com.intellij.openapi.application.ReadAction
import com.intellij.openapi.progress.blockingContext
import com.intellij.openapi.project.Project
import com.intellij.openapi.startup.ProjectActivity
import com.intellij.openapi.updateSettings.impl.UpdateChecker.excludedFromUpdateCheckPlugins
import com.intellij.util.concurrency.AppExecutorUtil
import org.jetbrains.kotlin.idea.base.util.containsNonScriptKotlinFile
import org.jetbrains.kotlin.idea.configuration.notifications.notifyKotlinStyleUpdateIfNeeded
import org.jetbrains.kotlin.idea.core.KotlinPluginDisposable
import java.util.concurrent.Callable
internal open class PluginStartupActivity : ProjectActivity {
override suspend fun execute(project: Project) : Unit = blockingContext {
excludedFromUpdateCheckPlugins.add("org.jetbrains.kotlin")
executeExtraActions(project)
if (ApplicationManager.getApplication().isHeadlessEnvironment) {
return@blockingContext
}
val pluginDisposable = KotlinPluginDisposable.getInstance(project)
ReadAction.nonBlocking(Callable { project.containsNonScriptKotlinFile() })
.inSmartMode(project)
.expireWith(pluginDisposable)
.finishOnUiThread(ModalityState.any()) { hasKotlinFiles ->
if (!hasKotlinFiles) return@finishOnUiThread
notifyKotlinStyleUpdateIfNeeded(project)
}
.submit(AppExecutorUtil.getAppExecutorService())
}
protected open fun executeExtraActions(project: Project) = Unit

View File

@@ -1,12 +1,6 @@
inspection.migration.title=Code Migration
inspection.migration.profile.name=Migration
kotlin.eap.survey.notification.group.name=Kotlin EAP Survey
kotlin.eap.survey.notification.title=Kotlin Early Access Program
kotlin.eap.survey.notification.text=We want to know more about you and how we can make the EAP experience more convenient!
kotlin.eap.survey.notification.action=Take EAP survey
kotlin.eap.survey.notification.link=https://surveys.jetbrains.com/s3/kotlin-eaps-survey
configuration.migration.text.detected.migration=Detected migration:
configuration.migration.text.language.version=Language version: {0} to {1}
configuration.migration.text.api.version=API version: {0} to {1}
@@ -14,9 +8,11 @@ configuration.migration.title.kotlin.migration=Kotlin migration
configuration.migration.text.update.your.code.to.replace.deprecated=Update your code to replace the use of deprecated language and library features with supported constructs
configuration.migration.text.scan.for.deprecations=Scan for deprecations
configuration.kotlin.code.style=Kotlin code style
configuration.notification.update.code.style.to.official=Do you want to update your Kotlin code style settings to the recommended ones?
configuration.apply.new.code.style=Apply the code style
kotlin.code.style.default.changed=Kotlin code style default changed
kotlin.code.style.default.changed.description=The default Kotlin code style was changed. It appears that this project does not explicitly specify \
a Kotlin code style. Please read the migration guide to avoid any unwanted formatting changes.
kotlin.code.style.default.changed.action=Read Migration Guide
kotlin.code.style.default.changed.action.url=https://kotl.in/new-code-style-defaults
kotlin.external.compiler.updates.notification.content.0=Kotlin version {0} is available
kotlin.external.compiler.updates.notification.learn.what.is.new.action=Learn what's new

View File

@@ -0,0 +1,70 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.kotlin.idea.configuration.notifications
import com.intellij.application.options.CodeStyle
import com.intellij.ide.BrowserUtil
import com.intellij.ide.util.PropertiesComponent
import com.intellij.notification.NotificationAction
import com.intellij.notification.NotificationGroupManager
import com.intellij.notification.NotificationType
import com.intellij.openapi.application.runWriteAction
import com.intellij.openapi.externalSystem.model.ExternalSystemDataKeys
import com.intellij.openapi.project.Project
import org.jetbrains.annotations.ApiStatus
import org.jetbrains.kotlin.idea.KotlinIcons
import org.jetbrains.kotlin.idea.formatter.KotlinOfficialStyleGuide
import org.jetbrains.kotlin.idea.formatter.KotlinStyleGuideCodeStyle
import org.jetbrains.kotlin.idea.formatter.ProjectCodeStyleImporter
import org.jetbrains.kotlin.idea.formatter.kotlinCodeStyleDefaults
import org.jetbrains.kotlin.idea.migration.KotlinMigrationBundle
private const val MAX_SHOW_NOTIFICATION = 1
private const val NOTIFICATION_PROPERTIES_KEY = "kotlin.code.style.migration.dialog.show.count"
@ApiStatus.Internal
fun notifyKotlinStyleUpdateIfNeeded(project: Project) {
val codeStyle = CodeStyle.getSettings(project).kotlinCodeStyleDefaults()
if (codeStyle == KotlinOfficialStyleGuide.CODE_STYLE_ID) return
if (project.getUserData(ExternalSystemDataKeys.NEWLY_CREATED_PROJECT) == java.lang.Boolean.TRUE) {
// project has been just created, switch to new Kotlin code style automatically
applyKotlinCodeStyleSetting(project)
return
}
if (codeStyle != null) {
// Code style is already explicitly specified
return
}
val propertiesComponent = PropertiesComponent.getInstance()
val notificationShowCount = propertiesComponent.getInt(NOTIFICATION_PROPERTIES_KEY, 0)
if (notificationShowCount >= MAX_SHOW_NOTIFICATION) {
return
}
propertiesComponent.setValue(NOTIFICATION_PROPERTIES_KEY, notificationShowCount + 1, 0)
NotificationGroupManager.getInstance()
.getNotificationGroup("Update Kotlin code style")
.createNotification(
KotlinMigrationBundle.message("kotlin.code.style.default.changed"),
KotlinMigrationBundle.htmlMessage("kotlin.code.style.default.changed.description"),
NotificationType.WARNING,
)
.setSuggestionType(true)
.addAction(readBlogPost())
.setImportant(true)
.setIcon(KotlinIcons.SMALL_LOGO)
.notify(project)
}
private fun readBlogPost() = NotificationAction.create(
KotlinMigrationBundle.message("kotlin.code.style.default.changed.action"),
) { _ ->
BrowserUtil.open(KotlinMigrationBundle.message("kotlin.code.style.default.changed.action.url"))
}
private fun applyKotlinCodeStyleSetting(project: Project) {
runWriteAction {
ProjectCodeStyleImporter.apply(project, KotlinStyleGuideCodeStyle.INSTANCE)
}
}

View File

@@ -451,12 +451,6 @@
displayType="STICKY_BALLOON"
key="configuration.migration.group.name"
/>
<notificationGroup
id="Kotlin EAP Survey"
displayType="STICKY_BALLOON"
key="kotlin.eap.survey.notification.group.name"
bundle="messages.KotlinMigrationBundle"
/>
<notificationGroup
id="Kotlin EAP"
displayType="STICKY_BALLOON"