Merge pull request #3291 from dmaluka/diffgutter-cleanup
Diffgutter: simplify + fix race
This commit is contained in:
commit
917650826a
3 changed files with 19 additions and 31 deletions
|
@ -1480,9 +1480,7 @@ func (h *BufPane) HalfPageDown() bool {
|
||||||
func (h *BufPane) ToggleDiffGutter() bool {
|
func (h *BufPane) ToggleDiffGutter() bool {
|
||||||
if !h.Buf.Settings["diffgutter"].(bool) {
|
if !h.Buf.Settings["diffgutter"].(bool) {
|
||||||
h.Buf.Settings["diffgutter"] = true
|
h.Buf.Settings["diffgutter"] = true
|
||||||
h.Buf.UpdateDiff(func(synchronous bool) {
|
h.Buf.UpdateDiff()
|
||||||
screen.Redraw()
|
|
||||||
})
|
|
||||||
InfoBar.Message("Enabled diff gutter")
|
InfoBar.Message("Enabled diff gutter")
|
||||||
} else {
|
} else {
|
||||||
h.Buf.Settings["diffgutter"] = false
|
h.Buf.Settings["diffgutter"] = false
|
||||||
|
|
|
@ -1280,7 +1280,7 @@ func (b *Buffer) Write(bytes []byte) (n int, err error) {
|
||||||
return len(bytes), nil
|
return len(bytes), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Buffer) updateDiffSync() {
|
func (b *Buffer) updateDiff(synchronous bool) {
|
||||||
b.diffLock.Lock()
|
b.diffLock.Lock()
|
||||||
defer b.diffLock.Unlock()
|
defer b.diffLock.Unlock()
|
||||||
|
|
||||||
|
@ -1291,7 +1291,16 @@ func (b *Buffer) updateDiffSync() {
|
||||||
}
|
}
|
||||||
|
|
||||||
differ := dmp.New()
|
differ := dmp.New()
|
||||||
baseRunes, bufferRunes, _ := differ.DiffLinesToRunes(string(b.diffBase), string(b.Bytes()))
|
|
||||||
|
if !synchronous {
|
||||||
|
b.Lock()
|
||||||
|
}
|
||||||
|
bytes := b.Bytes()
|
||||||
|
if !synchronous {
|
||||||
|
b.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
baseRunes, bufferRunes, _ := differ.DiffLinesToRunes(string(b.diffBase), string(bytes))
|
||||||
diffs := differ.DiffMainRunes(baseRunes, bufferRunes, false)
|
diffs := differ.DiffMainRunes(baseRunes, bufferRunes, false)
|
||||||
lineN := 0
|
lineN := 0
|
||||||
|
|
||||||
|
@ -1320,13 +1329,9 @@ func (b *Buffer) updateDiffSync() {
|
||||||
|
|
||||||
// UpdateDiff computes the diff between the diff base and the buffer content.
|
// UpdateDiff computes the diff between the diff base and the buffer content.
|
||||||
// The update may be performed synchronously or asynchronously.
|
// The update may be performed synchronously or asynchronously.
|
||||||
// UpdateDiff calls the supplied callback when the update is complete.
|
|
||||||
// The argument passed to the callback is set to true if and only if
|
|
||||||
// the update was performed synchronously.
|
|
||||||
// If an asynchronous update is already pending when UpdateDiff is called,
|
// If an asynchronous update is already pending when UpdateDiff is called,
|
||||||
// UpdateDiff does not schedule another update, in which case the callback
|
// UpdateDiff does not schedule another update.
|
||||||
// is not called.
|
func (b *Buffer) UpdateDiff() {
|
||||||
func (b *Buffer) UpdateDiff(callback func(bool)) {
|
|
||||||
if b.updateDiffTimer != nil {
|
if b.updateDiffTimer != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -1337,20 +1342,18 @@ func (b *Buffer) UpdateDiff(callback func(bool)) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if lineCount < 1000 {
|
if lineCount < 1000 {
|
||||||
b.updateDiffSync()
|
b.updateDiff(true)
|
||||||
callback(true)
|
|
||||||
} else if lineCount < 30000 {
|
} else if lineCount < 30000 {
|
||||||
b.updateDiffTimer = time.AfterFunc(500*time.Millisecond, func() {
|
b.updateDiffTimer = time.AfterFunc(500*time.Millisecond, func() {
|
||||||
b.updateDiffTimer = nil
|
b.updateDiffTimer = nil
|
||||||
b.updateDiffSync()
|
b.updateDiff(false)
|
||||||
callback(false)
|
screen.Redraw()
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
// Don't compute diffs for very large files
|
// Don't compute diffs for very large files
|
||||||
b.diffLock.Lock()
|
b.diffLock.Lock()
|
||||||
b.diff = make(map[int]DiffStatus)
|
b.diff = make(map[int]DiffStatus)
|
||||||
b.diffLock.Unlock()
|
b.diffLock.Unlock()
|
||||||
callback(true)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1362,9 +1365,7 @@ func (b *Buffer) SetDiffBase(diffBase []byte) {
|
||||||
} else {
|
} else {
|
||||||
b.diffBaseLineCount = strings.Count(string(diffBase), "\n")
|
b.diffBaseLineCount = strings.Count(string(diffBase), "\n")
|
||||||
}
|
}
|
||||||
b.UpdateDiff(func(synchronous bool) {
|
b.UpdateDiff()
|
||||||
screen.Redraw()
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DiffStatus returns the diff status for a line in the buffer
|
// DiffStatus returns the diff status for a line in the buffer
|
||||||
|
|
|
@ -384,18 +384,7 @@ func (w *BufWindow) displayBuffer() {
|
||||||
|
|
||||||
if b.ModifiedThisFrame {
|
if b.ModifiedThisFrame {
|
||||||
if b.Settings["diffgutter"].(bool) {
|
if b.Settings["diffgutter"].(bool) {
|
||||||
b.UpdateDiff(func(synchronous bool) {
|
b.UpdateDiff()
|
||||||
// If the diff was updated asynchronously, the outer call to
|
|
||||||
// displayBuffer might already be completed and we need to
|
|
||||||
// schedule a redraw in order to display the new diff.
|
|
||||||
// Note that this cannot lead to an infinite recursion
|
|
||||||
// because the modifications were cleared above so there won't
|
|
||||||
// be another call to UpdateDiff when displayBuffer is called
|
|
||||||
// during the redraw.
|
|
||||||
if !synchronous {
|
|
||||||
screen.Redraw()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
b.ModifiedThisFrame = false
|
b.ModifiedThisFrame = false
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue