-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHistory.lua
191 lines (153 loc) · 6.61 KB
/
History.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
local micro = import("micro")
local config = import("micro/config")
local buffer = import("micro/buffer")
local filepath = import("path/filepath")
local os = import("os")
local fmt = import('fmt')
local OmniCursorHistory = {}
local OmniCursorReverseFilePathMap = {}
local OmniCursorFilePathMap = {}
local OmniCursorIndices =
{
StartIndex = 0,
EndIndex = 0,
CurrentIndex = 0,
}
package.path = fmt.Sprintf('%s;%s/plug/MicroOmni/?.lua', package.path, config.ConfigDir)
local Common = require("Common")
local Self = {}
local function CopyLoc(loc)
return buffer.Loc(loc.X, loc.Y)
end
function Self.RecordCursorHistory()
if micro.CurPane() == nil or micro.CurPane().Cursor == nil or micro.CurPane().Buf == nil then
return
end
local currentCursorLoc = micro.CurPane().Cursor.Loc
local bufPath = micro.CurPane().Buf.AbsPath
local currentHistorySize = #OmniCursorHistory
if bufPath == nil or bufPath == "" then
return
end
-- Check bufPath exists and is a file
if not Common.path_exists(bufPath) or Common.IsPathDir(bufPath) then
return
end
-- micro.Log("currentCursorLoc: ", currentCursorLoc.X, ", ", currentCursorLoc.Y)
-- micro.Log("bufPath: ", bufPath)
-- micro.Log("currentHistorySize: ", currentHistorySize)
-- If we haven't see this path
if OmniCursorReverseFilePathMap[bufPath] == nil then
OmniCursorFilePathMap[#OmniCursorFilePathMap + 1] = bufPath
OmniCursorReverseFilePathMap[bufPath] = #OmniCursorFilePathMap
end
-- If first entry, just append
if currentHistorySize == 0 then
-- Append current cursor and return
OmniCursorHistory[1] =
{
FileId = OmniCursorReverseFilePathMap[bufPath],
CursorLoc = CopyLoc(currentCursorLoc)
}
OmniCursorIndices.StartIndex = 1
OmniCursorIndices.EndIndex = 1
OmniCursorIndices.CurrentIndex = 1
return
end
local currentHistory = OmniCursorHistory[OmniCursorIndices.CurrentIndex]
-- If difference is too less, then just leave it
local lineDiff = Common.OmniHistoryLineDiff
local timeTravelling = false
if OmniCursorIndices.CurrentIndex < OmniCursorIndices.EndIndex then
lineDiff = lineDiff * Common.OmniHistoryTimeTravelMulti
timeTravelling = true
end
if currentHistory.FileId == OmniCursorReverseFilePathMap[bufPath] and
math.abs(currentHistory.CursorLoc.Y - currentCursorLoc.Y) < lineDiff then
if timeTravelling then
micro.InfoBar():Message("Cursor time travel (anchor lines ",
math.max(currentHistory.CursorLoc.Y-lineDiff+1, 1), "-",
currentHistory.CursorLoc.Y+lineDiff+1,"): ",
OmniCursorIndices.CurrentIndex, "/", OmniCursorIndices.EndIndex)
end
-- Just update X if on the same line
if currentHistory.CursorLoc.Y == currentCursorLoc.Y then
-- micro.Log("currentHistory.CursorLoc.X: ", currentHistory.CursorLoc.X)
-- micro.Log("currentCursorLoc.X: ", currentCursorLoc.X)
OmniCursorHistory[OmniCursorIndices.CurrentIndex].CursorLoc = CopyLoc(currentCursorLoc)
end
return
end
if timeTravelling then
micro.InfoBar():Message("New cursor history branch created")
end
OmniCursorHistory[OmniCursorIndices.CurrentIndex + 1] =
{
FileId = OmniCursorReverseFilePathMap[bufPath],
CursorLoc = CopyLoc(currentCursorLoc)
}
OmniCursorIndices.CurrentIndex = OmniCursorIndices.CurrentIndex + 1
-- If we are not at the end of the history, we will need to remove the rest of the history until the end
if OmniCursorIndices.EndIndex > OmniCursorIndices.CurrentIndex then
for i = OmniCursorIndices.CurrentIndex + 1, OmniCursorIndices.EndIndex do
OmniCursorHistory[i] = nil
end
end
OmniCursorIndices.EndIndex = OmniCursorIndices.CurrentIndex
currentHistorySize = #OmniCursorHistory
-- Remove the first entry if we have more than 256 entries. Just to keep it small
if currentHistorySize > 256 then
OmniCursorHistory[OmniCursorIndices.StartIndex] = nil
OmniCursorIndices.StartIndex = OmniCursorIndices.StartIndex + 1
end
-- Debug log printing the whole cursor history
-- for i = OmniCursorIndices.StartIndex, OmniCursorIndices.EndIndex do
-- if i == OmniCursorIndices.CurrentIndex then
-- micro.Log("Current Index")
-- end
--
-- micro.Log( "Cursor History at ", i, ": ", OmniCursorFilePathMap[OmniCursorHistory[i].FileId],
-- ", ", OmniCursorHistory[i].CursorLoc.X, ", ", OmniCursorHistory[i].CursorLoc.Y)
-- end
end
local function GoToHistoryEntry(bp, entry)
micro.Log("GoToHistoryEntry called")
micro.Log( "Goto Entry: ", OmniCursorFilePathMap[entry.FileId],
", ", entry.CursorLoc.X, ", ", entry.CursorLoc.Y)
local entryFilePath = OmniCursorFilePathMap[entry.FileId]
-- micro.Log("os.Getwd():", os.Getwd())
local wd, err = os.Getwd()
if err == nil then
local relPath, relErr = filepath.Rel(wd, entryFilePath)
if relErr == nil and relPath ~= nil then
entryFilePath = relPath
end
end
micro.Log("entryFilePath:", entryFilePath)
-- micro.Log("We have ", #micro.Tabs().List, " tabs")
if not Common.OpenPaneIfExist(entryFilePath) then
Common.HandleOpenFile(entryFilePath, bp, "1", false)
end
micro.CurPane().Cursor:ResetSelection()
micro.CurPane().Cursor:GotoLoc(Common.LocBoundCheck(micro.CurPane().Buf, entry.CursorLoc))
micro.CurPane():Relocate()
end
function Self.GoToPreviousHistory(bp)
if #OmniCursorHistory == 0 or OmniCursorIndices.CurrentIndex <= OmniCursorIndices.StartIndex then
return
end
OmniCursorIndices.CurrentIndex = OmniCursorIndices.CurrentIndex - 1;
micro.InfoBar():Message("Going to previous history at index ", OmniCursorIndices.CurrentIndex,
"/", OmniCursorIndices.EndIndex)
GoToHistoryEntry(bp, OmniCursorHistory[OmniCursorIndices.CurrentIndex])
end
function Self.GoToNextHistory(bp)
if #OmniCursorHistory == 0 or OmniCursorIndices.CurrentIndex >= OmniCursorIndices.EndIndex then
return
end
OmniCursorIndices.CurrentIndex = OmniCursorIndices.CurrentIndex + 1;
micro.InfoBar():Message("Going to next history at index ", OmniCursorIndices.CurrentIndex,
"/", OmniCursorIndices.EndIndex)
GoToHistoryEntry(bp, OmniCursorHistory[OmniCursorIndices.CurrentIndex])
end
return Self