mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-01-24 05:36:19 +02:00
simplify merge panel logic
This commit is contained in:
parent
cf8ded0b79
commit
a533f8e1a5
@ -7,6 +7,15 @@ import (
|
|||||||
"github.com/jesseduffield/lazygit/pkg/utils"
|
"github.com/jesseduffield/lazygit/pkg/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func clamp(x int, min int, max int) int {
|
||||||
|
if x < min {
|
||||||
|
return min
|
||||||
|
} else if x > max {
|
||||||
|
return max
|
||||||
|
}
|
||||||
|
return x
|
||||||
|
}
|
||||||
|
|
||||||
type Selection int
|
type Selection int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -29,60 +38,61 @@ func (c *mergeConflict) hasAncestor() bool {
|
|||||||
return c.ancestor >= 0
|
return c.ancestor >= 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *mergeConflict) availableSelections() []Selection {
|
||||||
|
if c.hasAncestor() {
|
||||||
|
return []Selection{TOP, MIDDLE, BOTTOM}
|
||||||
|
} else {
|
||||||
|
return []Selection{TOP, BOTTOM}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type State struct {
|
type State struct {
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
conflictIndex int
|
conflictIndex int
|
||||||
conflictSelection Selection
|
selectionIndex int
|
||||||
conflicts []*mergeConflict
|
conflicts []*mergeConflict
|
||||||
EditHistory *stack.Stack
|
EditHistory *stack.Stack
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewState() *State {
|
func NewState() *State {
|
||||||
return &State{
|
return &State{
|
||||||
Mutex: sync.Mutex{},
|
Mutex: sync.Mutex{},
|
||||||
conflictIndex: 0,
|
conflictIndex: 0,
|
||||||
conflictSelection: TOP,
|
selectionIndex: 0,
|
||||||
conflicts: []*mergeConflict{},
|
conflicts: []*mergeConflict{},
|
||||||
EditHistory: stack.New(),
|
EditHistory: stack.New(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *State) SelectPrevConflictHunk() {
|
func (s *State) setConflictIndex(index int) {
|
||||||
switch s.conflictSelection {
|
if len(s.conflicts) == 0 {
|
||||||
case MIDDLE:
|
s.conflictIndex = clamp(index, 0, len(s.conflicts)-1)
|
||||||
s.conflictSelection = TOP
|
} else {
|
||||||
case BOTTOM:
|
s.conflictIndex = 0
|
||||||
if s.currentConflict().hasAncestor() {
|
}
|
||||||
s.conflictSelection = MIDDLE
|
s.setSelectionIndex(s.selectionIndex)
|
||||||
} else {
|
}
|
||||||
s.conflictSelection = TOP
|
|
||||||
}
|
func (s *State) setSelectionIndex(index int) {
|
||||||
|
if selections := s.availableSelections(); len(selections) != 0 {
|
||||||
|
s.selectionIndex = clamp(index, 0, len(selections)-1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *State) SelectNextConflictHunk() {
|
func (s *State) SelectNextConflictHunk() {
|
||||||
switch s.conflictSelection {
|
s.setSelectionIndex(s.selectionIndex + 1)
|
||||||
case TOP:
|
}
|
||||||
if s.currentConflict().hasAncestor() {
|
|
||||||
s.conflictSelection = MIDDLE
|
func (s *State) SelectPrevConflictHunk() {
|
||||||
} else {
|
s.setSelectionIndex(s.selectionIndex - 1)
|
||||||
s.conflictSelection = BOTTOM
|
|
||||||
}
|
|
||||||
case MIDDLE:
|
|
||||||
s.conflictSelection = BOTTOM
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *State) SelectNextConflict() {
|
func (s *State) SelectNextConflict() {
|
||||||
if s.conflictIndex < len(s.conflicts)-1 {
|
s.setConflictIndex(s.conflictIndex + 1)
|
||||||
s.conflictIndex++
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *State) SelectPrevConflict() {
|
func (s *State) SelectPrevConflict() {
|
||||||
if s.conflictIndex > 0 {
|
s.setConflictIndex(s.conflictIndex - 1)
|
||||||
s.conflictIndex--
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *State) PushFileSnapshot(content string) {
|
func (s *State) PushFileSnapshot(content string) {
|
||||||
@ -111,12 +121,7 @@ func (s *State) SetConflictsFromCat(cat string) {
|
|||||||
|
|
||||||
func (s *State) setConflicts(conflicts []*mergeConflict) {
|
func (s *State) setConflicts(conflicts []*mergeConflict) {
|
||||||
s.conflicts = conflicts
|
s.conflicts = conflicts
|
||||||
|
s.setConflictIndex(s.conflictIndex)
|
||||||
if s.conflictIndex > len(s.conflicts)-1 {
|
|
||||||
s.conflictIndex = len(s.conflicts) - 1
|
|
||||||
} else if s.conflictIndex < 0 {
|
|
||||||
s.conflictIndex = 0
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *State) NoConflicts() bool {
|
func (s *State) NoConflicts() bool {
|
||||||
@ -124,7 +129,17 @@ func (s *State) NoConflicts() bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *State) Selection() Selection {
|
func (s *State) Selection() Selection {
|
||||||
return s.conflictSelection
|
if selections := s.availableSelections(); len(selections) > 0 {
|
||||||
|
return selections[s.selectionIndex]
|
||||||
|
}
|
||||||
|
return TOP
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *State) availableSelections() []Selection {
|
||||||
|
if conflict := s.currentConflict(); conflict != nil {
|
||||||
|
return conflict.availableSelections()
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *State) IsFinalConflict() bool {
|
func (s *State) IsFinalConflict() bool {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user