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

action: add block/paragraph selection #2012

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
42 changes: 42 additions & 0 deletions internal/action/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,48 @@ func (h *BufPane) ParagraphNext() bool {
return true
}

// SelectParagraphPrevious select to the previous empty line, or beginning of the buffer if there's none
func (h *BufPane) SelectParagraphPrevious() bool {
var line int
if !h.Cursor.HasSelection() {
h.Cursor.OrigSelection[0] = h.Cursor.Loc
}
for line = h.Cursor.Y; line > 0; line-- {
if len(h.Buf.LineBytes(line)) == 0 && line != h.Cursor.Y {
h.Cursor.X = 0
h.Cursor.Y = line
break
}
}
// If no empty line found. move cursor to end of buffer
if line == 0 {
h.Cursor.Loc = h.Buf.Start()
}
h.Cursor.SelectTo(h.Cursor.Loc)
Copy link
Collaborator

@dmaluka dmaluka Mar 26, 2024

Choose a reason for hiding this comment

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

h.Relocate() is missing. As a result, when selecting paragraphs beyond the bottom/top of the screen, it doesn't scroll the screen automatically (unlike with ParagraphNext and ParagraphPrevious).

...In fact, looks like this can be implemented much shorter, by reusing ParagraphNext() and ParagraphPrevious() (and that would also automatically take care of scrolling):

// SelectParagraphPrevious select to the previous empty line, or beginning of the buffer if there's none
func (h *BufPane) SelectParagraphPrevious() bool {
	if !h.Cursor.HasSelection() {
		h.Cursor.OrigSelection[0] = h.Cursor.Loc
	}
	h.ParagraphPrevious()
	h.Cursor.SelectTo(h.Cursor.Loc)
	return true
}

// SelectParagraphNext select to the next empty line, or end of the buffer if there's none
func (h *BufPane) SelectParagraphNext() bool {
	if !h.Cursor.HasSelection() {
		h.Cursor.OrigSelection[0] = h.Cursor.Loc
	}
	h.ParagraphNext()
	h.Cursor.SelectTo(h.Cursor.Loc)
	return true
}

...Actually there is one more issue: if there are several empty lines one after another, it just selects those empty lines one by one, as if they were "paragraphs", instead of jumping to selecting the next actual paragraph. But the same issue actually exists with ParagraphNext and ParagraphPrevious as well, so it can be fixed separately.

Choose a reason for hiding this comment

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

In this solution, while it does select the block, the cursor does not get moved with the selection, rather it is projected from the cursor while it stays in place. I would expect block select to drag the selection behind the cursor, essentially being ParagraphNext + selection. In the same way that ctrl+shift+left/right moves the cursor, and all other ctrl+shift combinations.

return true
}

// SelectParagraphNext select to the next empty line, or end of the buffer if there's none
func (h *BufPane) SelectParagraphNext() bool {
var line int
if !h.Cursor.HasSelection() {
h.Cursor.OrigSelection[0] = h.Cursor.Loc
}
for line = h.Cursor.Y; line < h.Buf.LinesNum(); line++ {
if len(h.Buf.LineBytes(line)) == 0 && line != h.Cursor.Y {
h.Cursor.X = 0
h.Cursor.Y = line
break
}
}
// If no empty line found. move cursor to end of buffer
if line == h.Buf.LinesNum() {
h.Cursor.Loc = h.Buf.End()
}
h.Cursor.SelectTo(h.Cursor.Loc)
return true
}

// Retab changes all tabs to spaces or all spaces to tabs depending
// on the user's settings
func (h *BufPane) Retab() bool {
Expand Down
4 changes: 4 additions & 0 deletions internal/action/bufpane.go
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,8 @@ var BufKeyActions = map[string]BufKeyAction{
"SelectToEndOfLine": (*BufPane).SelectToEndOfLine,
"ParagraphPrevious": (*BufPane).ParagraphPrevious,
"ParagraphNext": (*BufPane).ParagraphNext,
"SelectParagraphPrevious": (*BufPane).SelectParagraphPrevious,
"SelectParagraphNext": (*BufPane).SelectParagraphNext,
"InsertNewline": (*BufPane).InsertNewline,
"Backspace": (*BufPane).Backspace,
"Delete": (*BufPane).Delete,
Expand Down Expand Up @@ -734,6 +736,8 @@ var MultiActions = map[string]bool{
"SelectToEndOfLine": true,
"ParagraphPrevious": true,
"ParagraphNext": true,
"SelectParagraphPrevious": true,
"SelectParagraphNext": true,
"InsertNewline": true,
"Backspace": true,
"Delete": true,
Expand Down