diff --git a/CHANGELOG.md b/CHANGELOG.md index fa6f952..923ca89 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ ## [Unreleased] +## [1.0.0] - 2024-12-11 +### Added +- Added JSON file Sorting feature + ## [0.1.1] - 2024-11-17 ### Added - Added Shuffle Lines feature @@ -26,3 +30,4 @@ [0.0.2]: https://github.com/anton-erofeev/line-sorter-intellij-plugin/commits/v0.0.2 [0.1.0]: https://github.com/anton-erofeev/line-sorter-intellij-plugin/commits/v0.1.0 [0.1.1]: https://github.com/anton-erofeev/line-sorter-intellij-plugin/commits/v0.1.1 +[1.0.0]: https://github.com/anton-erofeev/line-sorter-intellij-plugin/commits/v1.0.0 diff --git a/README.md b/README.md index b432b12..366554b 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ ## Description Lines Sorter Pro is a handy tool for developers that lets you quickly sort or shuffle lines within a file or just within a selected area. -The plugin adds multiple string sorting options, including alphabetical, by line length, and shuffle, to the editor's right-click Refactor menu, making it easy and intuitive to organize or mix up your content. +The plugin adds multiple string sorting options, including alphabetical, by line length, shuffle and JSON sorting to the editor's right-click Refactor menu, making it easy and intuitive to organize or mix up your content. ### **Key Features:** - **Alphabetical Sorting:** Sort lines alphabetically. Supports both ascending (A-Z) and descending (Z-A) order. @@ -15,7 +15,7 @@ The plugin adds multiple string sorting options, including alphabetical, by line - **Selected Lines Sorting:** If text is highlighted, the plugin sorts only the selected lines, leaving the rest of the file unchanged. - **Full-File Sorting:** If no text is selected, all lines in the file are sorted alphabetically. - **Shuffle Lines:** Randomly shuffle the lines in the selection or the entire file, providing a quick way to mix up content without sorting. - +- **JSON File Sorting:** Allows sorting of JSON files, with options for alphabetical, value length, or shuffled order. **Ideal for:** Developers working with configuration files, property lists, text data, or any files where organized, readable line order improves workflow and readability. diff --git a/gradle.properties b/gradle.properties index 937350f..2b75601 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,7 +4,7 @@ pluginGroup = com.github.antonerofeev.linesorterintellijplugin pluginName = line-sorter-intellij-plugin pluginRepositoryUrl = https://github.com/anton-erofeev/line-sorter-intellij-plugin # SemVer format -> https://semver.org -pluginVersion = 0.1.1 +pluginVersion = 1.0.0 # Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html pluginSinceBuild = 231 diff --git a/src/main/kotlin/com/github/antonerofeev/linesorterintellijplugin/utils/LinesSorter.kt b/src/main/kotlin/com/github/antonerofeev/linesorterintellijplugin/utils/LinesSorter.kt index 508cfef..0d87556 100644 --- a/src/main/kotlin/com/github/antonerofeev/linesorterintellijplugin/utils/LinesSorter.kt +++ b/src/main/kotlin/com/github/antonerofeev/linesorterintellijplugin/utils/LinesSorter.kt @@ -6,6 +6,9 @@ import com.intellij.openapi.command.WriteCommandAction import com.intellij.openapi.editor.Editor import com.intellij.openapi.project.Project import com.intellij.openapi.ui.Messages +import kotlinx.serialization.json.Json +import kotlinx.serialization.json.JsonObject +import kotlinx.serialization.json.JsonPrimitive import java.util.* import java.util.stream.Collectors @@ -22,6 +25,7 @@ object LinesSorter { fun sort(project: Project?, editor: Editor?, sortType: SortType, sortOrder: SortOrder) { if (editor != null) { val document = editor.document + val fileExtension = editor.virtualFile.extension var start = editor.selectionModel.selectionStart var end = editor.selectionModel.selectionEnd @@ -35,12 +39,11 @@ object LinesSorter { end = document.textLength } - val sortedText = when (sortType) { - SortType.ALPHABETICAL -> sortAlphabetically(textToSort, sortOrder) - SortType.BY_LENGTH -> sortByLength(textToSort, sortOrder) - SortType.SHUFFLE -> shuffle(textToSort) + val sortedText = if (fileExtension == "json") { + sortJson(textToSort, sortType, sortOrder) + } else { + sortText(textToSort, sortType, sortOrder) } - WriteCommandAction.runWriteCommandAction(project) { document.replaceString(start, end, sortedText) } @@ -50,6 +53,15 @@ object LinesSorter { } } + private fun sortText(textToSort: String, sortType: SortType, sortOrder: SortOrder): String { + val sortedText = when (sortType) { + SortType.ALPHABETICAL -> sortAlphabetically(textToSort, sortOrder) + SortType.BY_LENGTH -> sortByLength(textToSort, sortOrder) + SortType.SHUFFLE -> shuffle(textToSort) + } + return sortedText + } + /** * Sorts lines alphabetically. * @@ -97,4 +109,40 @@ object LinesSorter { lines.shuffle() return lines.joinToString("\n") } + + private fun sortJson(text: String, sortType: SortType, sortOrder: SortOrder): String { + return try { + val json = Json { prettyPrint = true } + val jsonObject = json.parseToJsonElement(text) as JsonObject + + val sortedJsonObject = when (sortType) { + SortType.ALPHABETICAL -> { + JsonObject( + jsonObject.entries.sortedBy { it.key }.let { + if (sortOrder == SortOrder.DESCENDING) it.reversed() else it + }.associate { it.key to it.value } + ) + } + SortType.BY_LENGTH -> { + JsonObject( + jsonObject.entries.sortedBy { (_, value) -> + (value as? JsonPrimitive)?.content?.length ?: 0 + }.let { + if (sortOrder == SortOrder.DESCENDING) it.reversed() else it + }.associate { it.key to it.value } + ) + } + SortType.SHUFFLE -> { + JsonObject( + jsonObject.entries.shuffled().associate { it.key to it.value } + ) + } + } + + json.encodeToString(JsonObject.serializer(), sortedJsonObject) + } catch (e: Exception) { + Messages.showMessageDialog(null, "Invalid JSON format", "Error", Messages.getErrorIcon()) + text + } + } }