From ca0ff7a83a7bc069dd850d0f4500fda2944089d5 Mon Sep 17 00:00:00 2001 From: matthias314 Date: Mon, 2 Dec 2024 11:12:14 -0500 Subject: [PATCH 1/2] turned `autoclosePairs` and `autoNewlinePairs` into options --- runtime/plugins/autoclose/autoclose.lua | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/runtime/plugins/autoclose/autoclose.lua b/runtime/plugins/autoclose/autoclose.lua index f1fc2fad9f..5670b77ec8 100644 --- a/runtime/plugins/autoclose/autoclose.lua +++ b/runtime/plugins/autoclose/autoclose.lua @@ -1,9 +1,11 @@ VERSION = "1.0.0" +local config = import("micro/config") local uutil = import("micro/util") local utf8 = import("utf8") -local autoclosePairs = {"\"\"", "''", "``", "()", "{}", "[]"} -local autoNewlinePairs = {"()", "{}", "[]"} + +config.RegisterCommonOption("autoclose", "pairs", {"\"\"", "''", "``", "()", "{}", "[]"}) +config.RegisterCommonOption("autoclose", "newlinePairs", {"()", "{}", "[]"}) function charAt(str, i) -- lua indexing is one off from go @@ -11,6 +13,7 @@ function charAt(str, i) end function onRune(bp, r) + local autoclosePairs = bp.Buf.Settings["autoclose.pairs"] for i = 1, #autoclosePairs do if r == charAt(autoclosePairs[i], 2) then local curLine = bp.Buf:Line(bp.Cursor.Y) @@ -42,6 +45,7 @@ function onRune(bp, r) end function preInsertNewline(bp) + local autoNewlinePairs = bp.Buf.Settings["autoclose.newlinePairs"] local curLine = bp.Buf:Line(bp.Cursor.Y) local curRune = charAt(curLine, bp.Cursor.X) local nextRune = charAt(curLine, bp.Cursor.X+1) @@ -64,6 +68,7 @@ function preInsertNewline(bp) end function preBackspace(bp) + local autoclosePairs = bp.Buf.Settings["autoclose.pairs"] for i = 1, #autoclosePairs do local curLine = bp.Buf:Line(bp.Cursor.Y) if charAt(curLine, bp.Cursor.X+1) == charAt(autoclosePairs[i], 2) and charAt(curLine, bp.Cursor.X) == charAt(autoclosePairs[i], 1) then From 72e178c002e52291f9617554127ba0354269789c Mon Sep 17 00:00:00 2001 From: matthias314 Date: Mon, 23 Dec 2024 12:57:54 -0500 Subject: [PATCH 2/2] added documentation --- runtime/help/plugins.md | 4 ++-- runtime/plugins/autoclose/autoclose.lua | 1 + runtime/plugins/autoclose/help/autoclose.md | 13 +++++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 runtime/plugins/autoclose/help/autoclose.md diff --git a/runtime/help/plugins.md b/runtime/help/plugins.md index fbab646943..ae200d5b29 100644 --- a/runtime/help/plugins.md +++ b/runtime/help/plugins.md @@ -486,8 +486,8 @@ The following plugins come pre-installed with micro: directory, the diff gutter will show changes with respect to the most recent Git commit rather than the diff since opening the file. -See `> help linter`, `> help comment`, and `> help status` for additional -documentation specific to those plugins. +See `> help autoclose`, `> help linter`, `> help comment`, and `> help status` +for additional documentation specific to those plugins. These are good examples for many use-cases if you are looking to write your own plugins. diff --git a/runtime/plugins/autoclose/autoclose.lua b/runtime/plugins/autoclose/autoclose.lua index 5670b77ec8..4df879e1c0 100644 --- a/runtime/plugins/autoclose/autoclose.lua +++ b/runtime/plugins/autoclose/autoclose.lua @@ -6,6 +6,7 @@ local utf8 = import("utf8") config.RegisterCommonOption("autoclose", "pairs", {"\"\"", "''", "``", "()", "{}", "[]"}) config.RegisterCommonOption("autoclose", "newlinePairs", {"()", "{}", "[]"}) +config.AddRuntimeFile("autoclose", config.RTHelp, "help/autoclose.md") function charAt(str, i) -- lua indexing is one off from go diff --git a/runtime/plugins/autoclose/help/autoclose.md b/runtime/plugins/autoclose/help/autoclose.md new file mode 100644 index 0000000000..25d56c44ed --- /dev/null +++ b/runtime/plugins/autoclose/help/autoclose.md @@ -0,0 +1,13 @@ +# Autoclose + +The autoclose plugin automatically closes brackets, quotes and the like, +and it can add indentation between such symbols. The plugin can be configured +on a per-buffer basis via the following options: + +- `autoclose.pairs`: When the first rune in a pair is entered, autoclose will + add the second automatically. Moreover, when the first rune is deleted while + the cursor is on the second, then this second rune is also deleted. + The default value is ```{"\"\"", "''", "``", "()", "{}", "[]"}```. +- `autoclose.newlinePairs`: When `Enter` is pressed between such a pair, + autoclose will put the closing rune on a separate line and add indentation. + The default value is `{"()", "{}", "[]"}`.