add actions: CursorToViewTop, CursorToViewCenter, CursorToViewBottom (#3506)

This commit is contained in:
Nimish Jha 2024-10-23 16:25:33 +11:00 committed by GitHub
parent 2c6dc32f5d
commit b3227d6049
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 52 additions and 0 deletions

View file

@ -160,6 +160,52 @@ func (h *BufPane) Center() bool {
return true
}
// CursorToViewTop moves the cursor to the top of the view,
// offset by scrollmargin unless at the beginning or end of the file
func (h *BufPane) CursorToViewTop() bool {
v := h.GetView()
h.Buf.ClearCursors()
scrollmargin := int(h.Buf.Settings["scrollmargin"].(float64))
bStart := display.SLoc{0, 0}
if v.StartLine == bStart {
scrollmargin = 0
}
h.Cursor.GotoLoc(h.LocFromVLoc(display.VLoc{
SLoc: h.Scroll(v.StartLine, scrollmargin),
VisualX: 0,
}))
return true
}
// CursorToViewCenter moves the cursor to the center of the view
func (h *BufPane) CursorToViewCenter() bool {
v := h.GetView()
h.Buf.ClearCursors()
h.Cursor.GotoLoc(h.LocFromVLoc(display.VLoc{
SLoc: h.Scroll(v.StartLine, h.BufView().Height/2),
VisualX: 0,
}))
return true
}
// CursorToViewBottom moves the cursor to the bottom of the view,
// offset by scrollmargin unless at the beginning or end of the file
func (h *BufPane) CursorToViewBottom() bool {
v := h.GetView()
h.Buf.ClearCursors()
scrollmargin := int(h.Buf.Settings["scrollmargin"].(float64))
bEnd := h.SLocFromLoc(h.Buf.End())
lastLine := h.Scroll(v.StartLine, h.BufView().Height-1)
if lastLine == bEnd {
scrollmargin = 0
}
h.Cursor.GotoLoc(h.LocFromVLoc(display.VLoc{
SLoc: h.Scroll(lastLine, -scrollmargin),
VisualX: 0,
}))
return true
}
// MoveCursorUp is not an action
func (h *BufPane) MoveCursorUp(n int) {
if !h.Buf.Settings["softwrap"].(bool) {

View file

@ -738,6 +738,9 @@ var BufKeyActions = map[string]BufKeyAction{
"CursorRight": (*BufPane).CursorRight,
"CursorStart": (*BufPane).CursorStart,
"CursorEnd": (*BufPane).CursorEnd,
"CursorToViewTop": (*BufPane).CursorToViewTop,
"CursorToViewCenter": (*BufPane).CursorToViewCenter,
"CursorToViewBottom": (*BufPane).CursorToViewBottom,
"SelectToStart": (*BufPane).SelectToStart,
"SelectToEnd": (*BufPane).SelectToEnd,
"SelectUp": (*BufPane).SelectUp,

View file

@ -168,6 +168,9 @@ CursorLeft
CursorRight
CursorStart
CursorEnd
CursorToViewTop
CursorToViewCenter
CursorToViewBottom
SelectToStart
SelectToEnd
SelectUp