Skip to content
Open
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
15 changes: 15 additions & 0 deletions internal/action/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"io/fs"
"math"
"os"
"regexp"
"runtime"
Expand Down Expand Up @@ -482,6 +483,20 @@ func (h *BufPane) CursorEnd() bool {
return true
}

// CursorRatio moves the cursor to a given ratio of the buffer.
// For example, if ratio is 0.5, the cursor is moved to the center line.
func (h *BufPane) CursorRatio(ratio float64) bool {
if ratio < 0 || ratio > 1 {
return false
}
targetLocation := buffer.Loc{0, int(math.Round(float64(h.Buf.End().Y) * ratio))}
h.Cursor.Deselect(true)
h.Cursor.Loc = targetLocation
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please note that for goto (and generally for cases where the new location may be far away from the old location) you should use h.GotoLoc() rather than just set the new location and relocate. See #2628.

h.Cursor.StoreVisualX()
h.Relocate()
return true
}

// SelectToStart selects the text from the cursor to the start of the buffer
func (h *BufPane) SelectToStart() bool {
if !h.Cursor.HasSelection() {
Expand Down
21 changes: 20 additions & 1 deletion internal/action/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,12 @@ func (h *BufPane) QuitCmd(args []string) {
// For example: `goto line`, or `goto line:col`
func (h *BufPane) GotoCmd(args []string) {
if len(args) <= 0 {
InfoBar.Error("Not enough arguments")
// No argument given, alternate between jumping to the top or the bottom
if h.Cursor.Loc.Y > 0 {
h.CursorStart()
} else {
h.CursorEnd()
}
} else {
h.RemoveAllMultiCursors()
if strings.Contains(args[0], ":") {
Expand All @@ -723,6 +728,20 @@ func (h *BufPane) GotoCmd(args []string) {
line = util.Clamp(line-1, 0, h.Buf.LinesNum()-1)
col = util.Clamp(col-1, 0, util.CharacterCount(h.Buf.LineBytes(line)))
h.GotoLoc(buffer.Loc{col, line})
} else if strings.HasSuffix(args[0], "%") { // Jump to a percentage, like 50%
percentage, err := strconv.Atoi(args[0][:len(args[0])-1])
if err != nil {
InfoBar.Error(err)
return
}
h.CursorRatio(float64(percentage) * 0.01) // Convert from percentage to ratio with * 0.01
} else if strings.Count(args[0], ".") == 1 || strings.Count(args[0], ",") == 1 { // Jump to a ratio, like .5 or ,5 ?
ratio, err := strconv.ParseFloat(strings.ReplaceAll(args[0], ",", "."), 64)
if err != nil {
InfoBar.Error(err)
return
}
h.CursorRatio(ratio)
} else {
line, err := strconv.Atoi(args[0])
if err != nil {
Expand Down