-
Notifications
You must be signed in to change notification settings - Fork 0
/
Minimap.lua
415 lines (345 loc) · 14.4 KB
/
Minimap.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
local micro = import("micro")
local config = import("micro/config")
local util = import("micro/util")
local buffer = import("micro/buffer")
local fmt = import('fmt')
package.path = fmt.Sprintf('%s;%s/plug/MicroOmni/?.lua', package.path, config.ConfigDir)
local Common = require("Common")
local Self = {}
local OmniMinimapTargetPanes = {}
local OmniMinimapPanes = {}
local OmniMinimapRecords = {}
local function GetIndentLevel(str, tabSize)
local indentLevel = 0
local continuousSpace = 0
local nonWhiteSpaceChar = nil
for char in str:gmatch"." do
if char == " " then
continuousSpace = continuousSpace + 1
if continuousSpace == tabSize then
indentLevel = indentLevel + 1
continuousSpace = 0
end
elseif char == "\t" then
continuousSpace = 0
indentLevel = indentLevel + 1
else
nonWhiteSpaceChar = char
break
end
end
return indentLevel, nonWhiteSpaceChar
end
local function RemoveMinimapIfExist(targetBp)
for i, val in ipairs(OmniMinimapTargetPanes) do
if targetBp == val then
OmniMinimapPanes[i]:Quit()
table.remove(OmniMinimapTargetPanes, i)
table.remove(OmniMinimapPanes, i)
table.remove(OmniMinimapRecords, i)
return true
end
end
for i, val in ipairs(OmniMinimapPanes) do
if targetBp == val then
OmniMinimapPanes[i]:Quit()
table.remove(OmniMinimapTargetPanes, i)
table.remove(OmniMinimapPanes, i)
table.remove(OmniMinimapRecords, i)
return true
end
end
return false
end
function Self.OmniMinimap(bp)
if RemoveMinimapIfExist(bp) then
return
end
-- micro.InfoBar():Message("bp.buf.AbsPath: ", bp.buf.AbsPath)
local numOfLines = bp.Buf:LinesNum()
local linesRecords = {}
local indentCount = {}
-- Settings
local indentCutoff = Common.OmniMinimapMaxIndent
local showSubsequentLines = Common.OmniMinimapContextNumLines
local minDist = Common.OmniMinimapMinDistance
local minimapShowLength = Common.OmniMinimapMaxColumns
-- States
local lastIndent = 9999
local forceOutput = false
local subsequentLinesShowed = 0
local lastOutputLine = -99
-- Read the bp buffer
for i = 0, numOfLines - 1 do
local currentLineBytes = bp.Buf:LineBytes(i)
local currentLineStr = util.String(currentLineBytes)
local currentIndentLevel, firstNonWhiteSpaceChar =
GetIndentLevel(currentLineStr, bp.Buf.Settings["tabsize"])
-- Get the indentation level of each line
linesRecords[i] = {}
linesRecords[i].indentLevel = currentIndentLevel
linesRecords[i].isEmpty = (firstNonWhiteSpaceChar == nil and {true} or {false})[1]
-- Simulate if this line will be output or not
if not linesRecords[i].isEmpty then
local canOutput = false
if linesRecords[i].indentLevel == lastIndent and
subsequentLinesShowed < showSubsequentLines then
canOutput = true
subsequentLinesShowed = subsequentLinesShowed + 1
else
subsequentLinesShowed = 0
end
if not canOutput and
(forceOutput or
(linesRecords[i].indentLevel ~= lastIndent and
i - lastOutputLine > minDist)) then
canOutput = true
subsequentLinesShowed = 1
end
-- micro.Log("canOutput for line", i, "is", canOutput)
if canOutput then
lastOutputLine = i
lastIndent = linesRecords[i].indentLevel
-- micro.Log("linesRecords[i].indentLevel", linesRecords[i].indentLevel)
if indentCount[linesRecords[i].indentLevel] == nil then
indentCount[linesRecords[i].indentLevel] = 1
else
indentCount[linesRecords[i].indentLevel] = indentCount[linesRecords[i].indentLevel] + 1
end
-- micro.Log("indentCount[linesRecords[i].indentLevel]", indentCount[linesRecords[i].indentLevel])
end
forceOutput = false
else
subsequentLinesShowed = 0
forceOutput = true
end
-- micro.Log( "Line", i, "indent level:", linesRecords[i].indentLevel,
-- "isEmpty", linesRecords[i].isEmpty)
end
-- Allocate number of lines each indent level can output to minimap
local minimapMaxLines = Common.OmniMinimapTargetNumLines
local indentBudget = {}
local firstIndent = -1
for i = 0, indentCutoff do
if indentCount[i] ~= nil and indentCount[i] ~= 0 then
if indentCount[i] > minimapMaxLines and firstIndent == -1 then
indentBudget[i] = indentCount[i]
break
else
if indentCount[i] > minimapMaxLines then
indentBudget[i] = minimapMaxLines
break
else
indentBudget[i] = indentCount[i]
minimapMaxLines = minimapMaxLines - indentCount[i]
end
end
if firstIndent == -1 then
firstIndent = i
end
end
end
for i = 0, indentCutoff do
micro.Log("i:", i)
if indentCount[i] ~= nil then
micro.Log("indentCount[", i, "]:", indentCount[i])
end
if indentBudget[i] ~= nil then
micro.Log("indentBudget[", i, "]:", indentBudget[i])
end
end
-- States
-- This holds line numbers
local minimapRecords = {}
local outputLines = {}
lastIndent = 9999
forceOutput = false
subsequentLinesShowed = 0
local lastShowedLine = -99
-- Parse and output lines for minimap
for i = 0, numOfLines - 1 do
local curLineNum = i + 1
local curLineRecord = linesRecords[i]
local skipFarEnough = false
local showFarEnough = false
if #minimapRecords == 0 or
curLineNum - minimapRecords[#minimapRecords] > minDist then
skipFarEnough = true
end
if #minimapRecords == 0 or
curLineNum - lastShowedLine > minDist then
showFarEnough = true
end
local indentChange = false
if #minimapRecords == 0 or curLineRecord.indentLevel ~= lastIndent then
indentChange = true
end
-- micro.Log( "Line", curLineNum - 1, "farEnough:", farEnough, "indentChange",
-- indentChange, "lastIndent", lastIndent, "forceOutput", forceOutput,
-- "curLineRecord.indentLevel", curLineRecord.indentLevel,
-- "curLineRecord.isEmpty", curLineRecord.isEmpty)
-- if indentBudget[curLineRecord.indentLevel] ~= nil then
-- micro.Log("indentBudget[", curLineRecord.indentLevel, "]:", indentBudget[curLineRecord.indentLevel])
-- end
local writeLine = false
local writeSkip = false
-- Write line to minimap
if not curLineRecord.isEmpty and
indentBudget[curLineRecord.indentLevel] ~= nil and
indentBudget[curLineRecord.indentLevel] > 0 then
-- If last line has same indent level and within subsequent showable distance, show it
if #minimapRecords ~= 0 and
curLineNum - minimapRecords[#minimapRecords] == 1 and
not indentChange and
subsequentLinesShowed < showSubsequentLines then
subsequentLinesShowed = subsequentLinesShowed + 1
writeLine = true
else
subsequentLinesShowed = 0
end
-- If we force output or it is far enough with change in indent, show it
if not writeLine and (forceOutput or (showFarEnough and indentChange)) then
writeLine = true
subsequentLinesShowed = 1
end
end
-- Write ... to minimap every certain distance
if not writeLine and skipFarEnough then
writeSkip = true
end
if writeLine then
lastIndent = curLineRecord.indentLevel
lastShowedLine = curLineNum
local currentLineBytes = bp.Buf:LineBytes(curLineNum - 1)
local currentLineStr = util.String(currentLineBytes)
-- micro.Log("currentLineStr[", curLineNum, "]:", currentLineStr)
-- currentLineStr = string.gsub(currentLineStr, "[\n\r]", "")
if #currentLineStr > minimapShowLength then
table.insert( outputLines,
string.format( "%-5d %s",
curLineNum,
currentLineStr:sub(1, minimapShowLength).."..."))
else
table.insert(outputLines, string.format("%-5d %s", curLineNum, currentLineStr))
end
local processedLine, _ = string.gsub(outputLines[#outputLines], "\\\"", "")
processedLine, _ = string.gsub(processedLine, "\\\'", "")
local _, doubleQuoteCount = string.gsub(processedLine, "\"", "")
local _, singleQuoteCount = string.gsub(processedLine, "\'", "")
local _, blockCommentCount = string.gsub(processedLine, "/%*", "")
local appends = ""
if singleQuoteCount % 2 == 1 then
appends = appends.."\'"
end
if doubleQuoteCount % 2 == 1 then
appends = appends.."\""
end
if blockCommentCount > 0 then
appends = appends.."*/"
end
if #appends ~= 0 then
outputLines[#outputLines] = outputLines[#outputLines]..appends
end
indentBudget[curLineRecord.indentLevel] = indentBudget[curLineRecord.indentLevel] - 1
table.insert(minimapRecords, curLineNum)
elseif writeSkip then
table.insert(outputLines, string.format("%-5d %s", curLineNum, "..."))
table.insert(minimapRecords, curLineNum)
end
forceOutput = false
-- If it is an empty line, we force minimap output next time
if curLineRecord.isEmpty then
forceOutput = true
end
end
-- Output minimap
local minimapBuf, err = buffer.NewBuffer(table.concat(outputLines, "\n"), "minimap"..#OmniMinimapTargetPanes)
if err ~= nil then
micro.InfoBar():Error(err)
return
end
minimapBuf.Type.Readonly = true
minimapBuf:SetOptionNative("ruler", false)
local minimapPane = micro.CurPane():VSplitIndex(minimapBuf, true)
table.insert(OmniMinimapTargetPanes, bp)
table.insert(OmniMinimapPanes, minimapPane)
table.insert(OmniMinimapRecords, minimapRecords)
minimapPane:SetLocalCmd({"filetype", bp.Buf.Settings["filetype"]})
-- Set focus back
bp:Tab():SetActive(bp:Tab():GetPane(bp:ID()))
Self.UpdateMinimapView()
end
function Self.CheckAndQuitMinimap(targetBp)
if targetBp == nil then
return
end
-- If one of the target pane is trying to quit, we quit the minimap first.
-- Then remove the records
for i, val in ipairs(OmniMinimapTargetPanes) do
if targetBp == val then
OmniMinimapPanes[i]:Quit()
table.remove(OmniMinimapTargetPanes, i)
table.remove(OmniMinimapPanes, i)
table.remove(OmniMinimapRecords, i)
return
end
end
-- If one of the minimap is trying to quit, just remove records
for i, val in ipairs(OmniMinimapPanes) do
if targetBp == val then
table.remove(OmniMinimapTargetPanes, i)
table.remove(OmniMinimapPanes, i)
table.remove(OmniMinimapRecords, i)
return
end
end
end
function Self.UpdateMinimapView()
if micro.CurPane() == nil then
return
end
local cursorLoc = micro.CurPane().Cursor.Loc
for i, val in ipairs(OmniMinimapTargetPanes) do
if micro.CurPane() == val then
local targetMinimapLine = 1
for j = 1, #OmniMinimapRecords[i] do
if OmniMinimapRecords[i][j] > cursorLoc.Y + 1 then
break
end
targetMinimapLine = j
end
OmniMinimapPanes[i].Cursor:ResetSelection()
OmniMinimapPanes[i].Buf:ClearCursors()
OmniMinimapPanes[i].Cursor:GotoLoc(Common.LocBoundCheck(OmniMinimapPanes[i].Buf,
buffer.Loc(0, targetMinimapLine - 1)))
OmniMinimapPanes[i].Cursor:SelectWord()
local minimapHeight = OmniMinimapPanes[i]:GetView().Height - 2
OmniMinimapPanes[i]:GetView().StartLine.Line = targetMinimapLine - minimapHeight / 2
if OmniMinimapPanes[i]:GetView().StartLine.Line < 0 then
OmniMinimapPanes[i]:GetView().StartLine.Line = 0
end
return
end
end
if Common.OmniMinimapScrollContent == false then
return
end
for i, val in ipairs(OmniMinimapPanes) do
if micro.CurPane() == val then
if cursorLoc.Y + 1 > #OmniMinimapRecords[i] then
return
end
OmniMinimapTargetPanes[i].Cursor:GotoLoc(
Common.LocBoundCheck( OmniMinimapTargetPanes[i].Buf,
buffer.Loc(0, OmniMinimapRecords[i][cursorLoc.Y + 1] - 1)))
local viewHeight = OmniMinimapTargetPanes[i]:GetView().Height - 2
OmniMinimapTargetPanes[i]:GetView().StartLine.Line =
OmniMinimapTargetPanes[i].Cursor.Loc.Y - viewHeight / 2
if OmniMinimapTargetPanes[i]:GetView().StartLine.Line < 0 then
OmniMinimapTargetPanes[i]:GetView().StartLine.Line = 0
end
return
end
end
end
return Self