-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiff.lua
More file actions
421 lines (350 loc) · 13.9 KB
/
Diff.lua
File metadata and controls
421 lines (350 loc) · 13.9 KB
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
416
417
418
419
420
421
local micro = import("micro")
local shell = import("micro/shell")
local buffer = import("micro/buffer")
local config = import("micro/config")
local fmt = import('fmt')
package.path = fmt.Sprintf('%s;%s/plug/?.lua', package.path, config.ConfigDir)
local Common = require("MicroOmni.Common")
local Self = {}
local OmniDiffPlusFile = true
local OmniDiffTargetPanes = {}
local OmniDiffDiffPanes = {}
local OmniDiffDiffPanesLineOffsets = {} -- Target start line = Diff start line + offset
local OmniDiffDiffPanesPersistent = {}
local function processDiffOutput(output)
local outputLines = {}
local currentLineIndex = 1
-- Split the output string by newline
for i = 1, #output do
local c = output:sub(i, i)
if c == "\r" then
if outputLines[currentLineIndex] ~= nil then
outputLines[currentLineIndex] = table.concat(outputLines[currentLineIndex])
end
currentLineIndex = currentLineIndex + 1
elseif c == "\n" then
if i > 1 and output:sub(i - 1, i - 1) ~= "\r" then
if outputLines[currentLineIndex] ~= nil then
outputLines[currentLineIndex] = table.concat(outputLines[currentLineIndex])
end
currentLineIndex = currentLineIndex + 1
end
else
if outputLines[currentLineIndex] == nil then
outputLines[currentLineIndex] = {}
end
table.insert(outputLines[currentLineIndex], c)
end
end
-- micro.Log("outputLines: ", outputLines)
-- Get the index of the first diff
local outputLinesCount = currentLineIndex
local firstDiffIndex = -1
for i = 1, outputLinesCount do
local curLine
if outputLines[i] ~= nil then
curLine = outputLines[i]
micro.Log("MicroOmni.Diff - Trying to find first diff: ", curLine)
if #curLine > 2 then
micro.Log("MicroOmni.Diff - Checking[1]: ", curLine:sub(1, 1):byte())
micro.Log("MicroOmni.Diff - Checking[2]: ", curLine:sub(2, 2):byte())
end
if #curLine > 2 and curLine:sub(1, 2) == "@@" then
firstDiffIndex = i
break
end
end
end
micro.Log("MicroOmni.Diff - firstDiffIndex: ", firstDiffIndex)
-- Return empty string if no diff
if firstDiffIndex <= 0 then
micro.Log("MicroOmni.Diff - No diff found")
return ""
end
-- Populate the return lines
local returnLines = {}
for i = firstDiffIndex, outputLinesCount do
local curLine
if outputLines[i] ~= nil then
curLine = outputLines[i]
-- micro.Log("Processing: ", curLine)
local targetLineIndex = nil
if #curLine > 2 and curLine:sub(1, 2) == "@@" then
if OmniDiffPlusFile then
targetLineIndex = curLine:match("%+%d+")
else
targetLineIndex = curLine:match("%-%d+")
end
targetLineIndex = tonumber(targetLineIndex:sub(2, #targetLineIndex))
end
-- micro.Log("targetLineIndex: ", targetLineIndex)
-- If we are at new diff
if targetLineIndex ~= nil then
-- Add empty lines until we reach the hunk
while #returnLines < targetLineIndex - 2 do
table.insert(returnLines, "")
end
-- Add diff header
if #returnLines == targetLineIndex - 1 then
returnLines[#returnLines] = curLine
elseif #returnLines == targetLineIndex - 2 then
table.insert(returnLines, curLine)
-- micro.Log("Appending diff header")
end
else
-- Otherwise just append the diffs
table.insert(returnLines, curLine)
-- micro.Log("Appending diff content")
end
end
end
return returnLines
end
local function OnDiffFinishCallback(resp, cancelled)
if cancelled then
return
end
local tabSpecified = false
local tabIndex = -1
local splitIndex = 1
if #resp >= 4 and resp:sub(1, 4) == "tab:" then
tabSpecified = true
local respTokens = {}
for s in string.gmatch(resp, "[^:]+") do
table.insert(respTokens, s)
end
if #respTokens == 1 then
micro.InfoBar():Error("tab expecting index, like tab:<tab index>")
return
elseif #respTokens == 2 then
tabIndex = tonumber(respTokens[2])
elseif #respTokens == 3 then
tabIndex = tonumber(respTokens[2])
splitIndex = tonumber(respTokens[3])
end
if tabIndex == nil or splitIndex == nil then
micro.InfoBar():Error("Failed to parse tab index")
return
end
if respTokens[2]:sub(1, 1) == "-" or respTokens[2]:sub(1, 1) == "+" then
-- NOTE: micro.Tabs():Active() starts counting at 1
tabIndex = micro.Tabs():Active() + 1 + tabIndex
end
end
local respPath = resp
if tabSpecified then
if tabIndex <= 0 or tabIndex > #micro.Tabs().List then
micro.InfoBar():Error("tabIndex ", tabIndex, " out of bound for ", #micro.Tabs().List)
return
end
if splitIndex <= 0 or splitIndex > #micro.Tabs().List[tabIndex].Panes then
micro.InfoBar():Error("splitIndex ", splitIndex, " out of bound for ", #micro.Tabs().List[tabIndex].Panes)
return
end
if micro.Tabs().List[tabIndex].Panes[splitIndex] == nil then
micro.InfoBar():Error("micro.Tabs().List[", tabIndex, "].Panes[", splitIndex, "] is nil")
return
end
respPath = micro.Tabs().List[tabIndex].Panes[splitIndex].Buf.AbsPath
end
local minusFile
local plusFile
if OmniDiffPlusFile then
minusFile = respPath
plusFile = micro.CurPane().Buf.AbsPath
-- Create temp files if needed
if tabSpecified and micro.Tabs().List[tabIndex].Panes[splitIndex].Buf:Modified() then
local createdPath, success =
Common.CreateRuntimeFile( "./temp/minus.temp",
micro.Tabs().List[tabIndex].Panes[splitIndex].Buf:Bytes())
if not success then
return
end
minusFile = createdPath
end
if micro.CurPane().Buf:Modified() then
local createdPath, success =
Common.CreateRuntimeFile("./temp/plus.temp", micro.CurPane().Buf:Bytes())
if not success then
return
end
plusFile = createdPath
end
else
minusFile = micro.CurPane().Buf.AbsPath
plusFile = respPath
-- Create temp files if needed
if micro.CurPane().Buf:Modified() then
local createdPath, success =
Common.CreateRuntimeFile("./temp/minus.temp", micro.CurPane().Buf:Bytes())
if not success then
return
end
minusFile = createdPath
end
if tabSpecified and micro.Tabs().List[tabIndex].Panes[splitIndex].Buf:Modified() then
local createdPath, success =
Common.CreateRuntimeFile( "./temp/plus.temp",
micro.Tabs().List[tabIndex].Panes[splitIndex].Buf:Bytes())
if not success then
return
end
micro.Log("MicroOmni.Diff - Minus plus file success: ", createdPath)
plusFile = createdPath
end
end
if not Common.path_exists(plusFile) then
micro.InfoBar():Error("plusFile: ", minusFile, " does not exist")
return
end
if not Common.path_exists(minusFile) then
micro.InfoBar():Error("minusFile: ", minusFile, " does not exist")
return
end
micro.Log("MicroOmni.Diff - Running: ", "diff -U 5 \""..minusFile.."\" \""..plusFile.."\"")
local output, err = shell.RunCommand("diff -U 5 \""..minusFile.."\" \""..plusFile.."\"")
micro.Log("MicroOmni.Diff - output: ", output)
micro.Log("MicroOmni.Diff - err: ", err)
local processedDiffLines = processDiffOutput(output)
if err == nil or err:Error() == "exit status 1" or err:Error() == "exit status 2" then
local curPane = micro.CurPane()
local buf, bufErr = buffer.NewBuffer("", "diff"..#OmniDiffTargetPanes)
for i = 1, #processedDiffLines do
buf:Insert(buffer.Loc(0, i - 1), processedDiffLines[i] .. "\n")
end
if bufErr ~= nil then
micro.InfoBar():Error(bufErr)
return
end
local bp = micro.CurPane()
local diffPane = micro.CurPane():VSplitIndex(buf, not OmniDiffPlusFile)
table.insert(OmniDiffTargetPanes, curPane)
table.insert(OmniDiffDiffPanes, diffPane)
table.insert(OmniDiffDiffPanesLineOffsets, 0)
table.insert(OmniDiffDiffPanesPersistent, false)
micro.CurPane():SetLocalCmd({"filetype", "patch"})
-- Set focus back so that it is not scroll to top
bp:Tab():SetActive(bp:Tab():GetPane(bp:ID()))
else
micro.InfoBar():Error(err)
end
end
function Self.CheckAndQuitDiffView(targetBp)
if targetBp == nil then
return
end
-- If one of the target pane is trying to quit, we quit the diff view first if it is not persistent
-- Then remove the records
for i, val in ipairs(OmniDiffTargetPanes) do
if targetBp == val then
if not OmniDiffDiffPanesPersistent[i] then
OmniDiffDiffPanes[i]:Quit()
end
table.remove(OmniDiffTargetPanes, i)
table.remove(OmniDiffDiffPanes, i)
table.remove(OmniDiffDiffPanesLineOffsets, i)
table.remove(OmniDiffDiffPanesPersistent, i)
return
end
end
-- If one of the diff pane is trying to quit, just remove records
for i, val in ipairs(OmniDiffDiffPanes) do
if targetBp == val then
table.remove(OmniDiffTargetPanes, i)
table.remove(OmniDiffDiffPanes, i)
table.remove(OmniDiffDiffPanesLineOffsets, i)
table.remove(OmniDiffDiffPanesPersistent, i)
return
end
end
end
function Self.UpdateDiffView()
-- micro.InfoBar():Message("UpdateDiffCalled")
if micro.CurPane() == nil then
return nil
end
for i, val in ipairs(OmniDiffTargetPanes) do
if micro.CurPane() == val then
-- Target start line = Diff start line + offset
OmniDiffDiffPanes[i]:GetView().StartLine.Line =
micro.CurPane():GetView().StartLine.Line + OmniDiffDiffPanesLineOffsets[i]
-- OmniCenter(OmniDiffDiffPanes[i])
return OmniDiffDiffPanes[i]
end
end
for i, val in ipairs(OmniDiffDiffPanes) do
if micro.CurPane() == val then
-- Target start line = Diff start line + offset
OmniDiffTargetPanes[i]:GetView().StartLine.Line =
micro.CurPane():GetView().StartLine.Line - OmniDiffDiffPanesLineOffsets[i]
-- OmniCenter(OmniDiffTargetPanes[i])
return OmniDiffTargetPanes[i]
end
end
end
local function OnDiffPlusCallback(yes, cancelled)
if cancelled then
return
end
OmniDiffPlusFile = yes
micro.InfoBar():Prompt( "File to diff against (Use tab:[+/-]<tab index>[:<split index>] to diff other buffers) > ",
"",
"",
nil,
OnDiffFinishCallback)
end
function Self.OmniDiff(bp)
micro.InfoBar():YNPrompt("Is this plus file? (y/n/esc) > ", OnDiffPlusCallback)
end
function Self.OmniMapSideBySide(bp)
-- local curSplitId = bp:ID()
if #bp:Tab().Panes <= 1 then
micro.InfoBar():Error("Current tab has 1 or less split, cannot perform side by side scrolling")
return
end
local active = -1
for i = 1, #bp:Tab().Panes do
if bp:Tab().Panes[i] == bp then
active = i
break
end
end
local diffBp
if active + 1 > #bp:Tab().Panes then
diffBp = bp:Tab().Panes[1]
else
diffBp = bp:Tab().Panes[active + 1]
end
table.insert(OmniDiffTargetPanes, bp)
table.insert(OmniDiffDiffPanes, diffBp)
-- Target start line = Diff start line + offset
table.insert( OmniDiffDiffPanesLineOffsets,
diffBp:GetView().StartLine.Line - bp:GetView().StartLine.Line)
-- micro.Log("diffBp:GetView().StartLine.Line:", diffBp:GetView().StartLine.Line)
-- micro.Log("bp:GetView().StartLine.Line:", bp:GetView().StartLine.Line)
table.insert(OmniDiffDiffPanesPersistent, true)
micro.InfoBar():Message("Mapped scrolling to next pane")
end
function Self.OmniUnmapSideBySide(bp)
for i, val in ipairs(OmniDiffTargetPanes) do
if bp == val then
table.remove(OmniDiffTargetPanes, i)
table.remove(OmniDiffDiffPanes, i)
table.remove(OmniDiffDiffPanesLineOffsets, i)
table.remove(OmniDiffDiffPanesPersistent, i)
micro.InfoBar():Message("Unmapped scrolling")
return
end
end
for i, val in ipairs(OmniDiffDiffPanes) do
if bp == val then
table.remove(OmniDiffTargetPanes, i)
table.remove(OmniDiffDiffPanes, i)
table.remove(OmniDiffDiffPanesLineOffsets, i)
table.remove(OmniDiffDiffPanesPersistent, i)
micro.InfoBar():Message("Unmapped scrolling")
return
end
end
end
return Self