1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-07-03 00:57:52 +02:00
Files
lazygit/pkg/gui/patch_exploring/focus.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

48 lines
1.7 KiB
Go

package patch_exploring
func calculateOrigin(currentOrigin int, bufferHeight int, numLines int, firstLineIdx int, lastLineIdx int, selectedLineIdx int, mode selectMode) int {
needToSeeIdx, wantToSeeIdx := getNeedAndWantLineIdx(firstLineIdx, lastLineIdx, selectedLineIdx, mode)
return calculateNewOriginWithNeededAndWantedIdx(currentOrigin, bufferHeight, numLines, needToSeeIdx, wantToSeeIdx)
}
// we want to scroll our origin so that the index we need to see is in view
// and the other index we want to see (e.g. the other side of a line range)
// is as close to being in view as possible.
func calculateNewOriginWithNeededAndWantedIdx(currentOrigin int, bufferHeight int, numLines int, needToSeeIdx int, wantToSeeIdx int) int {
origin := currentOrigin
if needToSeeIdx < currentOrigin || needToSeeIdx >= currentOrigin+bufferHeight {
origin = max(min(needToSeeIdx-bufferHeight/2, numLines-bufferHeight), 0)
}
bottom := origin + bufferHeight
if wantToSeeIdx < origin {
requiredChange := origin - wantToSeeIdx
allowedChange := bottom - needToSeeIdx
return origin - min(requiredChange, allowedChange)
} else if wantToSeeIdx >= bottom {
requiredChange := wantToSeeIdx - bottom
allowedChange := needToSeeIdx - origin
return origin + min(requiredChange, allowedChange)
}
return origin
}
func getNeedAndWantLineIdx(firstLineIdx int, lastLineIdx int, selectedLineIdx int, mode selectMode) (int, int) {
switch mode {
case LINE:
return selectedLineIdx, selectedLineIdx
case RANGE:
if selectedLineIdx == firstLineIdx {
return firstLineIdx, lastLineIdx
}
return lastLineIdx, firstLineIdx
case HUNK:
return firstLineIdx, lastLineIdx
default:
// we should never land here
panic("unknown mode")
}
}