1 local utf8 = require "runtime/plugins/autoclose/utf8/utf8"
3 function charAt(str, i)
4 if i <= utf8.len(str) then
5 return utf8.sub(str, i, i)
11 if GetOption("autoclose") == nil then
12 AddOption("autoclose", true)
15 local autoclosePairs = {"\"\"", "''", "``", "()", "{}", "[]"}
16 local autoNewlinePairs = {"()", "{}", "[]"}
19 if not GetOption("autoclose") then
23 for i = 1, #autoclosePairs do
24 if r == charAt(autoclosePairs[i], 2) then
25 local curLine = v.Buf:Line(v.Cursor.Y)
27 if charAt(curLine, v.Cursor.X+1) == charAt(autoclosePairs[i], 2) then
33 if v.Cursor.X > 1 and (IsWordChar(charAt(curLine, v.Cursor.X-1)) or charAt(curLine, v.Cursor.X-1) == charAt(autoclosePairs[i], 1)) then
37 if r == charAt(autoclosePairs[i], 1) then
38 local curLine = v.Buf:Line(v.Cursor.Y)
40 if v.Cursor.X == utf8.len(curLine) or not IsWordChar(charAt(curLine, v.Cursor.X+1)) then
41 -- the '-' here is to derefence the pointer to v.Cursor.Loc which is automatically made
42 -- when converting go structs to lua
43 -- It needs to be dereferenced because the function expects a non pointer struct
44 v.Buf:Insert(-v.Cursor.Loc, charAt(autoclosePairs[i], 2))
51 function preInsertNewline(v)
52 if not GetOption("autoclose") then
56 local curLine = v.Buf:Line(v.Cursor.Y)
57 local curRune = charAt(curLine, v.Cursor.X)
58 local nextRune = charAt(curLine, v.Cursor.X+1)
59 local ws = GetLeadingWhitespace(curLine)
61 for i = 1, #autoNewlinePairs do
62 if curRune == charAt(autoNewlinePairs[i], 1) then
63 if nextRune == charAt(autoNewlinePairs[i], 2) then
64 v:InsertNewline(false)
66 v.Buf:Insert(-v.Cursor.Loc, "\n" .. ws)
75 function preBackspace(v)
76 if not GetOption("autoclose") then
80 for i = 1, #autoclosePairs do
81 local curLine = v.Buf:Line(v.Cursor.Y)
82 if charAt(curLine, v.Cursor.X+1) == charAt(autoclosePairs[i], 2) and charAt(curLine, v.Cursor.X) == charAt(autoclosePairs[i], 1) then