Skip to content

Commit c895461

Browse files
Allow multi-cursor to work properly for autoclose plugin
1 parent 815ca0b commit c895461

File tree

1 file changed

+39
-25
lines changed

1 file changed

+39
-25
lines changed

runtime/plugins/autoclose/autoclose.lua

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,36 @@ function charAt(str, i)
1010
return uutil.RuneAt(str, i-1)
1111
end
1212

13+
function preInsertNewlineAct(bp)
14+
local curLine = bp.Buf:Line(bp.Cursor.Y)
15+
local curRune = charAt(curLine, bp.Cursor.X)
16+
local nextRune = charAt(curLine, bp.Cursor.X+1)
17+
local ws = uutil.GetLeadingWhitespace(curLine)
18+
19+
for j = 1, #autoNewlinePairs do
20+
if curRune == charAt(autoNewlinePairs[j], 1) then
21+
if nextRune == charAt(autoNewlinePairs[j], 2) then
22+
bp.Buf:Insert(-bp.Cursor.Loc, "\n" .. ws)
23+
bp:StartOfLine()
24+
bp:CursorLeft()
25+
bp:InsertNewline()
26+
bp:InsertTab()
27+
return true
28+
end
29+
end
30+
end
31+
return false
32+
end
33+
34+
function preBackspaceAct(bp)
35+
for i = 1, #autoclosePairs do
36+
local curLine = bp.Buf:Line(bp.Cursor.Y)
37+
if charAt(curLine, bp.Cursor.X+1) == charAt(autoclosePairs[i], 2) and charAt(curLine, bp.Cursor.X) == charAt(autoclosePairs[i], 1) then
38+
bp:Delete()
39+
end
40+
end
41+
end
42+
1343
function onRune(bp, r)
1444
for i = 1, #autoclosePairs do
1545
if r == charAt(autoclosePairs[i], 2) then
@@ -38,38 +68,22 @@ function onRune(bp, r)
3868
end
3969
end
4070
end
41-
return true
4271
end
4372

4473
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)
49-
50-
for i = 1, #autoNewlinePairs do
51-
if curRune == charAt(autoNewlinePairs[i], 1) then
52-
if nextRune == charAt(autoNewlinePairs[i], 2) then
53-
bp.Buf:Insert(-bp.Cursor.Loc, "\n" .. ws)
54-
bp:StartOfLine()
55-
bp:CursorLeft()
56-
bp:InsertNewline()
57-
bp:InsertTab()
58-
return false
59-
end
74+
local activeCursorNum = bp.Buf:GetActiveCursor().Num
75+
local inserted = false
76+
for i = 1,#bp.Buf:getCursors() do
77+
bp.Cursor = bp.Buf:GetCursor(i-1)
78+
if preInsertNewlineAct(bp) then
79+
inserted = true
6080
end
6181
end
62-
63-
return true
82+
bp.Cursor = bp.Buf:GetCursor(activeCursorNum)
83+
return not inserted
6484
end
6585

6686
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
70-
bp:Delete()
71-
end
72-
end
73-
87+
preBackspaceAct(bp)
7488
return true
7589
end

0 commit comments

Comments
 (0)