Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions src/main/kotlin/com/github/patou/gitmoji/GitMojiConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
import javax.swing.JComponent
import javax.swing.JLabel
import javax.swing.JPanel
import javax.swing.JTextArea

class GitMojiConfig(private val project: Project) : SearchableConfigurable {
private val mainPanel: JPanel
private val useUnicode = JCheckBox(GitmojiBundle.message("config.useUnicode"))
private val displayEmoji =
JCheckBox(GitmojiBundle.message("config.displayEmoji"))
private val displayEmoji = JCheckBox(GitmojiBundle.message("config.displayEmoji"))
private val insertInCursorPosition = JCheckBox(GitmojiBundle.message("config.insertInCursorPosition"))
private val includeGitMojiDescription = JCheckBox(GitmojiBundle.message("config.includeGitMojiDescription"))
private val previewGitCommitMessage = JTextArea(2, 80)
private var useUnicodeConfig: Boolean = false
private var displayEmojiConfig: String = "emoji"
private var insertInCursorPositionConfig: Boolean = false
Expand Down Expand Up @@ -49,6 +50,9 @@

init {
val flow = GridLayout(20, 2)
useUnicode.addChangeListener { previewCommit() }
insertInCursorPosition.addChangeListener { previewCommit() }
includeGitMojiDescription.addChangeListener { previewCommit() }
Comment on lines +53 to +55

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider extracting this to a separate function to avoid duplication with the apply and reset methods. This will improve maintainability and readability.

Suggested change
useUnicode.addChangeListener { previewCommit() }
insertInCursorPosition.addChangeListener { previewCommit() }
includeGitMojiDescription.addChangeListener { previewCommit() }
useUnicode.addChangeListener { updatePreviewCommit() }
insertInCursorPosition.addChangeListener { updatePreviewCommit() }
includeGitMojiDescription.addChangeListener { updatePreviewCommit() }

mainPanel = JPanel(flow)
mainPanel.add(displayEmoji, null)
mainPanel.add(useUnicode, null)
Expand All @@ -62,6 +66,9 @@
languageJPanel.add(JLabel(GitmojiBundle.message("config.language")))
languageJPanel.add(languages, null)
mainPanel.add(languageJPanel)
previewGitCommitMessage.isEditable = false
mainPanel.add(JLabel("Preview"))
mainPanel.add(previewGitCommitMessage)
}

override fun apply() {
Expand All @@ -85,8 +92,8 @@
projectInstance.setValue(CONFIG_AFTER_UNICODE, textAfterUnicodeConfig)
instance.setValue(CONFIG_LANGUAGE, languagesConfig)
GitmojiLocale.loadTranslations()
previewCommit()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Call updatePreviewCommit here instead of previewCommit to use the extracted function.

Suggested change
previewCommit()
updatePreviewCommit()

}

override fun reset() {
val propertiesComponent = PropertiesComponent.getInstance(project)
val instance = PropertiesComponent.getInstance()
Expand All @@ -107,6 +114,31 @@
else -> textAfterUnicodeOptions.indexOf(textAfterUnicodeConfig)
}
languages.selectedIndex = languageOptions.indexOf(languagesConfig)
previewCommit()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Call updatePreviewCommit here instead of previewCommit to use the extracted function.

Suggested change
previewCommit()
updatePreviewCommit()

}

private fun previewCommit():Boolean {
var message = ""
if (insertInCursorPositionConfig) {
message += "Commit message|"
}
if (useUnicodeConfig) {

Check notice on line 125 in src/main/kotlin/com/github/patou/gitmoji/GitMojiConfig.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Return or assignment can be lifted out

'Assignment' can be lifted out of 'if'
message += ""
}
else {
message += ":sparkles:"
}
message += textAfterUnicodeConfig
val selectionStart = message.length
if (includeGitMojiDescriptionConfig) {
message += "Introduce new features."
}
else if (!insertInCursorPositionConfig) {
message += "Commit message"
}
previewGitCommitMessage.text = message
previewGitCommitMessage.select(selectionStart, message.length)
return true
}
Comment on lines +120 to 142

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This logic is used to construct the preview commit message. Consider using a StringBuilder for better readability and performance. Also, directly use the values from the UI components (e.g., useUnicode.isSelected) instead of the config variables to ensure the preview reflects the current UI state. Finally, consider renaming this function to updatePreviewCommitText to better reflect what it does.

private fun updatePreviewCommitText(): Boolean {
    val messageBuilder = StringBuilder()

    if (insertInCursorPosition.isSelected) {
        messageBuilder.append("Commit message|")
    }

    if (useUnicode.isSelected) {
        messageBuilder.append("")
    } else {
        messageBuilder.append(":sparkles:")
    }

    messageBuilder.append(textAfterUnicode.selectedItem ?: "")
    val selectionStart = messageBuilder.length

    if (includeGitMojiDescription.isSelected) {
        messageBuilder.append("Introduce new features.")
    } else if (!insertInCursorPosition.isSelected) {
        messageBuilder.append("Commit message")
    }

    val message = messageBuilder.toString()
    previewGitCommitMessage.text = message
    previewGitCommitMessage.select(selectionStart, message.length)
    return true
}


override fun createComponent(): JComponent = mainPanel
Expand Down
Loading