1 local uutil = import("micro/util")
2 local utf8 = import("utf8")
3 local autoclosePairs = {"\"\"", "''", "``", "()", "{}", "[]"}
4 local autoNewlinePairs = {"()", "{}", "[]"}
6 function charAt(str, i)
7 -- lua indexing is one off from go
8 return uutil.RuneAt(str, i-1)
11 function onRune(bp, r)
12 for i = 1, #autoclosePairs do
13 if r == charAt(autoclosePairs[i], 2) then
14 local curLine = bp.Buf:Line(bp.Cursor.Y)
16 if charAt(curLine, bp.Cursor.X+1) == charAt(autoclosePairs[i], 2) then
22 if bp.Cursor.X > 1 and (uutil.IsWordChar(charAt(curLine, bp.Cursor.X-1)) or charAt(curLine, bp.Cursor.X-1) == charAt(autoclosePairs[i], 1)) then
26 if r == charAt(autoclosePairs[i], 1) then
27 local curLine = bp.Buf:Line(bp.Cursor.Y)
29 if bp.Cursor.X == utf8.RuneCountInString(curLine) or not uutil.IsWordChar(charAt(curLine, bp.Cursor.X+1)) then
30 -- the '-' here is to derefence the pointer to bp.Cursor.Loc which is automatically made
31 -- when converting go structs to lua
32 -- It needs to be dereferenced because the function expects a non pointer struct
33 bp.Buf:Insert(-bp.Cursor.Loc, charAt(autoclosePairs[i], 2))
42 function preInsertNewline(bp)
43 local curLine = bp.Buf:Line(bp.Cursor.Y)
44 local curRune = charAt(curLine, bp.Cursor.X)
45 local nextRune = charAt(curLine, bp.Cursor.X+1)
46 local ws = uutil.GetLeadingWhitespace(curLine)
48 for i = 1, #autoNewlinePairs do
49 if curRune == charAt(autoNewlinePairs[i], 1) then
50 if nextRune == charAt(autoNewlinePairs[i], 2) then
53 bp.Buf:Insert(-bp.Cursor.Loc, "\n" .. ws)
64 function preBackspace(bp)
65 for i = 1, #autoclosePairs do
66 local curLine = bp.Buf:Line(bp.Cursor.Y)
67 if charAt(curLine, bp.Cursor.X+1) == charAt(autoclosePairs[i], 2) and charAt(curLine, bp.Cursor.X) == charAt(autoclosePairs[i], 1) then