]> git.lizzy.rs Git - micro.git/blobdiff - runtime/plugins/autoclose/autoclose.lua
Merge branch 'python-highlight-zero' of https://github.com/a11ce/micro into a11ce...
[micro.git] / runtime / plugins / autoclose / autoclose.lua
index aed2ec6346af78bd9fe218c27fbd216db5a67a14..531b7601962d0a0881d773ccccc05587be47c30a 100644 (file)
@@ -1,63 +1,60 @@
-function charAt(str, i)
-    return string.sub(str, i, i)
-end
-
-if GetOption("autoclose") == nil then
-    AddOption("autoclose", true)
-end
+VERSION = "1.0.0"
 
+local uutil = import("micro/util")
+local utf8 = import("utf8")
 local autoclosePairs = {"\"\"", "''", "``", "()", "{}", "[]"}
 local autoNewlinePairs = {"()", "{}", "[]"}
 
-function onRune(r, v)
-    if not GetOption("autoclose") then
-        return
-    end
+function charAt(str, i)
+    -- lua indexing is one off from go
+    return uutil.RuneAt(str, i-1)
+end
 
+function onRune(bp, r)
     for i = 1, #autoclosePairs do
         if r == charAt(autoclosePairs[i], 2) then
-            local curLine = v.Buf:Line(v.Cursor.Y)
+            local curLine = bp.Buf:Line(bp.Cursor.Y)
 
-            if charAt(curLine, v.Cursor.X+1) == charAt(autoclosePairs[i], 2) then
-                v:Backspace(false)
-                v:CursorRight(false)
+            if charAt(curLine, bp.Cursor.X+1) == charAt(autoclosePairs[i], 2) then
+                bp:Backspace()
+                bp:CursorRight()
                 break
             end
 
-            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
+            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
                 break
             end
         end
         if r == charAt(autoclosePairs[i], 1) then
-            local curLine = v.Buf:Line(v.Cursor.Y)
+            local curLine = bp.Buf:Line(bp.Cursor.Y)
 
-            if v.Cursor.X == #curLine or not IsWordChar(charAt(curLine, v.Cursor.X+1)) then
-                -- the '-' here is to derefence the pointer to v.Cursor.Loc which is automatically made
+            if bp.Cursor.X == uutil.CharacterCountInString(curLine) or not uutil.IsWordChar(charAt(curLine, bp.Cursor.X+1)) then
+                -- the '-' here is to derefence the pointer to bp.Cursor.Loc which is automatically made
                 -- when converting go structs to lua
                 -- It needs to be dereferenced because the function expects a non pointer struct
-                v.Buf:Insert(-v.Cursor.Loc, charAt(autoclosePairs[i], 2))
+                bp.Buf:Insert(-bp.Cursor.Loc, charAt(autoclosePairs[i], 2))
+                bp:CursorLeft()
                 break
             end
         end
     end
+    return true
 end
 
-function preInsertNewline(v)
-    if not GetOption("autoclose") then
-        return
-    end
-
-    local curLine = v.Buf:Line(v.Cursor.Y)
-    local curRune = charAt(curLine, v.Cursor.X)
-    local nextRune = charAt(curLine, v.Cursor.X+1)
-    local ws = GetLeadingWhitespace(curLine)
+function preInsertNewline(bp)
+    local curLine = bp.Buf:Line(bp.Cursor.Y)
+    local curRune = charAt(curLine, bp.Cursor.X)
+    local nextRune = charAt(curLine, bp.Cursor.X+1)
+    local ws = uutil.GetLeadingWhitespace(curLine)
 
     for i = 1, #autoNewlinePairs do
         if curRune == charAt(autoNewlinePairs[i], 1) then
             if nextRune == charAt(autoNewlinePairs[i], 2) then
-                v:InsertNewline(false)
-                v:InsertTab(false)
-                v.Buf:Insert(-v.Cursor.Loc, "\n" .. ws)
+                bp:InsertNewline()
+                bp:InsertTab()
+                bp.Buf:Insert(-bp.Cursor.Loc, "\n" .. ws)
+                bp:StartOfLine()
+                bp:CursorLeft()
                 return false
             end
         end
@@ -66,15 +63,11 @@ function preInsertNewline(v)
     return true
 end
 
-function preBackspace(v)
-    if not GetOption("autoclose") then
-        return
-    end
-
+function preBackspace(bp)
     for i = 1, #autoclosePairs do
-        local curLine = v.Buf:Line(v.Cursor.Y)
-        if charAt(curLine, v.Cursor.X+1) == charAt(autoclosePairs[i], 2) and charAt(curLine, v.Cursor.X) == charAt(autoclosePairs[i], 1) then
-            v:Delete(false)
+        local curLine = bp.Buf:Line(bp.Cursor.Y)
+        if charAt(curLine, bp.Cursor.X+1) == charAt(autoclosePairs[i], 2) and charAt(curLine, bp.Cursor.X) == charAt(autoclosePairs[i], 1) then
+            bp:Delete()
         end
     end