Fix issue with autoclose plugin

This commit is contained in:
Zachary Yedidia
2016-06-22 21:03:40 -04:00
parent dbeb99bb6b
commit e6adc173f3
2 changed files with 110 additions and 108 deletions

View File

@@ -1,4 +1,6 @@
getmetatable('').__index = function(str,i) return string.sub(str,i,i) end
function charAt(str, i)
return string.sub(str, i, i)
end
if GetOption("autoclose") == nil then
AddOption("autoclose", true)
@@ -14,27 +16,27 @@ function onRune(r)
local v = CurView()
for i = 1, #autoclosePairs do
if r == autoclosePairs[i][2] then
if r == charAt(autoclosePairs[i], 2) then
local curLine = v.Buf:Line(v.Cursor.Y)
if curLine[v.Cursor.X+1] == autoclosePairs[i][2] then
if charAt(curLine, v.Cursor.X+1) == charAt(autoclosePairs[i], 2) then
v:Backspace()
v:CursorRight()
break
end
if v.Cursor.X > 1 and (IsWordChar(curLine[v.Cursor.X-1]) or curLine[v.Cursor.X-1] == autoclosePairs[i][1]) then
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
break
end
end
if r == autoclosePairs[i][1] then
if r == charAt(autoclosePairs[i], 1) then
local curLine = v.Buf:Line(v.Cursor.Y)
if v.Cursor.X == #curLine or not IsWordChar(curLine[v.Cursor.X+1]) then
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
-- 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, autoclosePairs[i][2])
v.Buf:Insert(-v.Cursor.Loc, charAt(autoclosePairs[i], 2))
break
end
end
@@ -49,12 +51,12 @@ function onInsertEnter()
local v = CurView()
local curLine = v.Buf:Line(v.Cursor.Y)
local lastLine = v.Buf:Line(v.Cursor.Y-1)
local curRune = lastLine[#lastLine]
local nextRune = curLine[1]
local curRune = charAt(lastLine, #lastLine)
local nextRune = charAt(curLine, 1)
for i = 1, #autoNewlinePairs do
if curRune == autoNewlinePairs[i][1] then
if nextRune == autoNewlinePairs[i][2] then
if curRune == charAt(autoNewlinePairs[i], 1) then
if nextRune == charAt(autoNewlinePairs[i], 2) then
v:InsertTab()
v.Buf:Insert(-v.Cursor.Loc, "\n")
end
@@ -71,7 +73,7 @@ function onBackspace()
for i = 1, #autoclosePairs do
local curLine = v.Buf:Line(v.Cursor.Y)
if curLine[v.Cursor.X+1] == autoclosePairs[i][2] then
if charAt(curLine, v.Cursor.X+1) == charAt(autoclosePairs[i], 2) then
v:Delete()
end
end