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

Implemented new actions SelectWord, SelectNextTextOccurrence, SelectPreviousTextOccurrence #3405

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
74 changes: 74 additions & 0 deletions internal/action/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,65 @@ func (h *BufPane) CursorEnd() bool {
return true
}

// SelectWord selects the current word under the cursor if there is no selection already
func (h *BufPane) SelectWord() bool {
if h.Cursor.HasSelection() {
return false
}
h.Cursor.SelectWord()
return h.Cursor.HasSelection()
}

// SelectNextTextOccurrence selects the next occurrence of the currently selected text
func (h *BufPane) SelectNextTextOccurrence() bool {
if !h.Cursor.HasSelection() {
return false
}
if h.Buf.NumCursors() > 1 {
return false
}
selectedText := string(h.Cursor.GetSelection())
searchLoc := h.Cursor.Loc
match, found, err := h.Buf.FindNext(selectedText, h.Buf.Start(), h.Buf.End(), searchLoc, true, false)
if err != nil {
return false
}
if found {
h.Cursor.SetSelectionStart(match[0])
h.Cursor.SetSelectionEnd(match[1])
h.Cursor.OrigSelection[0] = h.Cursor.CurSelection[0]
h.Cursor.OrigSelection[1] = h.Cursor.CurSelection[1]
h.GotoLoc(h.Cursor.CurSelection[1])
return true
}
return false
}

// SelectPreviousTextOccurrence selects the previous occurrence of the currently selected text
func (h *BufPane) SelectPreviousTextOccurrence() bool {
if !h.Cursor.HasSelection() {
return false
}
if h.Buf.NumCursors() > 1 {
return false
}
selectedText := string(h.Cursor.GetSelection())
searchLoc := h.Cursor.CurSelection[0]
match, found, err := h.Buf.FindNext(selectedText, h.Buf.Start(), h.Buf.End(), searchLoc, false, false)
if err != nil {
return false
}
if found {
h.Cursor.SetSelectionStart(match[0])
h.Cursor.SetSelectionEnd(match[1])
h.Cursor.OrigSelection[0] = h.Cursor.CurSelection[0]
h.Cursor.OrigSelection[1] = h.Cursor.CurSelection[1]
h.GotoLoc(h.Cursor.CurSelection[1])
return true
}
return false
}

// SelectToStart selects the text from the cursor to the start of the buffer
func (h *BufPane) SelectToStart() bool {
if !h.Cursor.HasSelection() {
Expand Down Expand Up @@ -1138,6 +1197,21 @@ func (h *BufPane) ResetSearch() bool {
return false
}

// FindSelected sets the currently selected text as the search term
func (h *BufPane) FindSelected() bool {
if !h.Cursor.HasSelection() {
return false
}
selectedText := string(h.Cursor.GetSelection())
if selectedText == h.Buf.LastSearch {
return false
}
h.Buf.HighlightSearch = h.Buf.Settings["hlsearch"].(bool)
h.Buf.LastSearchRegex = false
h.Buf.LastSearch = selectedText
return true
}

// FindNext searches forwards for the last used search term
func (h *BufPane) FindNext() bool {
if h.Buf.LastSearch == "" {
Expand Down
Loading