diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..d5347bf --- /dev/null +++ b/.editorconfig @@ -0,0 +1,14 @@ +root = true + +[*] +charset = utf-8 +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true + +[*.{kt,kts}] +indent_style = space +indent_size = 4 +continuation_indent_size = 4 +max_line_length = off +ktlint_code_style = ktlint_official diff --git a/build.gradle.kts b/build.gradle.kts index bf50ef5..7347bea 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -12,6 +12,7 @@ plugins { alias(libs.plugins.intellijPlatform) alias(libs.plugins.changelog) alias(libs.plugins.kover) + alias(libs.plugins.ktlint) } group = properties("pluginGroup").get() @@ -34,12 +35,12 @@ dependencies { bundledPlugins( properties("platformBundledPlugins").map { it.split(",").map(String::trim).filter(String::isNotEmpty) - } + }, ) plugins( properties("platformPlugins").map { it.split(",").map(String::trim).filter(String::isNotEmpty) - } + }, ) } } @@ -67,29 +68,31 @@ intellijPlatform { name = properties("pluginName") version = properties("pluginVersion") - description = providers.fileContents(layout.projectDirectory.file("README.md")).asText.map { - val start = "" - val end = "" + description = + providers.fileContents(layout.projectDirectory.file("README.md")).asText.map { + val start = "" + val end = "" - with(it.lines()) { - if (!containsAll(listOf(start, end))) { - throw GradleException("Plugin description section not found in README.md:\n$start ... $end") + with(it.lines()) { + if (!containsAll(listOf(start, end))) { + throw GradleException("Plugin description section not found in README.md:\n$start ... $end") + } + subList(indexOf(start) + 1, indexOf(end)).joinToString("\n").let(::markdownToHTML) } - subList(indexOf(start) + 1, indexOf(end)).joinToString("\n").let(::markdownToHTML) } - } val changelog = project.changelog - changeNotes = properties("pluginVersion").map { pluginVersion -> - with(changelog) { - renderItem( - (getOrNull(pluginVersion) ?: getUnreleased()) - .withHeader(false) - .withEmptySections(false), - Changelog.OutputType.HTML, - ) + changeNotes = + properties("pluginVersion").map { pluginVersion -> + with(changelog) { + renderItem( + (getOrNull(pluginVersion) ?: getUnreleased()) + .withHeader(false) + .withEmptySections(false), + Changelog.OutputType.HTML, + ) + } } - } ideaVersion { sinceBuild = properties("pluginSinceBuild") @@ -101,8 +104,14 @@ intellijPlatform { token = providers.environmentVariable("PUBLISH_TOKEN") channels.set( properties("pluginVersion").map { - listOf(it.split("-").getOrElse(1) { "default" }.split(".").first()) - } + listOf( + it + .split("-") + .getOrElse(1) { "default" } + .split(".") + .first(), + ) + }, ) } @@ -128,6 +137,11 @@ kover { } } +ktlint { + version.set(libs.versions.ktlint) + outputToConsole.set(true) +} + tasks { wrapper { gradleVersion = properties("gradleVersion").get() diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 791be41..d054657 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -5,6 +5,8 @@ changelog = "2.5.0" intellijPlatform = "2.16.0" kotlin = "2.3.21" kover = "0.9.8" +ktlint = "1.8.0" +ktlintGradle = "14.2.0" [libraries] junit = { group = "junit", name = "junit", version.ref = "junit" } @@ -14,3 +16,4 @@ changelog = { id = "org.jetbrains.changelog", version.ref = "changelog" } intellijPlatform = { id = "org.jetbrains.intellij.platform", version.ref = "intellijPlatform" } kotlin = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" } kover = { id = "org.jetbrains.kotlinx.kover", version.ref = "kover" } +ktlint = { id = "org.jlleitschuh.gradle.ktlint", version.ref = "ktlintGradle" } diff --git a/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/CodeXPBundle.kt b/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/CodeXPBundle.kt index e95a286..b84d8a6 100644 --- a/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/CodeXPBundle.kt +++ b/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/CodeXPBundle.kt @@ -8,14 +8,17 @@ import org.jetbrains.annotations.PropertyKey private const val BUNDLE = "messages.CodeXPBundle" object CodeXPBundle : DynamicBundle(BUNDLE) { - @Suppress("SpreadOperator") @JvmStatic - fun message(@PropertyKey(resourceBundle = BUNDLE) key: String, vararg params: Any) = - getMessage(key, *params) + fun message( + @PropertyKey(resourceBundle = BUNDLE) key: String, + vararg params: Any, + ) = getMessage(key, *params) @Suppress("SpreadOperator", "unused") @JvmStatic - fun messagePointer(@PropertyKey(resourceBundle = BUNDLE) key: String, vararg params: Any) = - getLazyMessage(key, *params) + fun messagePointer( + @PropertyKey(resourceBundle = BUNDLE) key: String, + vararg params: Any, + ) = getLazyMessage(key, *params) } diff --git a/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/activities/CodeXPStartupActivity.kt b/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/activities/CodeXPStartupActivity.kt index baedf27..8722070 100644 --- a/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/activities/CodeXPStartupActivity.kt +++ b/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/activities/CodeXPStartupActivity.kt @@ -11,4 +11,4 @@ class CodeXPStartupActivity : StartupActivity { ApplicationManager.getApplication().getService(CodeXPService::class.java) CodeXPUIManager.createDialogArea() } -} \ No newline at end of file +} diff --git a/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/enums/Event.kt b/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/enums/Event.kt index c72673f..651f344 100644 --- a/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/enums/Event.kt +++ b/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/enums/Event.kt @@ -3,7 +3,9 @@ package com.github.ilovegamecoding.intellijcodexp.enums /** * Event enum for the plugin events. */ -enum class Event(val xpValue: Long) { +enum class Event( + val xpValue: Long, +) { NONE(0), TYPING(2), CUT(1), @@ -16,5 +18,5 @@ enum class Event(val xpValue: Long) { BUILD(5), RUN(10), DEBUG(20), - ACTION(5); -} \ No newline at end of file + ACTION(5), +} diff --git a/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/enums/PositionToDisplayGainedXP.kt b/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/enums/PositionToDisplayGainedXP.kt index 2ad2907..dc597be 100644 --- a/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/enums/PositionToDisplayGainedXP.kt +++ b/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/enums/PositionToDisplayGainedXP.kt @@ -3,7 +3,10 @@ package com.github.ilovegamecoding.intellijcodexp.enums /** * Position enum for the gained XP label. */ -enum class PositionToDisplayGainedXP(val x: Int, val y: Int) { +enum class PositionToDisplayGainedXP( + val x: Int, + val y: Int, +) { TOP(0, -1), TOP_LEFT(-1, -1), LEFT(-1, 0), @@ -11,5 +14,5 @@ enum class PositionToDisplayGainedXP(val x: Int, val y: Int) { BOTTOM(0, 1), BOTTOM_RIGHT(1, 1), RIGHT(1, 0), - TOP_RIGHT(1, -1) -} \ No newline at end of file + TOP_RIGHT(1, -1), +} diff --git a/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/listeners/CodeXPEventListener.kt b/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/listeners/CodeXPEventListener.kt index 79e53ed..e760e1b 100644 --- a/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/listeners/CodeXPEventListener.kt +++ b/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/listeners/CodeXPEventListener.kt @@ -23,5 +23,8 @@ interface CodeXPEventListener { * @param event The event that occurred. * @param dataContext The data context of the event. */ - fun eventOccurred(event: Event, dataContext: DataContext? = null) -} \ No newline at end of file + fun eventOccurred( + event: Event, + dataContext: DataContext? = null, + ) +} diff --git a/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/listeners/CodeXPListener.kt b/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/listeners/CodeXPListener.kt index 7bd90de..e187fe9 100644 --- a/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/listeners/CodeXPListener.kt +++ b/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/listeners/CodeXPListener.kt @@ -37,12 +37,19 @@ interface CodeXPListener { * * @param challenge Updated challenge. */ - fun challengeUpdated(event: Event, challenge: CodeXPChallenge, newChallenge: CodeXPChallenge? = null) + fun challengeUpdated( + event: Event, + challenge: CodeXPChallenge, + newChallenge: CodeXPChallenge? = null, + ) /** * Function that is called when challenge completed. * * @param challenge Completed challenge. */ - fun challengeCompleted(event: Event, challenge: CodeXPChallenge) -} \ No newline at end of file + fun challengeCompleted( + event: Event, + challenge: CodeXPChallenge, + ) +} diff --git a/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/listeners/IdeEventListener.kt b/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/listeners/IdeEventListener.kt index 1aedfa9..f8152ce 100644 --- a/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/listeners/IdeEventListener.kt +++ b/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/listeners/IdeEventListener.kt @@ -14,13 +14,20 @@ import com.intellij.openapi.application.ApplicationManager * This class listens to events from the IDE and fires them to the message bus. */ internal class IdeEventListener : AnActionListener { - override fun afterEditorTyping(c: Char, dataContext: DataContext) { + override fun afterEditorTyping( + c: Char, + dataContext: DataContext, + ) { super.afterEditorTyping(c, dataContext) fireEvent(Event.TYPING, dataContext) } - override fun afterActionPerformed(action: AnAction, event: AnActionEvent, result: AnActionResult) { + override fun afterActionPerformed( + action: AnAction, + event: AnActionEvent, + result: AnActionResult, + ) { super.afterActionPerformed(action, event, result) when (action.templateText) { @@ -45,8 +52,14 @@ internal class IdeEventListener : AnActionListener { * @param event The event to fire. * @param dataContext The data context of the event. */ - private fun fireEvent(event: Event, dataContext: DataContext? = null) { - ApplicationManager.getApplication().messageBus.syncPublisher(CodeXPEventListener.CODEXP_EVENT) + private fun fireEvent( + event: Event, + dataContext: DataContext? = null, + ) { + ApplicationManager + .getApplication() + .messageBus + .syncPublisher(CodeXPEventListener.CODEXP_EVENT) .eventOccurred(event, dataContext) } -} \ No newline at end of file +} diff --git a/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/managers/CodeXPNotificationManager.kt b/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/managers/CodeXPNotificationManager.kt index 94cc3f1..01ff1df 100644 --- a/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/managers/CodeXPNotificationManager.kt +++ b/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/managers/CodeXPNotificationManager.kt @@ -9,7 +9,10 @@ object CodeXPNotificationManager { private val notificationGroup: NotificationGroup = NotificationGroupManager.getInstance().getNotificationGroup("CodeXP") - private fun notify(title: String, content: String) { + private fun notify( + title: String, + content: String, + ) { notificationGroup.createNotification(title, content, NotificationType.INFORMATION).notify(null) } @@ -17,10 +20,14 @@ object CodeXPNotificationManager { notify("${codeXPChallenge.name} completed!", "Reward: ${codeXPChallenge.rewardXP} XP") } - fun notifyLevelUp(nickname: String, level: Int, xpToNextLevel: Long) { + fun notifyLevelUp( + nickname: String, + level: Int, + xpToNextLevel: Long, + ) { notify( "Level up!", - "Congratulations $nickname! You are now level $level. You need $xpToNextLevel XP to reach the next level." + "Congratulations $nickname! You are now level $level. You need $xpToNextLevel XP to reach the next level.", ) } -} \ No newline at end of file +} diff --git a/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/managers/CodeXPUIManager.kt b/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/managers/CodeXPUIManager.kt index 79fd116..1b357e0 100644 --- a/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/managers/CodeXPUIManager.kt +++ b/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/managers/CodeXPUIManager.kt @@ -5,10 +5,10 @@ import com.github.ilovegamecoding.intellijcodexp.listeners.CodeXPEventListener import com.github.ilovegamecoding.intellijcodexp.listeners.CodeXPListener import com.github.ilovegamecoding.intellijcodexp.models.CodeXPChallenge import com.github.ilovegamecoding.intellijcodexp.models.CodeXPConfiguration -import com.github.ilovegamecoding.intellijcodexp.views.CodeXPDialog import com.github.ilovegamecoding.intellijcodexp.models.CodeXPLevel import com.github.ilovegamecoding.intellijcodexp.services.CodeXPService import com.github.ilovegamecoding.intellijcodexp.utils.StringUtil +import com.github.ilovegamecoding.intellijcodexp.views.CodeXPDialog import com.intellij.openapi.actionSystem.CommonDataKeys import com.intellij.openapi.actionSystem.DataContext import com.intellij.openapi.application.ApplicationManager @@ -22,10 +22,15 @@ import java.awt.Font import java.awt.Point import java.awt.event.MouseAdapter import java.awt.event.MouseMotionAdapter -import javax.swing.* +import javax.swing.BoxLayout +import javax.swing.JComponent +import javax.swing.JLabel +import javax.swing.JLayeredPane +import javax.swing.JPanel +import javax.swing.SwingUtilities +import javax.swing.Timer import kotlin.math.max - /** * CodeXPUIManager class * @@ -77,12 +82,14 @@ object CodeXPUIManager : CodeXPEventListener, CodeXPListener { connection.subscribe(CodeXPListener.CODEXP, this) } - override fun eventOccurred(event: Event, dataContext: DataContext?) { + override fun eventOccurred( + event: Event, + dataContext: DataContext?, + ) { displayXPLabel(event, dataContext) } override fun xpUpdated(levelInfo: CodeXPLevel) { - } override fun levelUp(levelInfo: CodeXPLevel) { @@ -90,22 +97,28 @@ object CodeXPUIManager : CodeXPEventListener, CodeXPListener { CodeXPDialog.createDialog( "Level Up!", "Congratulations! You are now level ${StringUtil.numberToStringWithCommas(levelInfo.level.toLong())}!", - "XP to next level: ${StringUtil.numberToStringWithCommas(levelInfo.totalXPForNextLevel)} xp" - ) + "XP to next level: ${StringUtil.numberToStringWithCommas(levelInfo.totalXPForNextLevel)} xp", + ), ) } - override fun challengeUpdated(event: Event, challenge: CodeXPChallenge, newChallenge: CodeXPChallenge?) { - + override fun challengeUpdated( + event: Event, + challenge: CodeXPChallenge, + newChallenge: CodeXPChallenge?, + ) { } - override fun challengeCompleted(event: Event, challenge: CodeXPChallenge) { + override fun challengeCompleted( + event: Event, + challenge: CodeXPChallenge, + ) { showDialog( CodeXPDialog.createDialog( "Challenge Completed!", "Congratulations! You have completed ${challenge.name.lowercase()}!", - "XP earned: ${StringUtil.numberToStringWithCommas(challenge.rewardXP)} xp" - ) + "XP earned: ${StringUtil.numberToStringWithCommas(challenge.rewardXP)} xp", + ), ) } @@ -115,11 +128,17 @@ object CodeXPUIManager : CodeXPEventListener, CodeXPListener { * @param event The event to fire. * @param dataContext The data context of the event. */ - private fun displayXPLabel(event: Event, dataContext: DataContext?) { + private fun displayXPLabel( + event: Event, + dataContext: DataContext?, + ) { dataContext ?: return val codeXPConfiguration = - ApplicationManager.getApplication().getService(CodeXPService::class.java).state.codeXPConfiguration + ApplicationManager + .getApplication() + .getService(CodeXPService::class.java) + .state.codeXPConfiguration if (!codeXPConfiguration.showGainedXP) { return @@ -143,14 +162,15 @@ object CodeXPUIManager : CodeXPEventListener, CodeXPListener { currentXPGainValue += event.xpValue.toInt() - val newFadingLabel = FadingLabel(currentXPGainValue).apply { - font = Font(font.fontName, Font.BOLD, editor.colorsScheme.editorFontSize) - size = preferredSize - val caretHeight = editor.lineHeight - location = - calculateLabelLocation(codeXPConfiguration, fadingLabelPosition, caretHeight, preferredSize.width) - startFadeOut() - } + val newFadingLabel = + FadingLabel(currentXPGainValue).apply { + font = Font(font.fontName, Font.BOLD, editor.colorsScheme.editorFontSize) + size = preferredSize + val caretHeight = editor.lineHeight + location = + calculateLabelLocation(codeXPConfiguration, fadingLabelPosition, caretHeight, preferredSize.width) + startFadeOut() + } fadingLabels[component] = newFadingLabel @@ -165,14 +185,15 @@ object CodeXPUIManager : CodeXPEventListener, CodeXPListener { config: CodeXPConfiguration, point: Point, caretHeight: Int, - labelWidth: Int + labelWidth: Int, ): Point { with(config.positionToDisplayGainedXP) { - val xOffset = when { - name.contains("LEFT") -> -labelWidth - name.contains("RIGHT") -> 0 - else -> -labelWidth / 2 - } + val xOffset = + when { + name.contains("LEFT") -> -labelWidth + name.contains("RIGHT") -> 0 + else -> -labelWidth / 2 + } point.translate((x * 4) + xOffset, y * (caretHeight / 2)) } return point @@ -181,7 +202,9 @@ object CodeXPUIManager : CodeXPEventListener, CodeXPListener { /** * Fading label class for displaying XP gain. */ - internal class FadingLabel(initialValue: Int) : JLabel(if (initialValue == 0) "0 xp" else "+$initialValue XP") { + internal class FadingLabel( + initialValue: Int, + ) : JLabel(if (initialValue == 0) "0 xp" else "+$initialValue XP") { private lateinit var timer: Timer /** @@ -201,14 +224,14 @@ object CodeXPUIManager : CodeXPEventListener, CodeXPListener { JBColor.foreground().red, JBColor.foreground().green, JBColor.foreground().blue, - newAlpha + newAlpha, ), Color( JBColor.foreground().red, JBColor.foreground().green, JBColor.foreground().blue, - newAlpha - ) + newAlpha, + ), ) } } @@ -250,8 +273,13 @@ object CodeXPUIManager : CodeXPEventListener, CodeXPListener { addMouseMotionListener(object : MouseMotionAdapter() {}) } - WindowManager.getInstance() - .getIdeFrame(ProjectManager.getInstance().openProjects.firstOrNull())?.component?.rootPane?.layeredPane?.let { + WindowManager + .getInstance() + .getIdeFrame(ProjectManager.getInstance().openProjects.firstOrNull()) + ?.component + ?.rootPane + ?.layeredPane + ?.let { ide = it ide.add(dialogArea, JLayeredPane.POPUP_LAYER, 0) } ?: run { diff --git a/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/models/CodeXPChallenge.kt b/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/models/CodeXPChallenge.kt index 1e554e0..244c846 100644 --- a/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/models/CodeXPChallenge.kt +++ b/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/models/CodeXPChallenge.kt @@ -1,7 +1,7 @@ package com.github.ilovegamecoding.intellijcodexp.models import com.github.ilovegamecoding.intellijcodexp.enums.Event -import java.util.* +import java.util.UUID /** * CodeXPChallenge class @@ -65,7 +65,7 @@ class CodeXPChallenge() { rewardXPIncrement: Long, progress: Long, goal: Long, - goalIncrement: Long + goalIncrement: Long, ) : this() { this.id = UUID.randomUUID().toString() this.event = event @@ -77,4 +77,4 @@ class CodeXPChallenge() { this.goal = goal this.goalIncrement = goalIncrement } -} \ No newline at end of file +} diff --git a/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/models/CodeXPChallengeFactory.kt b/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/models/CodeXPChallengeFactory.kt index d66a333..9721c0f 100644 --- a/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/models/CodeXPChallengeFactory.kt +++ b/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/models/CodeXPChallengeFactory.kt @@ -13,71 +13,105 @@ object CodeXPChallengeFactory { * * @return List of default challenges. */ - fun createEventDefaultChallenges(): List { - return listOf( + fun createEventDefaultChallenges(): List = + listOf( createChallenge( event = Event.TYPING, - name = "Typing Challenge", description = "Typing [goal] times.", - goal = 100, rewardXP = 100, rewardXPIncrement = 200 + name = "Typing Challenge", + description = "Typing [goal] times.", + goal = 100, + rewardXP = 100, + rewardXPIncrement = 200, ), createChallenge( event = Event.CUT, - name = "Cut Challenge", description = "Cut [goal] times.", - goal = 10, rewardXP = 100, rewardXPIncrement = 150 + name = "Cut Challenge", + description = "Cut [goal] times.", + goal = 10, + rewardXP = 100, + rewardXPIncrement = 150, ), createChallenge( event = Event.COPY, - name = "Copy Challenge", description = "Copy [goal] times.", - goal = 10, rewardXP = 100, rewardXPIncrement = 150 + name = "Copy Challenge", + description = "Copy [goal] times.", + goal = 10, + rewardXP = 100, + rewardXPIncrement = 150, ), createChallenge( event = Event.PASTE, - name = "Paste Challenge", description = "Paste [goal] times.", - goal = 10, rewardXP = 100, rewardXPIncrement = 150 + name = "Paste Challenge", + description = "Paste [goal] times.", + goal = 10, + rewardXP = 100, + rewardXPIncrement = 150, ), createChallenge( event = Event.BACKSPACE, - name = "Backspace Challenge", description = "Press the backspace key [goal] times.", - goal = 50, rewardXP = 100, rewardXPIncrement = 150 + name = "Backspace Challenge", + description = "Press the backspace key [goal] times.", + goal = 50, + rewardXP = 100, + rewardXPIncrement = 150, ), createChallenge( event = Event.TAB, name = "Tab Challenge", description = "Press the tab key [goal] times.", - goal = 50, rewardXP = 100, rewardXPIncrement = 150 + goal = 50, + rewardXP = 100, + rewardXPIncrement = 150, ), createChallenge( event = Event.ENTER, - name = "Enter Challenge", description = "Press the enter key [goal] times.", - goal = 10, rewardXP = 100, rewardXPIncrement = 200 + name = "Enter Challenge", + description = "Press the enter key [goal] times.", + goal = 10, + rewardXP = 100, + rewardXPIncrement = 200, ), createChallenge( event = Event.SAVE, - name = "Save Challenge", description = "Save [goal] times.", - goal = 10, rewardXP = 300, rewardXPIncrement = 400 + name = "Save Challenge", + description = "Save [goal] times.", + goal = 10, + rewardXP = 300, + rewardXPIncrement = 400, ), createChallenge( event = Event.BUILD, - name = "Build Challenge", description = "Build [goal] times.", - goal = 10, rewardXP = 150, rewardXPIncrement = 200 + name = "Build Challenge", + description = "Build [goal] times.", + goal = 10, + rewardXP = 150, + rewardXPIncrement = 200, ), createChallenge( event = Event.RUN, - name = "Run Challenge", description = "Run [goal] times.", - goal = 10, rewardXP = 200, rewardXPIncrement = 250 + name = "Run Challenge", + description = "Run [goal] times.", + goal = 10, + rewardXP = 200, + rewardXPIncrement = 250, ), createChallenge( event = Event.DEBUG, - name = "Debug Challenge", description = "Debug [goal] times.", - goal = 10, rewardXP = 300, rewardXPIncrement = 400 + name = "Debug Challenge", + description = "Debug [goal] times.", + goal = 10, + rewardXP = 300, + rewardXPIncrement = 400, ), createChallenge( event = Event.ACTION, - name = "Action Challenge", description = "Perform [goal] actions.", - goal = 20, rewardXP = 100, rewardXPIncrement = 120 - ) + name = "Action Challenge", + description = "Perform [goal] actions.", + goal = 20, + rewardXP = 100, + rewardXPIncrement = 120, + ), ) - } /** * Creates a challenge with the given parameters. @@ -96,9 +130,9 @@ object CodeXPChallengeFactory { description: String, goal: Long, rewardXP: Long, - rewardXPIncrement: Long - ): CodeXPChallenge { - return CodeXPChallenge( + rewardXPIncrement: Long, + ): CodeXPChallenge = + CodeXPChallenge( event = event, name = name, description = description, @@ -106,7 +140,6 @@ object CodeXPChallengeFactory { goal = goal, goalIncrement = goal, rewardXP = rewardXP, - rewardXPIncrement = rewardXPIncrement + rewardXPIncrement = rewardXPIncrement, ) - } -} \ No newline at end of file +} diff --git a/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/models/CodeXPConfiguration.kt b/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/models/CodeXPConfiguration.kt index 19b8a08..973251e 100644 --- a/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/models/CodeXPConfiguration.kt +++ b/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/models/CodeXPConfiguration.kt @@ -12,24 +12,20 @@ data class CodeXPConfiguration( * Notification type. */ var notificationType: String = "CodeXP Notification", - /** * Show a notification when the user levels up. */ var showLevelUpNotification: Boolean = true, - /** * Show a notification when the user completes a challenge. */ var showCompleteChallengeNotification: Boolean = true, - /** * Show a notification when the user gains XP. */ var showGainedXP: Boolean = true, - /** * Gained XP display position. */ - var positionToDisplayGainedXP: PositionToDisplayGainedXP = PositionToDisplayGainedXP.TOP_RIGHT -) \ No newline at end of file + var positionToDisplayGainedXP: PositionToDisplayGainedXP = PositionToDisplayGainedXP.TOP_RIGHT, +) diff --git a/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/models/CodeXPLevel.kt b/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/models/CodeXPLevel.kt index 69e2512..672372c 100644 --- a/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/models/CodeXPLevel.kt +++ b/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/models/CodeXPLevel.kt @@ -10,21 +10,18 @@ data class CodeXPLevel( * Current level. */ val level: Int, - /** * XP into the current level. */ val xpIntoCurrentLevel: Long, - /** * Progress to the next level. */ val progressPercentage: Int, - /** * XP needed to reach the next level. */ - val totalXPForNextLevel: Long + val totalXPForNextLevel: Long, ) { companion object { /** @@ -50,4 +47,4 @@ data class CodeXPLevel( return CodeXPLevel(level, xpGainedInCurrentLevel, progressPercentage, xpRequiredForNextLevel.toLong()) } } -} \ No newline at end of file +} diff --git a/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/models/CodeXPState.kt b/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/models/CodeXPState.kt index 50400d5..acd2031 100644 --- a/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/models/CodeXPState.kt +++ b/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/models/CodeXPState.kt @@ -12,46 +12,37 @@ data class CodeXPState( * Whether the plugin has been executed before. */ var hasExecuted: Boolean = false, - /** * User's nickname. */ var nickname: String = "", - /** * User's total XP. */ var xp: Long = 0, - /** * Save the number of times an event has occurred based on the [Event] enum. */ var eventCounts: MutableMap = mutableMapOf(), - /** * Challenges that not yet been completed. */ var challenges: MutableMap = mutableMapOf(), - /** * Challenges that have been completed. */ var completedChallenges: MutableList = mutableListOf(), - /** * Show completed challenges in the dashboard. */ var showCompletedChallenges: Boolean = true, - /** * The configuration of the CodeXP plugin */ - var codeXPConfiguration: CodeXPConfiguration = CodeXPConfiguration() + var codeXPConfiguration: CodeXPConfiguration = CodeXPConfiguration(), ) { /** * Get the number of times an event has occurred. */ - fun getEventCount(event: Event): Long { - return eventCounts[event] ?: 0 - } -} \ No newline at end of file + fun getEventCount(event: Event): Long = eventCounts[event] ?: 0 +} diff --git a/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/services/CodeXPService.kt b/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/services/CodeXPService.kt index fa8e191..17c5f76 100644 --- a/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/services/CodeXPService.kt +++ b/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/services/CodeXPService.kt @@ -27,9 +27,11 @@ import com.intellij.util.messages.MessageBusConnection @Service(Service.Level.APP) @State( name = "com.github.ilovegamecoding.intellijcodexp.services.CodeXP", - storages = [Storage("CodeXP.xml")] + storages = [Storage("CodeXP.xml")], ) -class CodeXPService : PersistentStateComponent, CodeXPEventListener { +class CodeXPService : + PersistentStateComponent, + CodeXPEventListener { /** * The state of the CodeXP plugin */ @@ -54,9 +56,7 @@ class CodeXPService : PersistentStateComponent, CodeXPEventListener connection.subscribe(CodeXPEventListener.CODEXP_EVENT, this) } - override fun getState(): CodeXPState { - return codeXPState - } + override fun getState(): CodeXPState = codeXPState override fun noStateLoaded() { super.noStateLoaded() @@ -68,7 +68,10 @@ class CodeXPService : PersistentStateComponent, CodeXPEventListener initialize { } } - override fun eventOccurred(event: Event, dataContext: DataContext?) { + override fun eventOccurred( + event: Event, + dataContext: DataContext?, + ) { increaseEventCount(event) increaseChallengeProgress(event) } @@ -112,7 +115,10 @@ class CodeXPService : PersistentStateComponent, CodeXPEventListener * @param event The event to increase the count for. * @param incrementValue The amount to increase the count by. */ - private fun increaseEventCount(event: Event, incrementValue: Long = 1) { + private fun increaseEventCount( + event: Event, + incrementValue: Long = 1, + ) { codeXPState.eventCounts[event] = codeXPState.eventCounts.getOrDefault(event, 0) + incrementValue increaseXP(event.xpValue) } @@ -130,15 +136,17 @@ class CodeXPService : PersistentStateComponent, CodeXPEventListener if (beforeLevelInfo.level != currentLevelInfo.level && beforeLevelInfo.level != 0 && codeXPState.codeXPConfiguration.showLevelUpNotification) { if (codeXPState.codeXPConfiguration.showLevelUpNotification) { when (codeXPState.codeXPConfiguration.notificationType) { - "IntelliJ Notification" -> + "IntelliJ Notification" -> { CodeXPNotificationManager.notifyLevelUp( codeXPState.nickname, currentLevelInfo.level, - currentLevelInfo.totalXPForNextLevel + currentLevelInfo.totalXPForNextLevel, ) + } - "CodeXP Notification" -> + "CodeXP Notification" -> { messageBus.syncPublisher(CodeXPListener.CODEXP).levelUp(currentLevelInfo) + } } } } @@ -161,15 +169,19 @@ class CodeXPService : PersistentStateComponent, CodeXPEventListener if (codeXPState.codeXPConfiguration.showCompleteChallengeNotification) { when (codeXPState.codeXPConfiguration.notificationType) { - "IntelliJ Notification" -> CodeXPNotificationManager.notifyChallengeComplete( - challenge - ) + "IntelliJ Notification" -> { + CodeXPNotificationManager.notifyChallengeComplete( + challenge, + ) + } - "CodeXP Notification" -> + "CodeXP Notification" -> { messageBus.syncPublisher(CodeXPListener.CODEXP).challengeCompleted(event, challenge) + } } } - messageBus.syncPublisher(CodeXPListener.CODEXP) + messageBus + .syncPublisher(CodeXPListener.CODEXP) .challengeUpdated(event, challenge, state.challenges[event]) } else { messageBus.syncPublisher(CodeXPListener.CODEXP).challengeUpdated(event, challenge, null) @@ -183,11 +195,15 @@ class CodeXPService : PersistentStateComponent, CodeXPEventListener * @param completedChallenge The completed challenge. * @param event The type of the completed challenge. */ - private fun replaceChallengeWithNew(completedChallenge: CodeXPChallenge, event: Event) { + private fun replaceChallengeWithNew( + completedChallenge: CodeXPChallenge, + event: Event, + ) { codeXPState.completedChallenges.add(completedChallenge) - codeXPState.challenges[event] = createNextChallenge( - completedChallenge - ) + codeXPState.challenges[event] = + createNextChallenge( + completedChallenge, + ) } /** @@ -195,8 +211,8 @@ class CodeXPService : PersistentStateComponent, CodeXPEventListener * * @param completedChallenge The completed challenge. */ - private fun createNextChallenge(completedChallenge: CodeXPChallenge): CodeXPChallenge { - return CodeXPChallenge( + private fun createNextChallenge(completedChallenge: CodeXPChallenge): CodeXPChallenge = + CodeXPChallenge( event = completedChallenge.event, name = completedChallenge.name, description = completedChallenge.description, @@ -204,7 +220,6 @@ class CodeXPService : PersistentStateComponent, CodeXPEventListener rewardXPIncrement = completedChallenge.rewardXPIncrement, progress = 0, goal = completedChallenge.goal + completedChallenge.goalIncrement, - goalIncrement = completedChallenge.goalIncrement + goalIncrement = completedChallenge.goalIncrement, ) - } -} \ No newline at end of file +} diff --git a/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/toolWindow/CodeXPConfigurable.kt b/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/toolWindow/CodeXPConfigurable.kt index 441bc11..c858ca1 100644 --- a/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/toolWindow/CodeXPConfigurable.kt +++ b/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/toolWindow/CodeXPConfigurable.kt @@ -22,34 +22,40 @@ class CodeXPConfigurable : Configurable { * CodeXP service. */ private val config = - ApplicationManager.getApplication().getService(CodeXPService::class.java).state.codeXPConfiguration + ApplicationManager + .getApplication() + .getService(CodeXPService::class.java) + .state.codeXPConfiguration override fun createComponent(): JComponent? { // Create the configuration form and set the values to the current configuration. - codeXPConfigurationForm = CodeXPConfigurationForm().apply { - cbNotificationType.addItem("IntelliJ Notification") - cbNotificationType.addItem("CodeXP Notification") - cbNotificationType.selectedItem = config.notificationType - if(cbNotificationType.selectedItem == "IntelliJ Notification") { - lblTypeDescription.text = - "Default notification will appear in the bottom-right of the IDE and IDE notification tool window." - } else { - lblTypeDescription.text = "Customized notification will appear in the top-center of the IDE." + codeXPConfigurationForm = + CodeXPConfigurationForm().apply { + cbNotificationType.addItem("IntelliJ Notification") + cbNotificationType.addItem("CodeXP Notification") + cbNotificationType.selectedItem = config.notificationType + if (cbNotificationType.selectedItem == "IntelliJ Notification") { + lblTypeDescription.text = + "Default notification will appear in the bottom-right of the IDE and IDE notification tool window." + } else { + lblTypeDescription.text = "Customized notification will appear in the top-center of the IDE." + } + cbShowLevelUpNotification.isSelected = config.showLevelUpNotification + cbShowCompleteChallengeNotification.isSelected = config.showCompleteChallengeNotification + cbShowGainedXP.isSelected = config.showGainedXP + PositionToDisplayGainedXP + .values() + .map { it.name } + .forEach { cbPositionToDisplayGainedXP.addItem(it) } + cbPositionToDisplayGainedXP.isEnabled = config.showGainedXP + cbPositionToDisplayGainedXP.selectedItem = config.positionToDisplayGainedXP.name } - cbShowLevelUpNotification.isSelected = config.showLevelUpNotification - cbShowCompleteChallengeNotification.isSelected = config.showCompleteChallengeNotification - cbShowGainedXP.isSelected = config.showGainedXP - PositionToDisplayGainedXP.values().map { it.name } - .forEach { cbPositionToDisplayGainedXP.addItem(it) } - cbPositionToDisplayGainedXP.isEnabled = config.showGainedXP - cbPositionToDisplayGainedXP.selectedItem = config.positionToDisplayGainedXP.name - } return codeXPConfigurationForm.pMain } - override fun isModified(): Boolean { - return with(codeXPConfigurationForm) { - if(cbNotificationType.selectedItem == "IntelliJ Notification") { + override fun isModified(): Boolean = + with(codeXPConfigurationForm) { + if (cbNotificationType.selectedItem == "IntelliJ Notification") { lblTypeDescription.text = "Default notification will appear in the bottom-right of the IDE and IDE notification tool window." } else { @@ -57,12 +63,11 @@ class CodeXPConfigurable : Configurable { } cbPositionToDisplayGainedXP.isEnabled = cbShowGainedXP.isSelected cbNotificationType.selectedItem != config.notificationType || - cbShowLevelUpNotification.isSelected != config.showLevelUpNotification || - cbShowCompleteChallengeNotification.isSelected != config.showCompleteChallengeNotification || - cbShowGainedXP.isSelected != config.showGainedXP || - cbPositionToDisplayGainedXP.selectedItem != config.positionToDisplayGainedXP.name + cbShowLevelUpNotification.isSelected != config.showLevelUpNotification || + cbShowCompleteChallengeNotification.isSelected != config.showCompleteChallengeNotification || + cbShowGainedXP.isSelected != config.showGainedXP || + cbPositionToDisplayGainedXP.selectedItem != config.positionToDisplayGainedXP.name } - } override fun apply() { with(codeXPConfigurationForm) { @@ -75,7 +80,5 @@ class CodeXPConfigurable : Configurable { } } - override fun getDisplayName(): String { - return "CodeXP" - } -} \ No newline at end of file + override fun getDisplayName(): String = "CodeXP" +} diff --git a/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/toolWindow/CodeXPToolWindowFactory.kt b/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/toolWindow/CodeXPToolWindowFactory.kt index 3f3a2a0..a7af766 100644 --- a/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/toolWindow/CodeXPToolWindowFactory.kt +++ b/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/toolWindow/CodeXPToolWindowFactory.kt @@ -27,7 +27,6 @@ import javax.swing.JPanel import javax.swing.event.DocumentEvent import javax.swing.event.DocumentListener - /** * CodeXPToolWindowFactory class * @@ -44,7 +43,10 @@ class CodeXPToolWindowFactory : ToolWindowFactory { */ private lateinit var codeXPDashboardForm: CodeXPDashboardForm - override fun createToolWindowContent(project: Project, toolWindow: ToolWindow) { + override fun createToolWindowContent( + project: Project, + toolWindow: ToolWindow, + ) { // Get the CodeXP service codeXPService = ApplicationManager.getApplication().getService(CodeXPService::class.java) @@ -83,19 +85,21 @@ class CodeXPToolWindowFactory : ToolWindowFactory { private fun initializeNickname() { // Set nickname and listen changes from the text field codeXPDashboardForm.tfNickname.text = codeXPService.state.nickname - codeXPDashboardForm.tfNickname.document.addDocumentListener(object : DocumentListener { - override fun insertUpdate(e: DocumentEvent) { - updateNickname() - } + codeXPDashboardForm.tfNickname.document.addDocumentListener( + object : DocumentListener { + override fun insertUpdate(e: DocumentEvent) { + updateNickname() + } - override fun removeUpdate(e: DocumentEvent) { - updateNickname() - } + override fun removeUpdate(e: DocumentEvent) { + updateNickname() + } - override fun changedUpdate(e: DocumentEvent) { - updateNickname() - } - }) + override fun changedUpdate(e: DocumentEvent) { + updateNickname() + } + }, + ) } /** @@ -103,13 +107,14 @@ class CodeXPToolWindowFactory : ToolWindowFactory { */ private fun initializeEventStatisticsAndChallenges( eventStaticForms: HashMap, - challengeForms: HashMap + challengeForms: HashMap, ) { val gridBagConstraints = GridBagConstraints() gridBagConstraints.weightx = 1.0 gridBagConstraints.fill = GridBagConstraints.HORIZONTAL - Event.values().forEachIndexed { index, event -> // Add ui for each event + Event.values().forEachIndexed { index, event -> + // Add ui for each event if (event != Event.NONE) { // Ignore the NONE event type // Initialize event statistics gridBagConstraints.gridy = index @@ -153,7 +158,10 @@ class CodeXPToolWindowFactory : ToolWindowFactory { gridBagConstraints.fill = GridBagConstraints.HORIZONTAL codeXPDashboardForm.lblCompletedChallengesCount.text = - StringUtil.numberToStringWithCommas(codeXPService.state.completedChallenges.size.toLong()) + StringUtil.numberToStringWithCommas( + codeXPService.state.completedChallenges.size + .toLong(), + ) if (codeXPService.state.showCompletedChallenges) { updateCompletedChallenges() @@ -180,57 +188,76 @@ class CodeXPToolWindowFactory : ToolWindowFactory { */ private fun initializeConnection( eventStaticForms: HashMap, - challengeForms: HashMap + challengeForms: HashMap, ) { val gridBagConstraints = GridBagConstraints() gridBagConstraints.weightx = 1.0 gridBagConstraints.fill = GridBagConstraints.HORIZONTAL // Update the dashboard when events occur - ApplicationManager.getApplication().messageBus.connect() - .subscribe(CodeXPEventListener.CODEXP_EVENT, object : CodeXPEventListener { - override fun eventOccurred(event: Event, dataContext: DataContext?) { - (eventStaticForms[event]!!.getComponent(2) as JLabel).text = - StringUtil.numberToStringWithCommas(codeXPService.state.getEventCount(event)) - } - }) - - ApplicationManager.getApplication().messageBus.connect() - .subscribe(CodeXPListener.CODEXP, object : CodeXPListener { - override fun xpUpdated(levelInfo: CodeXPLevel) { - updateXPInfo(levelInfo) - } + ApplicationManager + .getApplication() + .messageBus + .connect() + .subscribe( + CodeXPEventListener.CODEXP_EVENT, + object : CodeXPEventListener { + override fun eventOccurred( + event: Event, + dataContext: DataContext?, + ) { + (eventStaticForms[event]!!.getComponent(2) as JLabel).text = + StringUtil.numberToStringWithCommas(codeXPService.state.getEventCount(event)) + } + }, + ) - override fun levelUp(levelInfo: CodeXPLevel) { + ApplicationManager + .getApplication() + .messageBus + .connect() + .subscribe( + CodeXPListener.CODEXP, + object : CodeXPListener { + override fun xpUpdated(levelInfo: CodeXPLevel) { + updateXPInfo(levelInfo) + } - } + override fun levelUp(levelInfo: CodeXPLevel) { + } - override fun challengeUpdated( - event: Event, - challenge: CodeXPChallenge, - newChallenge: CodeXPChallenge? - ) { - if (newChallenge != null) { - codeXPDashboardForm.lblCompletedChallengesCount.text = - StringUtil.numberToStringWithCommas(codeXPService.state.completedChallenges.size.toLong()) - - if (codeXPService.state.showCompletedChallenges) { - gridBagConstraints.gridy = codeXPDashboardForm.pCompletedChallenges.componentCount - codeXPDashboardForm.pCompletedChallenges.add( - createOrUpdateChallengeForm(challenge).pChallenge, - gridBagConstraints - ) + override fun challengeUpdated( + event: Event, + challenge: CodeXPChallenge, + newChallenge: CodeXPChallenge?, + ) { + if (newChallenge != null) { + codeXPDashboardForm.lblCompletedChallengesCount.text = + StringUtil.numberToStringWithCommas( + codeXPService.state.completedChallenges.size + .toLong(), + ) + + if (codeXPService.state.showCompletedChallenges) { + gridBagConstraints.gridy = codeXPDashboardForm.pCompletedChallenges.componentCount + codeXPDashboardForm.pCompletedChallenges.add( + createOrUpdateChallengeForm(challenge).pChallenge, + gridBagConstraints, + ) + } + createOrUpdateChallengeForm(newChallenge, challengeForms[event]!!) + } else { + updateChallengeProgress(challenge, challengeForms[event]!!) } - createOrUpdateChallengeForm(newChallenge, challengeForms[event]!!) - } else { - updateChallengeProgress(challenge, challengeForms[event]!!) } - } - override fun challengeCompleted(event: Event, challenge: CodeXPChallenge) { - - } - }) + override fun challengeCompleted( + event: Event, + challenge: CodeXPChallenge, + ) { + } + }, + ) } /** @@ -268,7 +295,7 @@ class CodeXPToolWindowFactory : ToolWindowFactory { gridBagConstraints.gridy = codeXPDashboardForm.pCompletedChallenges.componentCount codeXPDashboardForm.pCompletedChallenges.add( createOrUpdateChallengeForm(completedChallenge).pChallenge, - gridBagConstraints + gridBagConstraints, ) } } @@ -279,7 +306,10 @@ class CodeXPToolWindowFactory : ToolWindowFactory { * @param challenge Challenge. * @param challengeForm Challenge form. */ - private fun updateChallengeProgress(challenge: CodeXPChallenge, challengeForm: CodeXPChallengeForm) { + private fun updateChallengeProgress( + challenge: CodeXPChallenge, + challengeForm: CodeXPChallengeForm, + ) { val progressPercentage = ((challenge.progress.toDouble() / challenge.goal) * 100).toInt() challengeForm.lblChallengeProgress.text = progressPercentage.toString() challengeForm.pbChallengeProgress.value = progressPercentage @@ -294,7 +324,7 @@ class CodeXPToolWindowFactory : ToolWindowFactory { */ private fun createOrUpdateChallengeForm( challenge: CodeXPChallenge, - challengeForm: CodeXPChallengeForm? = null + challengeForm: CodeXPChallengeForm? = null, ): CodeXPChallengeForm { val form = challengeForm ?: CodeXPChallengeForm() diff --git a/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/utils/StringUtil.kt b/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/utils/StringUtil.kt index 34c07e6..48631a2 100644 --- a/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/utils/StringUtil.kt +++ b/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/utils/StringUtil.kt @@ -7,7 +7,11 @@ object StringUtil { /** * Convert a number to a string with commas. */ - fun numberToStringWithCommas(number: Long): String { - return number.toString().reversed().chunked(3).joinToString(",").reversed() - } -} \ No newline at end of file + fun numberToStringWithCommas(number: Long): String = + number + .toString() + .reversed() + .chunked(3) + .joinToString(",") + .reversed() +} diff --git a/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/views/CodeXPDialog.kt b/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/views/CodeXPDialog.kt index af7255d..82bcccf 100644 --- a/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/views/CodeXPDialog.kt +++ b/src/main/kotlin/com/github/ilovegamecoding/intellijcodexp/views/CodeXPDialog.kt @@ -2,8 +2,18 @@ package com.github.ilovegamecoding.intellijcodexp.views import com.intellij.ui.JBColor import com.intellij.util.ui.JBUI -import java.awt.* -import javax.swing.* +import java.awt.BorderLayout +import java.awt.Color +import java.awt.Dimension +import java.awt.Graphics +import java.awt.Graphics2D +import java.awt.RenderingHints +import javax.swing.BorderFactory +import javax.swing.BoxLayout +import javax.swing.JLabel +import javax.swing.JPanel +import javax.swing.SwingUtilities +import javax.swing.Timer /** * CodeXPDialog class @@ -15,12 +25,10 @@ class CodeXPDialog( * Dialog title. */ private var title: String = "", - /** * Dialog main description. */ private var mainDescription: String = "", - /** * Dialog sub description. */ @@ -57,64 +65,70 @@ class CodeXPDialog( private val fadeStep = 192.0 * stepMillis / fadeDuration init { - val lblTitle = JLabel().apply { - text = title - foreground = JBColor(Color(255, 255, 255, 0), Color(255, 255, 255, 0)) - font = font.deriveFont(20f) - horizontalAlignment = JLabel.CENTER - verticalAlignment = JLabel.CENTER - maximumSize = Dimension(400, Int.MAX_VALUE) - } + val lblTitle = + JLabel().apply { + text = title + foreground = JBColor(Color(255, 255, 255, 0), Color(255, 255, 255, 0)) + font = font.deriveFont(20f) + horizontalAlignment = JLabel.CENTER + verticalAlignment = JLabel.CENTER + maximumSize = Dimension(400, Int.MAX_VALUE) + } - val lblMainDescription = JLabel().apply { - text = mainDescription - foreground = JBColor(Color(255, 255, 255, 0), Color(255, 255, 255, 0)) - font = font.deriveFont(14f) - maximumSize = Dimension(400, Int.MAX_VALUE) - } + val lblMainDescription = + JLabel().apply { + text = mainDescription + foreground = JBColor(Color(255, 255, 255, 0), Color(255, 255, 255, 0)) + font = font.deriveFont(14f) + maximumSize = Dimension(400, Int.MAX_VALUE) + } - val lblSubDescription = JLabel().apply { - text = subDescription - foreground = JBColor(Color(255, 255, 255, 0), Color(255, 255, 255, 0)) - font = font.deriveFont(14f) - maximumSize = Dimension(400, Int.MAX_VALUE) - } + val lblSubDescription = + JLabel().apply { + text = subDescription + foreground = JBColor(Color(255, 255, 255, 0), Color(255, 255, 255, 0)) + font = font.deriveFont(14f) + maximumSize = Dimension(400, Int.MAX_VALUE) + } - val contentHeight = lblTitle.preferredSize.height + lblMainDescription.preferredSize.height + + val contentHeight = + lblTitle.preferredSize.height + lblMainDescription.preferredSize.height + lblSubDescription.preferredSize.height + (2 * 20) + (2 * 10) - content = object : JPanel() { - override fun paintComponent(g: Graphics) { - val g2d = g.create() as Graphics2D - g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON) - g2d.color = background - g2d.fillRoundRect(0, 0, width, height, 16, 16) - g2d.dispose() + content = + object : JPanel() { + override fun paintComponent(g: Graphics) { + val g2d = g.create() as Graphics2D + g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON) + g2d.color = background + g2d.fillRoundRect(0, 0, width, height, 16, 16) + g2d.dispose() + } + }.apply { + preferredSize = JBUI.size(440, contentHeight) + minimumSize = JBUI.size(440, contentHeight) + maximumSize = JBUI.size(440, contentHeight) + layout = BoxLayout(this, BoxLayout.Y_AXIS) + background = JBColor(Color(0, 0, 0, 0), Color(0, 0, 0, 0)) + border = BorderFactory.createEmptyBorder(20, 20, 20, 20) + + add(lblTitle) + add(lblMainDescription) + add(lblSubDescription) } - }.apply { - preferredSize = JBUI.size(440, contentHeight) - minimumSize = JBUI.size(440, contentHeight) - maximumSize = JBUI.size(440, contentHeight) - layout = BoxLayout(this, BoxLayout.Y_AXIS) - background = JBColor(Color(0, 0, 0, 0), Color(0, 0, 0, 0)) - border = BorderFactory.createEmptyBorder(20, 20, 20, 20) - - add(lblTitle) - add(lblMainDescription) - add(lblSubDescription) - } - with(frame) { layout = BoxLayout(this, BoxLayout.Y_AXIS) isOpaque = false add(content) - add(JPanel().apply { - preferredSize = JBUI.size(440, 20) - layout = BorderLayout() - background = JBColor(Color(255, 255, 255, 0), Color(255, 255, 255, 0)) - isOpaque = false - }) + add( + JPanel().apply { + preferredSize = JBUI.size(440, 20) + layout = BorderLayout() + background = JBColor(Color(255, 255, 255, 0), Color(255, 255, 255, 0)) + isOpaque = false + }, + ) } } @@ -126,9 +140,7 @@ class CodeXPDialog( title: String = "", mainDescription: String = "", subDescription: String = "", - ): CodeXPDialog { - return CodeXPDialog(title, mainDescription, subDescription) - } + ): CodeXPDialog = CodeXPDialog(title, mainDescription, subDescription) } /** @@ -189,4 +201,4 @@ class CodeXPDialog( }.start() } } -} \ No newline at end of file +} diff --git a/src/test/kotlin/com/github/ilovegamecoding/intellijcodexp/CodeXPUtilTest.kt b/src/test/kotlin/com/github/ilovegamecoding/intellijcodexp/CodeXPUtilTest.kt index 58493ba..ed322f1 100644 --- a/src/test/kotlin/com/github/ilovegamecoding/intellijcodexp/CodeXPUtilTest.kt +++ b/src/test/kotlin/com/github/ilovegamecoding/intellijcodexp/CodeXPUtilTest.kt @@ -15,4 +15,4 @@ class CodeXPUtilTest : BasePlatformTestCase() { assertEquals("0", StringUtil.numberToStringWithCommas(number2)) assertEquals("12,345", StringUtil.numberToStringWithCommas(number3)) } -} \ No newline at end of file +}