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
18 for i = 1, #autoclosePairs do
19 if r == charAt(autoclosePairs[i], 2) then
20 local curLine = v.Buf:Line(v.Cursor.Y)
22 if charAt(curLine, v.Cursor.X+1) == charAt(autoclosePairs[i], 2) then
28 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
32 if r == charAt(autoclosePairs[i], 1) then
33 local curLine = v.Buf:Line(v.Cursor.Y)
35 if v.Cursor.X == #curLine or not IsWordChar(charAt(curLine, v.Cursor.X+1)) then
36 -- the '-' here is to derefence the pointer to v.Cursor.Loc which is automatically made
37 -- when converting go structs to lua
38 -- It needs to be dereferenced because the function expects a non pointer struct
39 v.Buf:Insert(-v.Cursor.Loc, charAt(autoclosePairs[i], 2))
46 function preInsertNewline()
47 if not GetOption("autoclose") then
52 local curLine = v.Buf:Line(v.Cursor.Y)
53 local curRune = charAt(curLine, v.Cursor.X)
54 local nextRune = charAt(curLine, v.Cursor.X+1)
55 local ws = GetLeadingWhitespace(curLine)
56 messenger:Message(curRune, " ", nextRune)
58 for i = 1, #autoNewlinePairs do
59 if curRune == charAt(autoNewlinePairs[i], 1) then
60 if nextRune == charAt(autoNewlinePairs[i], 2) then
61 v:InsertNewline(false)
63 v.Buf:Insert(-v.Cursor.Loc, "\n" .. ws)
72 function preBackspace()
73 if not GetOption("autoclose") then
79 for i = 1, #autoclosePairs do
80 local curLine = v.Buf:Line(v.Cursor.Y)
81 if charAt(curLine, v.Cursor.X+1) == charAt(autoclosePairs[i], 2) and charAt(curLine, v.Cursor.X) == charAt(autoclosePairs[i], 1) then