3 local uutil = import("micro/util")
4 local utf8 = import("utf8")
5 local autoclosePairs = {"\"\"", "''", "``", "()", "{}", "[]"}
6 local autoNewlinePairs = {"()", "{}", "[]"}
8 function charAt(str, i)
9 -- lua indexing is one off from go
10 return uutil.RuneAt(str, i-1)
13 function onRune(bp, r)
14 for i = 1, #autoclosePairs do
15 if r == charAt(autoclosePairs[i], 2) then
16 local curLine = bp.Buf:Line(bp.Cursor.Y)
18 if charAt(curLine, bp.Cursor.X+1) == charAt(autoclosePairs[i], 2) then
24 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
28 if r == charAt(autoclosePairs[i], 1) then
29 local curLine = bp.Buf:Line(bp.Cursor.Y)
31 if bp.Cursor.X == uutil.CharacterCountInString(curLine) or not uutil.IsWordChar(charAt(curLine, bp.Cursor.X+1)) then
32 -- the '-' here is to derefence the pointer to bp.Cursor.Loc which is automatically made
33 -- when converting go structs to lua
34 -- It needs to be dereferenced because the function expects a non pointer struct
35 bp.Buf:Insert(-bp.Cursor.Loc, charAt(autoclosePairs[i], 2))
44 function preInsertNewline(bp)
45 local curLine = bp.Buf:Line(bp.Cursor.Y)
46 local curRune = charAt(curLine, bp.Cursor.X)
47 local nextRune = charAt(curLine, bp.Cursor.X+1)
48 local ws = uutil.GetLeadingWhitespace(curLine)
50 for i = 1, #autoNewlinePairs do
51 if curRune == charAt(autoNewlinePairs[i], 1) then
52 if nextRune == charAt(autoNewlinePairs[i], 2) then
55 bp.Buf:Insert(-bp.Cursor.Loc, "\n" .. ws)
66 function preBackspace(bp)
67 for i = 1, #autoclosePairs do
68 local curLine = bp.Buf:Line(bp.Cursor.Y)
69 if charAt(curLine, bp.Cursor.X+1) == charAt(autoclosePairs[i], 2) and charAt(curLine, bp.Cursor.X) == charAt(autoclosePairs[i], 1) then