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
10 changes: 0 additions & 10 deletions internal/action/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -916,16 +916,6 @@ func (h *BufPane) Autocomplete() bool {
return true
}

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

return b.Autocomplete(buffer.BufferComplete)
}

Expand Down
53 changes: 47 additions & 6 deletions internal/buffer/autocomplete.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ func (b *Buffer) GetSuggestions() {

// Autocomplete starts the autocomplete process
func (b *Buffer) Autocomplete(c Completer) bool {
if !b.GetActiveCursor().CanAutocomplete() {
return false
}
b.Completions, b.Suggestions = c(b)
if len(b.Completions) != len(b.Suggestions) || len(b.Completions) == 0 {
return false
Expand All @@ -49,23 +52,57 @@ func (b *Buffer) CycleAutocomplete(forward bool) {
b.CurSuggestion = len(b.Suggestions) - 1
}

c := b.GetActiveCursor()
// cycle autocomplete for all except active cursors

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This comment is not true anymore?

activeWord, _ := b.GetWord()
activeWord = bytes.Clone(activeWord)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Apparently bytes.Clone() was added in Go 1.20, while micro claims to support 1.19.

Maybe we should just bump the minimum required version to 1.20? @JoeKar WDYT?

for _, c := range b.cursors {
if !c.CanAutocomplete() {
continue
}

word, _ := c.GetWord()
if !bytes.Equal(word, activeWord) {
continue
}

c.autocomplete(prevSuggestion)
}

if len(b.Suggestions) > 1 {
b.HasSuggestions = true
}
}

func (c *Cursor) autocomplete(prevSuggestion int) {
b := c.buf
start := c.Loc
end := c.Loc

if prevSuggestion < len(b.Suggestions) && prevSuggestion >= 0 {
start = end.Move(-util.CharacterCountInString(b.Completions[prevSuggestion]), b)
}

b.Replace(start, end, b.Completions[b.CurSuggestion])
if len(b.Suggestions) > 1 {
b.HasSuggestions = true
}

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 (b *Buffer) GetWord() ([]byte, int) {
c := b.GetActiveCursor()
func (c *Cursor) GetWord() ([]byte, int) {
b := c.buf
l := b.LineBytes(c.Y)
l = util.SliceStart(l, c.X)

Expand All @@ -82,6 +119,10 @@ func (b *Buffer) GetWord() ([]byte, int) {
return input, c.X - util.CharacterCount(input)
}

func (b *Buffer) GetWord() ([]byte, int) {
return b.GetActiveCursor().GetWord()
}

// GetArg gets the most recent word (separated by ' ' only)
func (b *Buffer) GetArg() (string, int) {
c := b.GetActiveCursor()
Expand Down Expand Up @@ -153,7 +194,7 @@ func FileComplete(b *Buffer) ([]string, []string) {
// BufferComplete autocompletes based on previous words in the buffer
func BufferComplete(b *Buffer) ([]string, []string) {
c := b.GetActiveCursor()
input, argstart := b.GetWord()
input, argstart := c.GetWord()

if argstart == -1 {
return []string{}, []string{}
Expand Down