OSDN Git Service

Initial release
authorpluginfan <pluginfan@suremail.info>
Tue, 5 Apr 2016 18:03:26 +0000 (21:03 +0300)
committerpluginfan <pluginfan@suremail.info>
Tue, 5 Apr 2016 18:03:26 +0000 (21:03 +0300)
Line Sort/NEWS [new file with mode: 0644]
Line Sort/README.md [new file with mode: 0644]
Line Sort/TODO [new file with mode: 0644]
Line Sort/plugin.groovy [new file with mode: 0644]
README.md

diff --git a/Line Sort/NEWS b/Line Sort/NEWS
new file mode 100644 (file)
index 0000000..fe3e01d
--- /dev/null
@@ -0,0 +1,3 @@
+Version 0.0 -- April 2016
+~~~~~~~~~~~~~~~~~~~~~~~~~
+Initial release.
diff --git a/Line Sort/README.md b/Line Sort/README.md
new file mode 100644 (file)
index 0000000..a175a1d
--- /dev/null
@@ -0,0 +1,13 @@
+# Line Sort
+
+Sorts the selected lines according to a sorting method.
+
+Action:
+
+* `Line Sort`
+
+Shortcut:
+
+* <kbd>Alt</kbd>+<kbd>S</kbd> -- Line Sort
+
+Just a simple tool to help when organizing the source code.
diff --git a/Line Sort/TODO b/Line Sort/TODO
new file mode 100644 (file)
index 0000000..51b519c
--- /dev/null
@@ -0,0 +1,3 @@
+* Preserve leading and trailing whitespace in the selection, excluding them from sorting.
+* Think about handling empty lines inside the selection.
+* Add more sorting methods.
diff --git a/Line Sort/plugin.groovy b/Line Sort/plugin.groovy
new file mode 100644 (file)
index 0000000..97c6e0d
--- /dev/null
@@ -0,0 +1,47 @@
+// plugin  Line Sort
+// version 0.0
+// release April 2016
+// author  Plugin Fan
+// license GPL v 3.0
+
+// Sorts the selected lines according to a sorting method:
+//     - Alt-S - Line Sort
+//
+// Just a simple tool to help when organizing the source code.
+
+import com.intellij.openapi.actionSystem.AnActionEvent
+import com.intellij.openapi.actionSystem.Separator
+
+import static liveplugin.PluginUtil.*
+
+// available sort methods -- the closures are expecting a List of String
+def lineSortMethod = [
+   'Sort': { it.sort() },
+   'Sort Case Insensitive': { it.sort(false, String.CASE_INSENSITIVE_ORDER) },
+   'Sort Numerically': { it.sort { it.findAll(/-?\d*\.?\d+/).collect { it.isInteger() ? it.toInteger() : it.toDouble() } } },
+   'Sort By Length': { it.sort { it.size() } },
+   'No Sort, Reverse': { it.reverse() },
+]
+
+// perform the sorting
+def lineSort(project, sorter)
+{
+    runDocumentWriteAction(project) {
+        currentEditorIn(project).with {
+            if (! selectionModel.hasSelection())
+                return
+
+            def text = selectionModel.getSelectedText()
+
+            text = sorter(text.tokenize('\n')).join('\n')
+
+            document.replaceString(selectionModel.getSelectionStart(), selectionModel.getSelectionEnd(), text)
+        }
+    }
+}
+
+registerAction('Line Sort', 'alt S') { AnActionEvent event ->
+    def project = event.project
+
+    showPopupMenu(lineSortMethod.collectEntries { type, code -> [type, { lineSort(project, code) } ] }, 'Line Sort', event.dataContext)
+}
index f36d495..f08f725 100644 (file)
--- a/README.md
+++ b/README.md
@@ -2,5 +2,6 @@
 
 Plugins written in [Groovy](http://groovy-lang.org/) script for [IntelliJ IDEA](http://jetbrains.com/idea/) based IDE's that can be enhanced through [LivePlugin](https://github.com/dkandalov/live-plugin) plugin connector.
 
+* [Line Sort](https://osdn.jp/projects/ii-lp-script/scm/git/ii-lp-script/tree/master/Line%20Sort/) -- Sorts the selected lines according to a sorting method.
 * [Move Caret to Word End](https://osdn.jp/projects/ii-lp-script/scm/git/ii-lp-script/tree/master/Move%20Caret%20To%20Word%20End/) -- Moves all carets to the closes word end.
 * [PrettyPrint](https://osdn.jp/projects/ii-lp-script/scm/git/ii-lp-script/tree/master/PrettyPrint/) -- Formats the current selection as prettified JSON or XML.