Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

turn autoclosePairs and autoNewlinePairs into buffer-local options #3562

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions runtime/help/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 8 additions & 2 deletions runtime/plugins/autoclose/autoclose.lua
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
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", {"()", "{}", "[]"})
config.AddRuntimeFile("autoclose", config.RTHelp, "help/autoclose.md")

function charAt(str, i)
-- lua indexing is one off from go
return uutil.RuneAt(str, i-1)
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)
Expand Down Expand Up @@ -42,6 +46,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)
Expand All @@ -64,6 +69,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
Expand Down
13 changes: 13 additions & 0 deletions runtime/plugins/autoclose/help/autoclose.md
Original file line number Diff line number Diff line change
@@ -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 `{"()", "{}", "[]"}`.
Loading