2021-04-18 10:07:10 +02:00
|
|
|
package mergeconflicts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
|
2021-07-27 15:00:37 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/style"
|
2021-04-18 10:07:10 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/theme"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
func ColoredConflictFile(content string, state *State, hasFocus bool) string {
|
|
|
|
if len(state.conflicts) == 0 {
|
|
|
|
return content
|
|
|
|
}
|
|
|
|
conflict, remainingConflicts := shiftConflict(state.conflicts)
|
|
|
|
var outputBuffer bytes.Buffer
|
|
|
|
for i, line := range utils.SplitLines(content) {
|
2021-07-31 04:54:28 +02:00
|
|
|
textStyle := theme.DefaultTextColor
|
2021-08-21 18:03:05 +02:00
|
|
|
if i == conflict.start || i == conflict.ancestor || i == conflict.target || i == conflict.end {
|
2021-07-31 04:54:28 +02:00
|
|
|
textStyle = style.FgRed
|
2021-04-18 10:07:10 +02:00
|
|
|
}
|
2021-07-27 15:00:37 +02:00
|
|
|
|
2021-08-21 18:03:05 +02:00
|
|
|
if hasFocus && state.conflictIndex < len(state.conflicts) && *state.conflicts[state.conflictIndex] == *conflict && shouldHighlightLine(i, conflict, state.conflictSelection) {
|
2021-08-01 08:02:49 +02:00
|
|
|
textStyle = textStyle.MergeStyle(theme.SelectedRangeBgColor).SetBold()
|
2021-04-18 10:07:10 +02:00
|
|
|
}
|
|
|
|
if i == conflict.end && len(remainingConflicts) > 0 {
|
|
|
|
conflict, remainingConflicts = shiftConflict(remainingConflicts)
|
|
|
|
}
|
2021-07-31 04:54:28 +02:00
|
|
|
outputBuffer.WriteString(textStyle.Sprint(line) + "\n")
|
2021-04-18 10:07:10 +02:00
|
|
|
}
|
|
|
|
return outputBuffer.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func shiftConflict(conflicts []*mergeConflict) (*mergeConflict, []*mergeConflict) {
|
|
|
|
return conflicts[0], conflicts[1:]
|
|
|
|
}
|
|
|
|
|
2021-08-21 18:03:05 +02:00
|
|
|
func shouldHighlightLine(index int, conflict *mergeConflict, selection Selection) bool {
|
|
|
|
switch selection {
|
|
|
|
case TOP:
|
|
|
|
if conflict.ancestor >= 0 {
|
|
|
|
return index >= conflict.start && index <= conflict.ancestor
|
|
|
|
} else {
|
|
|
|
return index >= conflict.start && index <= conflict.target
|
|
|
|
}
|
|
|
|
case MIDDLE:
|
|
|
|
return index >= conflict.ancestor && index <= conflict.target
|
|
|
|
case BOTTOM:
|
|
|
|
return index >= conflict.target && index <= conflict.end
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
2021-04-18 10:07:10 +02:00
|
|
|
}
|