1 function charAt(str, i)
2 return string.sub(str, i, i)
5 if GetOption("autoclose") == nil then
6 AddOption("autoclose", true)
9 local autoclosePairs = {"\"\"", "''", "()", "{}", "[]"}
10 local autoNewlinePairs = {"()", "{}", "[]"}
13 if not GetOption("autoclose") then
17 for i = 1, #autoclosePairs do
18 if r == charAt(autoclosePairs[i], 2) then
19 local curLine = v.Buf:Line(v.Cursor.Y)
21 if charAt(curLine, v.Cursor.X+1) == charAt(autoclosePairs[i], 2) then
27 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
31 if r == charAt(autoclosePairs[i], 1) then
32 local curLine = v.Buf:Line(v.Cursor.Y)
34 if v.Cursor.X == #curLine or not IsWordChar(charAt(curLine, v.Cursor.X+1)) then
35 -- the '-' here is to derefence the pointer to v.Cursor.Loc which is automatically made
36 -- when converting go structs to lua
37 -- It needs to be dereferenced because the function expects a non pointer struct
38 v.Buf:Insert(-v.Cursor.Loc, charAt(autoclosePairs[i], 2))
45 function preInsertNewline(v)
46 if not GetOption("autoclose") then
50 local curLine = v.Buf:Line(v.Cursor.Y)
51 local curRune = charAt(curLine, v.Cursor.X)
52 local nextRune = charAt(curLine, v.Cursor.X+1)
53 local ws = GetLeadingWhitespace(curLine)
55 for i = 1, #autoNewlinePairs do
56 if curRune == charAt(autoNewlinePairs[i], 1) then
57 if nextRune == charAt(autoNewlinePairs[i], 2) then
58 v:InsertNewline(false)
60 v.Buf:Insert(-v.Cursor.Loc, "\n" .. ws)
69 function preBackspace(v)
70 if not GetOption("autoclose") then
74 for i = 1, #autoclosePairs do
75 local curLine = v.Buf:Line(v.Cursor.Y)
76 if charAt(curLine, v.Cursor.X+1) == charAt(autoclosePairs[i], 2) and charAt(curLine, v.Cursor.X) == charAt(autoclosePairs[i], 1) then