Skip to content
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
18 changes: 16 additions & 2 deletions internal/action/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,20 @@ func (h *BufPane) OutdentSelection() bool {
return false
}

func canAutocomplete(c *buffer.Cursor) bool {
if c.X == 0 {
return false
}

r := c.RuneUnder(c.X)
prev := c.RuneUnder(c.X - 1)
if !util.IsAutocomplete(prev) || util.IsWordChar(r) {
// don't autocomplete if cursor is within a word
return false
}
return true
}

// Autocomplete cycles the suggestions and performs autocompletion if there are suggestions
func (h *BufPane) Autocomplete() bool {
b := h.Buf
Expand All @@ -916,7 +930,7 @@ func (h *BufPane) Autocomplete() bool {
return true
}

return b.Autocomplete(buffer.BufferComplete)
return b.AutocompleteCB(buffer.BufferComplete, canAutocomplete)
}

// CycleAutocompleteBack cycles back in the autocomplete suggestion list
Expand All @@ -926,7 +940,7 @@ func (h *BufPane) CycleAutocompleteBack() bool {
}

if h.Buf.HasSuggestions {
h.Buf.CycleAutocomplete(false)
h.Buf.CycleAutocompleteCB(false, canAutocomplete)
return true
}
return false
Expand Down
29 changes: 12 additions & 17 deletions internal/buffer/autocomplete.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,29 @@ func (b *Buffer) GetSuggestions() {

// Autocomplete starts the autocomplete process
func (b *Buffer) Autocomplete(c Completer) bool {
if !b.GetActiveCursor().CanAutocomplete() {
return b.AutocompleteCB(c, nil)
}

func (b *Buffer) AutocompleteCB(c Completer, checkCB func(*Cursor) bool) bool {
if checkCB != nil && !checkCB(b.GetActiveCursor()) {
return false
}
b.Completions, b.Suggestions = c(b)

if len(b.Completions) != len(b.Suggestions) || len(b.Completions) == 0 {
return false
}
b.CurSuggestion = -1
b.CycleAutocomplete(true)
b.CycleAutocompleteCB(true, checkCB)
return true
}

// CycleAutocomplete moves to the next suggestion
func (b *Buffer) CycleAutocomplete(forward bool) {
b.CycleAutocompleteCB(forward, nil)
}

func (b *Buffer) CycleAutocompleteCB(forward bool, checkCB func(*Cursor) bool) {
prevSuggestion := b.CurSuggestion

if forward {
Expand All @@ -56,7 +65,7 @@ func (b *Buffer) CycleAutocomplete(forward bool) {
activeWord := make([]byte, len(tmpWord))
copy(activeWord, tmpWord)
for _, c := range b.cursors {
if !c.CanAutocomplete() {
if checkCB != nil && !checkCB(c) {
continue
}

Expand Down Expand Up @@ -85,20 +94,6 @@ func (c *Cursor) autocomplete(prevSuggestion int) {
b.Replace(start, end, b.Completions[b.CurSuggestion])
}

func (c *Cursor) CanAutocomplete() bool {
if c.X == 0 {
return false
}

r := c.RuneUnder(c.X)
prev := c.RuneUnder(c.X - 1)
if !util.IsAutocomplete(prev) || util.IsWordChar(r) {
// don't autocomplete if cursor is within a word
return false
}
return true
}

// GetWord gets the most recent word separated by any separator
// (whitespace, punctuation, any non alphanumeric character)
func (c *Cursor) GetWord() ([]byte, int) {
Expand Down