@@ -9,18 +9,25 @@ import com.intellij.openapi.options.SearchableConfigurable
99import com.intellij.openapi.project.Project
1010import com.intellij.openapi.ui.ComboBox
1111import com.intellij.openapi.util.Comparing
12- import java.awt.Color
12+ import com.intellij.ui.JBColor
13+ import com.intellij.ui.SeparatorFactory
14+ import com.intellij.util.ui.JBImageIcon
1315import java.awt.Cursor
1416import java.awt.Desktop
1517import java.awt.FlowLayout
1618import java.awt.GridLayout
1719import java.awt.event.MouseAdapter
1820import java.awt.event.MouseEvent
1921import java.net.URI
22+ import java.util.Locale
23+ import javax.imageio.ImageIO
24+ import javax.swing.BorderFactory
25+ import javax.swing.BoxLayout
2026import javax.swing.JCheckBox
2127import javax.swing.JComponent
2228import javax.swing.JLabel
2329import javax.swing.JPanel
30+ import javax.swing.JTextArea
2431import javax.swing.JTextField
2532
2633class GitMojiConfig (private val project : Project ) : SearchableConfigurable {
@@ -42,6 +49,7 @@ class GitMojiConfig(private val project: Project) : SearchableConfigurable {
4249 private val languages = ComboBox (languageOptions)
4350 private var textAfterUnicodeConfig: String = " "
4451 private var languagesConfig: String = " auto"
52+ private val previewArea = JTextArea (2 , 30 )
4553
4654 private val gitmojiSourceField = ComboBox (GitmojiSourceType .OPTIONS )
4755 private val gitmojiJsonUrlField = JTextField (40 )
@@ -81,7 +89,7 @@ class GitMojiConfig(private val project: Project) : SearchableConfigurable {
8189 val flow = GridLayout (20 , 2 )
8290 mainPanel = JPanel (flow)
8391 mainPanel.add(useProjectSettings, null )
84- mainPanel.add(JPanel ( ), null ) // empty cell
92+ mainPanel.add(SeparatorFactory .createSeparator( GitmojiBundle .message( " config.settings " ), null ), null )
8593 mainPanel.add(displayEmoji, null )
8694 mainPanel.add(useUnicode, null )
8795 mainPanel.add(insertInCursorPosition, null )
@@ -103,7 +111,7 @@ class GitMojiConfig(private val project: Project) : SearchableConfigurable {
103111
104112 sourceTooltipLabel.text = gitmojiSourceConfig.tooltipText
105113 sourceTooltipLabel.font = sourceTooltipLabel.font.deriveFont((sourceTooltipLabel.font.size - 2 ).toFloat())
106- sourceTooltipLabel.foreground = Color ( 120 , 120 , 120 )
114+ sourceTooltipLabel.foreground = JBColor . GRAY
107115 sourceTooltipLabel.toolTipText = gitmojiSourceConfig.tooltipUrl
108116 sourceTooltipLabel.cursor = Cursor .getPredefinedCursor(Cursor .HAND_CURSOR )
109117 sourceTooltipLabel.addMouseListener(object : MouseAdapter () {
@@ -124,7 +132,11 @@ class GitMojiConfig(private val project: Project) : SearchableConfigurable {
124132
125133 gitmojiSourceField.addItemListener {
126134 val selectedId = getCurrentGitmojiSourceId()
127- val type = GitmojiSourceTypeMapper .fromId(selectedId, gitmojiJsonUrlField.text.trim(), localizationUrlField.text.trim())
135+ val type = GitmojiSourceTypeMapper .fromId(
136+ selectedId,
137+ gitmojiJsonUrlField.text.trim(),
138+ localizationUrlField.text.trim()
139+ )
128140 setGitmojiSourceFieldsVisibility(type)
129141 updateSourceTooltip(type)
130142 }
@@ -140,6 +152,87 @@ class GitMojiConfig(private val project: Project) : SearchableConfigurable {
140152 localizationPanel.add(JLabel (GitmojiBundle .message(" config.source.localizationUrl" )))
141153 localizationPanel.add(localizationUrlField, null )
142154 mainPanel.add(localizationPanel)
155+
156+ mainPanel.add(SeparatorFactory .createSeparator(GitmojiBundle .message(" config.preview" ), null ), null )
157+
158+ val previewPanel = JPanel ()
159+ previewPanel.layout = BoxLayout (previewPanel, BoxLayout .Y_AXIS )
160+ val iconUrl = javaClass.getResource(" /icons/pluginIcon.png" )
161+ if (iconUrl != null ) {
162+ val image = ImageIO .read(iconUrl)
163+ val imageIcon = JBImageIcon (image)
164+ val imageLabel = JLabel (imageIcon)
165+ val iconPanel = JPanel (FlowLayout (FlowLayout .LEFT , 0 , 0 ))
166+ iconPanel.add(imageLabel)
167+ previewPanel.add(iconPanel)
168+ }
169+
170+ previewArea.isEditable = false
171+ previewArea.lineWrap = true
172+ previewArea.wrapStyleWord = true
173+ previewArea.border = BorderFactory .createLineBorder(JBColor .border())
174+ previewPanel.add(previewArea)
175+
176+ mainPanel.add(previewPanel, null )
177+
178+ useUnicode.addActionListener { updatePreview() }
179+ insertInCursorPosition.addActionListener { updatePreview() }
180+ includeGitMojiDescription.addActionListener { updatePreview() }
181+ textAfterUnicode.addActionListener { updatePreview() }
182+ languages.addActionListener { updatePreview() }
183+ }
184+
185+ private fun updatePreview () {
186+ val commitMessage = getTranslatedString(" Commit message" , languages.selectedItem as ? String ? : " auto" )
187+ val newFeaturesMessage = getTranslatedString(" Introduce new features." , languages.selectedItem as ? String ? : " auto" )
188+
189+ val messageBuilder = StringBuilder ()
190+ if (insertInCursorPosition.isSelected) {
191+ messageBuilder.append(" $commitMessage |" )
192+ }
193+ if (useUnicode.isSelected) {
194+ messageBuilder.append(" ✨" )
195+ } else {
196+ messageBuilder.append(" :sparkles:" )
197+ }
198+ val separator = when (textAfterUnicode.selectedIndex) {
199+ 0 -> " "
200+ 1 -> " "
201+ else -> textAfterUnicode.selectedItem as ? String ? : " "
202+ }
203+ messageBuilder.append(separator)
204+ val selectionStart = messageBuilder.length
205+
206+ if (includeGitMojiDescription.isSelected) {
207+ messageBuilder.append(newFeaturesMessage)
208+ } else if (! insertInCursorPosition.isSelected) {
209+ messageBuilder.append(commitMessage)
210+ }
211+
212+ val message = messageBuilder.toString()
213+ previewArea.text = message
214+ previewArea.select(selectionStart, message.length)
215+ }
216+
217+ private fun getTranslatedString (key : String , language : String ): String {
218+ val lang = if (language == " auto" ) Locale .getDefault().toString() else language
219+ return when (key) {
220+ " Commit message" -> when (lang) {
221+ " fr_FR" -> " Message de commit"
222+ " pt_BR" -> " Mensagem do commit"
223+ " ru_RU" -> " Сообщение коммита"
224+ " zh_CN" -> " 提交信息"
225+ else -> " Commit message"
226+ }
227+ " Introduce new features." -> when (lang) {
228+ " fr_FR" -> " Introduire de nouvelles fonctionnalités."
229+ " pt_BR" -> " Introduzir novas funcionalidades."
230+ " ru_RU" -> " Добавить новые функции."
231+ " zh_CN" -> " 引入新功能。"
232+ else -> " Introduce new features."
233+ }
234+ else -> key
235+ }
143236 }
144237
145238 override fun apply () {
@@ -256,6 +349,7 @@ class GitMojiConfig(private val project: Project) : SearchableConfigurable {
256349 localizationUrlField.text = localizationUrlConfig
257350 setGitmojiSourceFieldsVisibility(gitmojiSourceConfig)
258351 updateSourceTooltip(gitmojiSourceConfig)
352+ updatePreview()
259353 }
260354
261355 private fun setGitmojiSourceFieldsVisibility (type : GitmojiSourceType ) {
0 commit comments