From ff9b20e8637c12f7ee5b92def4be8fabbb162697 Mon Sep 17 00:00:00 2001 From: Anton Erofeev Date: Sun, 17 Nov 2024 19:35:15 +0300 Subject: [PATCH] added sort lines action --- CHANGELOG.md | 5 +++++ README.md | 5 +++-- gradle.properties | 2 +- .../actions/ShuffleLinesAction.kt | 17 +++++++++++++++++ .../linesorterintellijplugin/enums/SortType.kt | 3 ++- .../utils/LinesSorter.kt | 13 +++++++++++++ src/main/resources/META-INF/plugin.xml | 10 ++++++++-- 7 files changed, 49 insertions(+), 6 deletions(-) create mode 100644 src/main/kotlin/com/github/antonerofeev/linesorterintellijplugin/actions/ShuffleLinesAction.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c02332..fa6f952 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ ## [Unreleased] +## [0.1.1] - 2024-11-17 +### Added +- Added Shuffle Lines feature + ## [0.1.0] - 2024-11-14 ### Added - Added Sort Lines by length ascending feature @@ -21,3 +25,4 @@ [0.0.1]: https://github.com/anton-erofeev/line-sorter-intellij-plugin/commits/v0.0.1 [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 diff --git a/README.md b/README.md index 64dc625..b432b12 100644 --- a/README.md +++ b/README.md @@ -6,14 +6,15 @@ ## Description -Lines Sorter Pro is a handy tool for developers that lets you quickly sort lines within a file or just within a selected area. -This plugin adds a "Sort Lines" option to the editor's right-click Refactor menu, making line sorting easy and intuitive. +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. ### **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. **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 f86151c..937350f 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.0 +pluginVersion = 0.1.1 # 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/actions/ShuffleLinesAction.kt b/src/main/kotlin/com/github/antonerofeev/linesorterintellijplugin/actions/ShuffleLinesAction.kt new file mode 100644 index 0000000..36eb68e --- /dev/null +++ b/src/main/kotlin/com/github/antonerofeev/linesorterintellijplugin/actions/ShuffleLinesAction.kt @@ -0,0 +1,17 @@ +package com.github.antonerofeev.linesorterintellijplugin.actions + +import com.github.antonerofeev.linesorterintellijplugin.enums.SortOrder +import com.github.antonerofeev.linesorterintellijplugin.enums.SortType +import com.github.antonerofeev.linesorterintellijplugin.utils.LinesSorter +import com.intellij.openapi.actionSystem.AnAction +import com.intellij.openapi.actionSystem.AnActionEvent +import com.intellij.openapi.actionSystem.CommonDataKeys + +class ShuffleLinesAction : AnAction() { + override fun actionPerformed(e: AnActionEvent) { + val project = e.project + val editor = e.getData(CommonDataKeys.EDITOR) + + LinesSorter.sort(project, editor, SortType.SHUFFLE, SortOrder.ASCENDING) + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/github/antonerofeev/linesorterintellijplugin/enums/SortType.kt b/src/main/kotlin/com/github/antonerofeev/linesorterintellijplugin/enums/SortType.kt index 3134cd2..95316e3 100644 --- a/src/main/kotlin/com/github/antonerofeev/linesorterintellijplugin/enums/SortType.kt +++ b/src/main/kotlin/com/github/antonerofeev/linesorterintellijplugin/enums/SortType.kt @@ -2,5 +2,6 @@ package com.github.antonerofeev.linesorterintellijplugin.enums enum class SortType { ALPHABETICAL, - BY_LENGTH + BY_LENGTH, + SHUFFLE } \ No newline at end of file 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 82851dc..508cfef 100644 --- a/src/main/kotlin/com/github/antonerofeev/linesorterintellijplugin/utils/LinesSorter.kt +++ b/src/main/kotlin/com/github/antonerofeev/linesorterintellijplugin/utils/LinesSorter.kt @@ -38,6 +38,7 @@ object LinesSorter { val sortedText = when (sortType) { SortType.ALPHABETICAL -> sortAlphabetically(textToSort, sortOrder) SortType.BY_LENGTH -> sortByLength(textToSort, sortOrder) + SortType.SHUFFLE -> shuffle(textToSort) } WriteCommandAction.runWriteCommandAction(project) { @@ -84,4 +85,16 @@ object LinesSorter { } .collect(Collectors.joining("\n")) } + + /** + * Shuffles lines randomly. + * + * @param text The text to shuffle. + * @return The shuffled text. + */ + private fun shuffle(text: String): String { + val lines = text.split("\n".toRegex()).dropLastWhile { it.isEmpty() }.toMutableList() + lines.shuffle() + return lines.joinToString("\n") + } } diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index b5e040c..50a0ac5 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -8,8 +8,8 @@ messages.MyBundle @@ -45,6 +45,12 @@ text="Sort Lines By Length Descending" description="Sort lines in a file by length in descending order"> + +