-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHighlight.lua
194 lines (155 loc) · 6.32 KB
/
Highlight.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
local micro = import("micro")
local buffer = import("micro/buffer")
local util = import("micro/util")
local config = import("micro/config")
local fmt = import('fmt')
package.path = fmt.Sprintf('%s;%s/plug/MicroOmni/?.lua', package.path, config.ConfigDir)
local Common = require("Common")
local Self = {}
local OmniFirstMultiCursorSpawned = false
local function OnTypingHighlight(msg)
if micro.CurPane() == nil or micro.CurPane().Buf == nil then return end
local bp = micro.CurPane()
bp.Buf.LastSearch = msg
bp.Buf.LastSearchRegex = true
bp.Buf.HighlightSearch = true
end
local function OnSubmitHighlightFind(msg, cancelled)
if micro.CurPane() == nil or micro.CurPane().Buf == nil or msg == nil or msg == "" then return end
local bp = micro.CurPane()
if cancelled then
bp.Buf.HighlightSearch = false
return
end
bp.Buf.LastSearch = msg
bp.Buf.LastSearchRegex = true
bp.Buf.HighlightSearch = true
local startFindLoc = buffer.Loc(0, 0)
local lastLineIndex = bp.Buf:LinesNum()
if lastLineIndex > 0 then
lastLineIndex = lastLineIndex - 1
else
micro.InfoBar():Message("None found and highlighted")
return
end
local lastLineLength = util.CharacterCountInString(bp.Buf:Line(lastLineIndex))
if lastLineLength ~= 0 then
lastLineLength = lastLineLength - 1
end
local endFindLoc = buffer.Loc(lastLineLength, lastLineIndex)
local foundCounter = 0
local currentLoc = buffer.Loc(startFindLoc.X, startFindLoc.Y)
local firstOccurrenceLoc = buffer.Loc(-1, -1)
while true do
local foundLocs, found, err = bp.Buf:FindNext( msg,
startFindLoc,
endFindLoc,
currentLoc,
true,
true)
if found == false or
err ~= nil or
foundCounter >= 1500 or
(foundLocs[2].X == firstOccurrenceLoc.X and foundLocs[2].Y == firstOccurrenceLoc.Y) then
break
end
currentLoc = buffer.Loc(foundLocs[2].X, foundLocs[2].Y)
foundCounter = foundCounter + 1
if foundCounter == 1 then
firstOccurrenceLoc = buffer.Loc(foundLocs[2].X, foundLocs[2].Y)
end
end
micro.InfoBar():Message(foundCounter, " found and highlighted. Do FindNext/FindPrevious to "..
"go to the occurrences")
end
function Self.OmniHighlightOnly(bp)
local selectionText = ""
if bp.Cursor:HasSelection() then
selectionText = bp.Cursor:GetSelection()
selectionText = util.String(selectionText)
end
micro.InfoBar():Prompt( "Highlight Then Find (regex) > ",
selectionText, "", OnTypingHighlight, OnSubmitHighlightFind)
end
local function PerformMultiCursor(bp, forceMove)
-- Check highlight
if not bp.Buf.HighlightSearch or bp.Buf.LastSearch == "" then
bp:SpawnMultiCursor()
return
end
local lastCursor = bp.Buf:GetCursor(bp.Buf:NumCursors() - 1)
local moveCursor = false
if Common.OmniCanUseAddCursor then
moveCursor = not lastCursor:HasSelection()
else
moveCursor = ((bp.Buf:NumCursors() == 1 and not OmniFirstMultiCursorSpawned) and {true} or {false})[1]
end
if forceMove then moveCursor = true end
local searchStart = nil
if moveCursor then
OmniFirstMultiCursorSpawned = true
bp:Deselect()
local currentLoc = lastCursor.Loc
-- searchStart = bp.Buf, buffer.Loc(currentLoc.X, currentLoc.Y)
-- searchStart = Common.LocBoundCheck(bp.Buf, buffer.Loc(currentLoc.X, currentLoc.Y - 1))
searchStart = Common.LocBoundCheck(bp.Buf, buffer.Loc(0, currentLoc.Y))
-- bp.Cursor:Deselect(false)
else
OmniFirstMultiCursorSpawned = false
if Common.OmniCanUseAddCursor then
searchStart = buffer.Loc(lastCursor.CurSelection[2].X, lastCursor.CurSelection[2].Y)
else
searchStart = buffer.Loc(lastCursor.Loc.X, lastCursor.Loc.Y)
end
end
local foundLocs, found, err = bp.Buf:FindNext( bp.Buf.LastSearch,
bp.Buf:Start(),
bp.Buf:End(),
searchStart,
true,
true)
if found == false or err ~= nil then
micro.InfoBar():Message("No occurrences found")
return
end
-- Spawn new cursor if we don't move the last cursor
if not moveCursor then
if Common.OmniCanUseAddCursor then
lastCursor = buffer.NewCursor(bp.Buf, buffer.Loc(0, 0))
else
if not bp:SpawnMultiCursorDown() then
if not bp:SpawnMultiCursorUp() then
micro.InfoBar():Error("Failed to spawn cursor...")
return
end
end
lastCursor = bp.Buf:GetCursor(bp.Buf:NumCursors() - 1)
end
end
if Common.OmniCanUseAddCursor then
lastCursor:SetSelectionStart(foundLocs[1])
lastCursor:SetSelectionEnd(foundLocs[2])
lastCursor.OrigSelection[1].X = lastCursor.CurSelection[1].X
lastCursor.OrigSelection[1].Y = lastCursor.CurSelection[1].Y
lastCursor.OrigSelection[2].X = lastCursor.CurSelection[2].X
lastCursor.OrigSelection[2].Y = lastCursor.CurSelection[2].Y
lastCursor.Loc.X = lastCursor.CurSelection[2].X
lastCursor.Loc.Y = lastCursor.CurSelection[2].Y
if not moveCursor then
bp.Buf:AddCursor(lastCursor)
end
else
lastCursor.Loc.X = foundLocs[2].X
lastCursor.Loc.Y = foundLocs[2].Y
end
bp.Buf:SetCurCursor(bp.Buf:NumCursors() - 1)
bp.Buf:MergeCursors()
bp:Relocate()
end
function Self.OmniSpawnCursorNextHighlight(bp)
PerformMultiCursor(bp, false)
end
function Self.OmniMoveLastCursorNextHighlight(bp)
PerformMultiCursor(bp, true)
end
return Self