Skip to content

Commit 5e5891b

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

File tree

1 file changed

+48
-24
lines changed

1 file changed

+48
-24
lines changed

runtime/plugins/autoclose/autoclose.lua

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,39 @@ 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
28+
end
29+
end
30+
end
31+
32+
bp:InsertNewline()
33+
return
34+
end
35+
36+
function preBackspaceAct(bp)
37+
for i = 1, #autoclosePairs do
38+
local curLine = bp.Buf:Line(bp.Cursor.Y)
39+
if charAt(curLine, bp.Cursor.X+1) == charAt(autoclosePairs[i], 2) and charAt(curLine, bp.Cursor.X) == charAt(autoclosePairs[i], 1) then
40+
bp:Delete()
41+
return
42+
end
43+
end
44+
end
45+
1346
function onRune(bp, r)
1447
for i = 1, #autoclosePairs do
1548
if r == charAt(autoclosePairs[i], 2) then
@@ -38,38 +71,29 @@ function onRune(bp, r)
3871
end
3972
end
4073
end
41-
return true
4274
end
4375

4476
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
60-
end
77+
local activeCursorNum = bp.Buf:GetActiveCursor().Num
78+
local inserted = false
79+
for i = 1,#bp.Buf:getCursors() do
80+
bp.Cursor = bp.Buf:GetCursor(i-1)
81+
preInsertNewlineAct(bp)
6182
end
83+
bp.Cursor = bp.Buf:GetCursor(activeCursorNum)
6284

63-
return true
85+
return false
6486
end
6587

6688
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
89+
local activeCursorNum = bp.Buf:GetActiveCursor().Num
90+
local inserted = false
91+
for i = 1,#bp.Buf:getCursors() do
92+
bp.Cursor = bp.Buf:GetCursor(i-1)
93+
preBackspaceAct(bp)
94+
bp:Backspace()
7295
end
96+
bp.Cursor = bp.Buf:GetCursor(activeCursorNum)
7397

74-
return true
98+
return false
7599
end

0 commit comments

Comments
 (0)