1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-07-03 00:57:52 +02:00
Files
lazygit/pkg/gui/mergeconflicts/merge_conflict.go
Stefan Haller ca05a2ccea Enable revive linter, and fix a bunch of warnings
I took the set of enabled checks from revive's recommended configuration [1],
and removed some that I didn't like. There might be other useful checks in
revive that we might want to enable, but this is a nice improvement already.

The bulk of the changes here are removing unnecessary else statements after
returns, but there are a few others too.

[1] https://github.com/mgechev/revive?tab=readme-ov-file#recommended-configuration
2025-06-30 19:13:20 +02:00

77 lines
1.6 KiB
Go

package mergeconflicts
// mergeConflict : A git conflict with a start, ancestor (if exists), target, and end corresponding to line
// numbers in the file where the conflict markers appear.
// If no ancestor is present (i.e. we're not using the diff3 algorithm), then
// the `ancestor` field's value will be -1
type mergeConflict struct {
start int
ancestor int
target int
end int
}
func (c *mergeConflict) hasAncestor() bool {
return c.ancestor >= 0
}
func (c *mergeConflict) isMarkerLine(i int) bool {
return i == c.start ||
i == c.ancestor ||
i == c.target ||
i == c.end
}
type Selection int
const (
TOP Selection = iota
MIDDLE
BOTTOM
ALL
)
func (s Selection) isIndexToKeep(conflict *mergeConflict, i int) bool {
// we're only handling one conflict at a time so any lines outside this
// conflict we'll keep
if i < conflict.start || conflict.end < i {
return true
}
if conflict.isMarkerLine(i) {
return false
}
return s.selected(conflict, i)
}
func (s Selection) bounds(c *mergeConflict) (int, int) {
switch s {
case TOP:
if c.hasAncestor() {
return c.start, c.ancestor
}
return c.start, c.target
case MIDDLE:
return c.ancestor, c.target
case BOTTOM:
return c.target, c.end
case ALL:
return c.start, c.end
}
panic("unexpected selection for merge conflict")
}
func (s Selection) selected(c *mergeConflict, idx int) bool {
start, end := s.bounds(c)
return start < idx && idx < end
}
func availableSelections(c *mergeConflict) []Selection {
if c.hasAncestor() {
return []Selection{TOP, MIDDLE, BOTTOM}
}
return []Selection{TOP, BOTTOM}
}