From 7002660c874933a4d3eb563ee65c7a0c5ba206d6 Mon Sep 17 00:00:00 2001 From: rxi Date: Sun, 29 Mar 2020 08:59:42 +0100 Subject: Added sort plugin --- README.md | 4 ++-- sort.lua | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 sort.lua diff --git a/README.md b/README.md index 426e793..0c700b0 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,6 @@ Plugin | Description [`language_odin`](language_odin.lua?raw=1) | Syntax for the [Odin](https://github.com/odin-lang/Odin) programming language [`language_wren`](language_wren.lua?raw=1) | Syntax for the [Wren](http://wren.io/) programming language [`macmodkeys`](macmodkeys.lua?raw=1) | Remaps mac modkeys `command/option` to `ctrl/alt` +[`sort`](sort.lua?raw=1) | Sorts selected lines [`titleize`](titleize.lua?raw=1) | Titleizes selected string (`hello world` => `Hello World`) -[`togglesnakecamel`](togglesnakecamel.lua?raw=1) | Toggles symbols between snake\_case and camelCase - +[`togglesnakecamel`](togglesnakecamel.lua?raw=1) | Toggles symbols between `snake\_case` and `camelCase` diff --git a/sort.lua b/sort.lua new file mode 100644 index 0000000..e104043 --- /dev/null +++ b/sort.lua @@ -0,0 +1,20 @@ +local core = require "core" +local command = require "core.command" + +local function split_lines(text) + local res = {} + for line in (text .. "\n"):gmatch("(.-)\n") do + table.insert(res, line) + end + return res +end + +command.add("core.docview", { + ["sort:sort"] = function() + core.active_view.doc:replace(function(text) + local lines = split_lines(text) + table.sort(lines, function(a, b) return a:lower() < b:lower() end) + return table.concat(lines, "\n") + end) + end, +}) -- cgit v1.2.3