diff options
Diffstat (limited to 'pkg/gui/merge_panel.go')
-rw-r--r-- | pkg/gui/merge_panel.go | 76 |
1 files changed, 71 insertions, 5 deletions
diff --git a/pkg/gui/merge_panel.go b/pkg/gui/merge_panel.go index 7700f193c..dbd5b3be7 100644 --- a/pkg/gui/merge_panel.go +++ b/pkg/gui/merge_panel.go @@ -138,6 +138,13 @@ func (gui *Gui) renderConflicts(hasFocus bool) error { // TODO: find a way to not have to do this OnUIThread thing. Why doesn't it work // without it given that we're calling the 'no scroll' variant below? gui.OnUIThread(func() error { + gui.State.Panels.Merging.Lock() + defer gui.State.Panels.Merging.Unlock() + + if !state.Active() { + return nil + } + gui.centerYPos(gui.Views.Main, state.GetConflictMiddle()) return nil }) @@ -156,6 +163,12 @@ func (gui *Gui) renderConflictsWithFocus() error { return gui.renderConflicts(true) } +func (gui *Gui) renderConflictsWithLock(hasFocus bool) error { + return gui.withMergeConflictLock(func() error { + return gui.renderConflicts(hasFocus) + }) +} + func (gui *Gui) centerYPos(view *gocui.View, y int) { ox, _ := view.Origin() _, height := view.Size() @@ -205,15 +218,27 @@ func (gui *Gui) setMergeState(path string) (bool, error) { return !gui.State.Panels.Merging.NoConflicts(), nil } -func (gui *Gui) escapeMerge() error { +func (gui *Gui) setMergeStateWithLock(path string) (bool, error) { + gui.State.Panels.Merging.Lock() + defer gui.State.Panels.Merging.Unlock() + + return gui.setMergeState(path) +} + +func (gui *Gui) resetMergeStateWithLock() { + gui.State.Panels.Merging.Lock() + defer gui.State.Panels.Merging.Unlock() + gui.resetMergeState() +} - // it's possible this method won't be called from the merging view so we need to - // ensure we only 'return' focus if we already have it +func (gui *Gui) escapeMerge() error { + gui.resetMergeState() - if gui.currentContext().GetKey() == MAIN_MERGING_CONTEXT_KEY { + // doing this in separate UI thread so that we're not still holding the lock by the time refresh the file + gui.OnUIThread(func() error { return gui.pushContext(gui.State.Contexts.Files) - } + }) return nil } @@ -236,3 +261,44 @@ func (gui *Gui) withMergeConflictLock(f func() error) error { func (gui *Gui) takeOverMergeConflictScrolling() { gui.State.Panels.Merging.UserVerticalScrolling = false } + +func (gui *Gui) setConflictsAndRender(path string, hasFocus bool) (bool, error) { + hasConflicts, err := gui.setMergeState(path) + if err != nil { + return false, err + } + + // if we don't have conflicts we'll fall through and show the diff + if hasConflicts { + return true, gui.renderConflicts(hasFocus) + } + + return false, nil +} + +func (gui *Gui) setConflictsAndRenderWithLock(path string, hasFocus bool) (bool, error) { + gui.State.Panels.Merging.Lock() + defer gui.State.Panels.Merging.Unlock() + + return gui.setConflictsAndRender(path, hasFocus) +} + +func (gui *Gui) refreshMergeState() error { + gui.State.Panels.Merging.Lock() + defer gui.State.Panels.Merging.Unlock() + + if gui.currentContext().GetKey() != MAIN_MERGING_CONTEXT_KEY { + return nil + } + + hasConflicts, err := gui.setConflictsAndRender(gui.State.Panels.Merging.GetPath(), true) + if err != nil { + return gui.surfaceError(err) + } + + if !hasConflicts { + return gui.escapeMerge() + } + + return nil +} |