Skip to content

Commit 9176508

Browse files
authored
Merge pull request zyedidia#3291 from dmaluka/diffgutter-cleanup
Diffgutter: simplify + fix race
2 parents b70f0eb + 5a159ce commit 9176508

File tree

3 files changed

+19
-31
lines changed

3 files changed

+19
-31
lines changed

internal/action/actions.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,9 +1480,7 @@ func (h *BufPane) HalfPageDown() bool {
14801480
func (h *BufPane) ToggleDiffGutter() bool {
14811481
if !h.Buf.Settings["diffgutter"].(bool) {
14821482
h.Buf.Settings["diffgutter"] = true
1483-
h.Buf.UpdateDiff(func(synchronous bool) {
1484-
screen.Redraw()
1485-
})
1483+
h.Buf.UpdateDiff()
14861484
InfoBar.Message("Enabled diff gutter")
14871485
} else {
14881486
h.Buf.Settings["diffgutter"] = false

internal/buffer/buffer.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,7 +1280,7 @@ func (b *Buffer) Write(bytes []byte) (n int, err error) {
12801280
return len(bytes), nil
12811281
}
12821282

1283-
func (b *Buffer) updateDiffSync() {
1283+
func (b *Buffer) updateDiff(synchronous bool) {
12841284
b.diffLock.Lock()
12851285
defer b.diffLock.Unlock()
12861286

@@ -1291,7 +1291,16 @@ func (b *Buffer) updateDiffSync() {
12911291
}
12921292

12931293
differ := dmp.New()
1294-
baseRunes, bufferRunes, _ := differ.DiffLinesToRunes(string(b.diffBase), string(b.Bytes()))
1294+
1295+
if !synchronous {
1296+
b.Lock()
1297+
}
1298+
bytes := b.Bytes()
1299+
if !synchronous {
1300+
b.Unlock()
1301+
}
1302+
1303+
baseRunes, bufferRunes, _ := differ.DiffLinesToRunes(string(b.diffBase), string(bytes))
12951304
diffs := differ.DiffMainRunes(baseRunes, bufferRunes, false)
12961305
lineN := 0
12971306

@@ -1320,13 +1329,9 @@ func (b *Buffer) updateDiffSync() {
13201329

13211330
// UpdateDiff computes the diff between the diff base and the buffer content.
13221331
// The update may be performed synchronously or asynchronously.
1323-
// UpdateDiff calls the supplied callback when the update is complete.
1324-
// The argument passed to the callback is set to true if and only if
1325-
// the update was performed synchronously.
13261332
// If an asynchronous update is already pending when UpdateDiff is called,
1327-
// UpdateDiff does not schedule another update, in which case the callback
1328-
// is not called.
1329-
func (b *Buffer) UpdateDiff(callback func(bool)) {
1333+
// UpdateDiff does not schedule another update.
1334+
func (b *Buffer) UpdateDiff() {
13301335
if b.updateDiffTimer != nil {
13311336
return
13321337
}
@@ -1337,20 +1342,18 @@ func (b *Buffer) UpdateDiff(callback func(bool)) {
13371342
}
13381343

13391344
if lineCount < 1000 {
1340-
b.updateDiffSync()
1341-
callback(true)
1345+
b.updateDiff(true)
13421346
} else if lineCount < 30000 {
13431347
b.updateDiffTimer = time.AfterFunc(500*time.Millisecond, func() {
13441348
b.updateDiffTimer = nil
1345-
b.updateDiffSync()
1346-
callback(false)
1349+
b.updateDiff(false)
1350+
screen.Redraw()
13471351
})
13481352
} else {
13491353
// Don't compute diffs for very large files
13501354
b.diffLock.Lock()
13511355
b.diff = make(map[int]DiffStatus)
13521356
b.diffLock.Unlock()
1353-
callback(true)
13541357
}
13551358
}
13561359

@@ -1362,9 +1365,7 @@ func (b *Buffer) SetDiffBase(diffBase []byte) {
13621365
} else {
13631366
b.diffBaseLineCount = strings.Count(string(diffBase), "\n")
13641367
}
1365-
b.UpdateDiff(func(synchronous bool) {
1366-
screen.Redraw()
1367-
})
1368+
b.UpdateDiff()
13681369
}
13691370

13701371
// DiffStatus returns the diff status for a line in the buffer

internal/display/bufwindow.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -384,18 +384,7 @@ func (w *BufWindow) displayBuffer() {
384384

385385
if b.ModifiedThisFrame {
386386
if b.Settings["diffgutter"].(bool) {
387-
b.UpdateDiff(func(synchronous bool) {
388-
// If the diff was updated asynchronously, the outer call to
389-
// displayBuffer might already be completed and we need to
390-
// schedule a redraw in order to display the new diff.
391-
// Note that this cannot lead to an infinite recursion
392-
// because the modifications were cleared above so there won't
393-
// be another call to UpdateDiff when displayBuffer is called
394-
// during the redraw.
395-
if !synchronous {
396-
screen.Redraw()
397-
}
398-
})
387+
b.UpdateDiff()
399388
}
400389
b.ModifiedThisFrame = false
401390
}

0 commit comments

Comments
 (0)