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

Adding selection for ParagraphPrevious and ParagraphNext. #3353

Merged
merged 1 commit into from
Jul 20, 2024
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
74 changes: 64 additions & 10 deletions internal/action/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,38 +490,92 @@ func (h *BufPane) SelectToEndOfLine() bool {
return true
}

// ParagraphPrevious moves the cursor to the previous empty line, or beginning of the buffer if there's none
func (h *BufPane) ParagraphPrevious() bool {
func (h *BufPane) paragraphPrevious() {
var line int
// Skip to the first non-empty line
for line = h.Cursor.Y; line > 0; line-- {
if len(h.Buf.LineBytes(line)) == 0 && line != h.Cursor.Y {
if len(h.Buf.LineBytes(line)) != 0 {
break
}
}
// Find the first empty line
for ; line > 0; line-- {
if len(h.Buf.LineBytes(line)) == 0 {
h.Cursor.X = 0
h.Cursor.Y = line
break
}
}
// If no empty line found. move cursor to end of buffer
// If no empty line was found, move the cursor to the start of the buffer
if line == 0 {
h.Cursor.Loc = h.Buf.Start()
}
h.Relocate()
return true
}

// ParagraphNext moves the cursor to the next empty line, or end of the buffer if there's none
func (h *BufPane) ParagraphNext() bool {
func (h *BufPane) paragraphNext() {
var line int
// Skip to the first non-empty line
for line = h.Cursor.Y; line < h.Buf.LinesNum(); line++ {
if len(h.Buf.LineBytes(line)) == 0 && line != h.Cursor.Y {
if len(h.Buf.LineBytes(line)) != 0 {
break
}
}
// Find the first empty line
for ; line < h.Buf.LinesNum(); line++ {
if len(h.Buf.LineBytes(line)) == 0 {
h.Cursor.X = 0
h.Cursor.Y = line
break
}
}
// If no empty line found. move cursor to end of buffer
// If no empty line was found, move the cursor to the end of the buffer
if line == h.Buf.LinesNum() {
h.Cursor.Loc = h.Buf.End()
}
}

// ParagraphPrevious moves the cursor to the first empty line that comes before
// the paragraph closest to the cursor, or beginning of the buffer if there
// isn't a paragraph
func (h *BufPane) ParagraphPrevious() bool {
h.Cursor.Deselect(true)
h.paragraphPrevious()
h.Relocate()
return true
}

// ParagraphNext moves the cursor to the first empty line that comes after the
// paragraph closest to the cursor, or end of the buffer if there isn't a
// paragraph
func (h *BufPane) ParagraphNext() bool {
h.Cursor.Deselect(true)
h.paragraphNext()
h.Relocate()
return true
}

// SelectToParagraphPrevious selects to the first empty line that comes before
// the paragraph closest to the cursor, or beginning of the buffer if there
// isn't a paragraph
func (h *BufPane) SelectToParagraphPrevious() bool {
if !h.Cursor.HasSelection() {
h.Cursor.OrigSelection[0] = h.Cursor.Loc
}
h.paragraphPrevious()
h.Cursor.SelectTo(h.Cursor.Loc)
h.Relocate()
return true
}

// SelectToParagraphNext selects to the first empty line that comes after the
// paragraph closest to the cursor, or end of the buffer if there isn't a
// paragraph
func (h *BufPane) SelectToParagraphNext() bool {
if !h.Cursor.HasSelection() {
h.Cursor.OrigSelection[0] = h.Cursor.Loc
}
h.paragraphNext()
h.Cursor.SelectTo(h.Cursor.Loc)
h.Relocate()
return true
}
Expand Down
2 changes: 2 additions & 0 deletions internal/action/bufpane.go
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,8 @@ var BufKeyActions = map[string]BufKeyAction{
"SelectToEndOfLine": (*BufPane).SelectToEndOfLine,
"ParagraphPrevious": (*BufPane).ParagraphPrevious,
"ParagraphNext": (*BufPane).ParagraphNext,
"SelectToParagraphPrevious": (*BufPane).SelectToParagraphPrevious,
"SelectToParagraphNext": (*BufPane).SelectToParagraphNext,
"InsertNewline": (*BufPane).InsertNewline,
"Backspace": (*BufPane).Backspace,
"Delete": (*BufPane).Delete,
Expand Down
2 changes: 2 additions & 0 deletions runtime/help/keybindings.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ StartOfText
StartOfTextToggle
ParagraphPrevious
ParagraphNext
SelectToParagraphPrevious
SelectToParagraphNext
ToggleHelp
ToggleDiffGutter
ToggleRuler
Expand Down