Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent cursor jumping to bottom after deleting file #1247

Merged
merged 1 commit into from
May 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ func (app *app) loop() {
prev, ok := app.nav.dirCache[d.path]
if ok {
d.ind = prev.ind
d.pos = prev.pos
d.sel(prev.name(), app.nav.height)
}

Expand Down
37 changes: 23 additions & 14 deletions nav.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,27 @@ func (dir *dir) sel(name string, height int) {
}
}

edge := min(min(height/2, gOpts.scrolloff), len(dir.files)-dir.ind-1)
dir.pos = min(dir.ind, height-edge-1)
dir.boundPos(height)
}

func (dir *dir) boundPos(height int) {
if len(dir.files) <= height {
dir.pos = dir.ind
return
}

edge := min(height/2, gOpts.scrolloff)
dir.pos = max(dir.pos, edge)

// use a smaller value for half when the height is even and scrolloff is
// maxed in order to stay at the same row while scrolling up and down
if height%2 == 0 {
edge = min(height/2-1, gOpts.scrolloff)
}
dir.pos = min(dir.pos, height-1-edge)

dir.pos = min(dir.pos, dir.ind)
dir.pos = max(dir.pos, height-(len(dir.files)-dir.ind))
}

type nav struct {
Expand Down Expand Up @@ -904,8 +923,7 @@ func (nav *nav) up(dist int) bool {
dir.ind = max(0, dir.ind)

dir.pos -= dist
edge := min(min(nav.height/2, gOpts.scrolloff), dir.ind)
dir.pos = max(dir.pos, edge)
dir.boundPos(nav.height)

return old != dir.ind
}
Expand All @@ -928,16 +946,7 @@ func (nav *nav) down(dist int) bool {
dir.ind = min(maxind, dir.ind)

dir.pos += dist
// use a smaller value for half when the height is even and scrolloff is
// maxed in order to stay at the same row while scrolling up and down
half := nav.height / 2
if nav.height%2 == 0 {
half--
}
edge := min(min(half, gOpts.scrolloff), maxind-dir.ind)

dir.pos = min(dir.pos, nav.height-edge-1)
dir.pos = min(dir.pos, maxind)
dir.boundPos(nav.height)

return old != dir.ind
}
Expand Down