Skip to content

Commit cb14315

Browse files
committed
Add JSON import/export with extended mappings details
Implemented JSON-based import/export for symbol mappings with additional metadata, including IDE and plugin versions, export date, and mapping count. Enhanced UI feedback with success messages for both operations. Updated the MappingsData model to include versioning and removed unused fields.
1 parent 935379b commit cb14315

File tree

3 files changed

+52
-16
lines changed

3 files changed

+52
-16
lines changed

CHANGES.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
## 2024.1 (December 17, 2024)
2+
3+
* Added settings panel for symbol mappings
4+
* Added support for mapping categories
5+
* Added JSON import/export functionality for mappings
6+
* Added drag-and-drop support for sorting
7+
* Updated Gradle, Java and IntelliJ Platform versions
8+
19
## 2020.2 (April 19, 2020)
210

311
* Fix: Replace "**" with "^" only inside PyBinaryExpression
@@ -7,7 +15,7 @@
715
+ Folding ** to ^
816

917
## 2019.2.3 (December, 04, 2019)
10-
18+
1119
* Version for Intellij 2019.3.*
1220
* jvmTarget 11
1321

src/main/kotlin/dev/meanmail/prettifypython/settings/MappingsData.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import kotlinx.serialization.Serializable
44

55
@Serializable
66
data class MappingsData(
7+
val version: String = "1.0",
78
val exportDate: String,
89
val pluginVersion: String,
910
val ideVersion: String,
1011
val mappingsCount: Int,
1112
val comment: String = "",
12-
val mappings: List<MappingEntry>,
13-
val categories: Set<String> = emptySet()
13+
val mappings: List<MappingEntry>
1414
)

src/main/kotlin/dev/meanmail/prettifypython/settings/PrettifySettingsComponent.kt

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package dev.meanmail.prettifypython.settings
22

33
import com.intellij.icons.AllIcons
4+
import com.intellij.ide.plugins.PluginManagerCore
45
import com.intellij.openapi.actionSystem.ActionManager
56
import com.intellij.openapi.actionSystem.ActionToolbarPosition
67
import com.intellij.openapi.actionSystem.AnAction
78
import com.intellij.openapi.actionSystem.AnActionEvent
89
import com.intellij.openapi.actionSystem.impl.SimpleDataContext
10+
import com.intellij.openapi.application.ApplicationInfo
11+
import com.intellij.openapi.extensions.PluginId
912
import com.intellij.openapi.ui.DialogWrapper
1013
import com.intellij.openapi.ui.Messages
1114
import com.intellij.ui.IdeBorderFactory
@@ -191,8 +194,6 @@ class PrettifySettingsComponent {
191194
else -> return false
192195
}
193196

194-
// Remove old node
195-
val oldParent = draggedNode.parent as DefaultMutableTreeNode
196197
treeModel.removeNodeFromParent(draggedNode)
197198

198199
// Create new mapping with target category
@@ -208,42 +209,69 @@ class PrettifySettingsComponent {
208209

209210
private fun importMappings() {
210211
val fileChooser = JFileChooser().apply {
211-
fileFilter = FileNameExtensionFilter("JSON files", "json")
212212
dialogTitle = "Import Mappings"
213+
fileSelectionMode = JFileChooser.FILES_ONLY
214+
fileFilter = FileNameExtensionFilter("JSON files", "json")
213215
}
214216

215217
if (fileChooser.showOpenDialog(mainPanel) == JFileChooser.APPROVE_OPTION) {
216218
try {
217-
val jsonContent = fileChooser.selectedFile.readText()
218-
val importedMappings = json.decodeFromString<List<MappingEntry>>(jsonContent)
219-
setMappings(importedMappings)
219+
val jsonString = fileChooser.selectedFile.readText()
220+
val mappingsData = json.decodeFromString<MappingsData>(jsonString)
221+
setMappings(mappingsData.mappings)
222+
Messages.showInfoMessage(
223+
mainPanel,
224+
"Mappings imported successfully",
225+
"Import Successful"
226+
)
220227
} catch (e: Exception) {
221228
Messages.showErrorDialog(
222229
mainPanel,
223230
"Failed to import mappings: ${e.message}",
224-
"Import Error"
231+
"Import Failed"
225232
)
226233
}
227234
}
228235
}
229236

230237
private fun exportMappings() {
231238
val fileChooser = JFileChooser().apply {
232-
fileFilter = FileNameExtensionFilter("JSON files", "json")
233239
dialogTitle = "Export Mappings"
234-
val timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss"))
235-
selectedFile = File("prettify_python_mappings_$timestamp.json")
240+
fileSelectionMode = JFileChooser.FILES_ONLY
241+
fileFilter = FileNameExtensionFilter("JSON files", "json")
242+
val now = LocalDateTime.now()
243+
val formatter = DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss")
244+
selectedFile = File("prettify_python_mappings_${now.format(formatter)}.json")
236245
}
237246

238247
if (fileChooser.showSaveDialog(mainPanel) == JFileChooser.APPROVE_OPTION) {
239248
try {
240-
val jsonContent = json.encodeToString(getMappings())
241-
fileChooser.selectedFile.writeText(jsonContent)
249+
val mappings = getMappings()
250+
val pluginId = PluginId.findId("ru.meanmail.plugins.prettify-python")
251+
?: throw IllegalStateException("Plugin ID not found")
252+
val plugin = PluginManagerCore.getPlugin(pluginId)
253+
?: throw IllegalStateException("Plugin not found")
254+
255+
val now = LocalDateTime.now()
256+
val mappingsData = MappingsData(
257+
mappings = mappings,
258+
ideVersion = ApplicationInfo.getInstance().fullVersion,
259+
pluginVersion = plugin.version,
260+
mappingsCount = mappings.size,
261+
exportDate = now.toString()
262+
)
263+
val jsonString = json.encodeToString(mappingsData)
264+
fileChooser.selectedFile.writeText(jsonString)
265+
Messages.showInfoMessage(
266+
mainPanel,
267+
"Mappings exported successfully",
268+
"Export Successful"
269+
)
242270
} catch (e: Exception) {
243271
Messages.showErrorDialog(
244272
mainPanel,
245273
"Failed to export mappings: ${e.message}",
246-
"Export Error"
274+
"Export Failed"
247275
)
248276
}
249277
}

0 commit comments

Comments
 (0)