Skip to content

Commit

Permalink
Merge pull request #13 from anton-erofeev/sort-lines-action
Browse files Browse the repository at this point in the history
added json sorting
  • Loading branch information
anton-erofeev authored Dec 11, 2024
2 parents 243c176 + c607ea0 commit c9bc520
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
## Description
<!-- Plugin 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.
- **Sorting by Line Length:** Sort lines based on their length. Supports both ascending (shortest to longest) and descending (longest to shortest) order.
- **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.

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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)
}
Expand All @@ -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.
*
Expand Down Expand Up @@ -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
}
}
}

0 comments on commit c9bc520

Please sign in to comment.