Skip to content

Commit

Permalink
Merge branch 'autocomplete-fix' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Neko-Box-Coder committed Mar 8, 2025
2 parents c93d951 + d5d9127 commit 17335e3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
14 changes: 6 additions & 8 deletions internal/action/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import (
"time"

shellquote "github.com/kballard/go-shellquote"
"github.com/micro-editor/tcell/v2"
"github.com/zyedidia/micro/v2/internal/buffer"
"github.com/zyedidia/micro/v2/internal/clipboard"
"github.com/zyedidia/micro/v2/internal/config"
"github.com/zyedidia/micro/v2/internal/display"
"github.com/zyedidia/micro/v2/internal/screen"
"github.com/zyedidia/micro/v2/internal/shell"
"github.com/zyedidia/micro/v2/internal/util"
"github.com/micro-editor/tcell/v2"
)

// ScrollUp is not an action
Expand Down Expand Up @@ -901,19 +901,17 @@ func (h *BufPane) OutdentSelection() bool {
// Autocomplete cycles the suggestions and performs autocompletion if there are suggestions
func (h *BufPane) Autocomplete() bool {
b := h.Buf
cc := buffer.AutocompleteCursorCheck(h.Cursor)
rc := buffer.AutocompleteRuneCheck(h.Cursor)

// Don't autocomplete at all if the active cursor cannot be autocomplete
if !buffer.AutocompleteCheck(h.Cursor) {
return false
}

if !b.HasSuggestions && !b.StartAutocomplete(buffer.BufferComplete) {
if !b.HasSuggestions && (!rc || !cc || !b.StartAutocomplete(buffer.BufferComplete)) {
return false
}

prevSuggestion := b.CycleAutocomplete(true)
for i := 0; i < b.NumCursors(); i++ {
if buffer.AutocompleteCheck(b.GetCursor(i)) {
if buffer.AutocompleteCursorCheck(b.GetCursor(i)) {
b.PerformSingleAutocomplete(prevSuggestion, b.GetCursor(i))
}
}
Expand All @@ -931,7 +929,7 @@ func (h *BufPane) CycleAutocompleteBack() bool {
if b.HasSuggestions {
prevSuggestion := b.CycleAutocomplete(false)
for i := 0; i < b.NumCursors(); i++ {
if buffer.AutocompleteCheck(b.GetCursor(i)) {
if buffer.AutocompleteCursorCheck(b.GetCursor(i)) {
b.PerformSingleAutocomplete(prevSuggestion, b.GetCursor(i))
}
}
Expand Down
12 changes: 10 additions & 2 deletions internal/action/infopane.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package action
import (
"bytes"

"github.com/micro-editor/tcell/v2"
"github.com/zyedidia/micro/v2/internal/buffer"
"github.com/zyedidia/micro/v2/internal/config"
"github.com/zyedidia/micro/v2/internal/display"
"github.com/zyedidia/micro/v2/internal/info"
"github.com/zyedidia/micro/v2/internal/util"
"github.com/micro-editor/tcell/v2"
)

type InfoKeyAction func(*InfoPane)
Expand Down Expand Up @@ -193,11 +193,19 @@ func (h *InfoPane) CommandComplete() {
b := h.Buf
c := b.GetActiveCursor()

cc := buffer.AutocompleteCursorCheck(c)
rc := buffer.AutocompleteRuneCheck(c)

// Cycling commands
if !buffer.AutocompleteCheck(c) {
if !b.HasSuggestions && !cc && !rc {
return
}

if b.HasSuggestions {
if !cc {
return
}

prevSuggestion := b.CycleAutocomplete(true)
b.PerformSingleAutocomplete(prevSuggestion, c)
return
Expand Down
18 changes: 11 additions & 7 deletions internal/buffer/autocomplete.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,7 @@ func (b *Buffer) GetSuggestions() {

}

func AutocompleteCheck(cursor *Cursor) bool {
if cursor.HasSelection() {
return false
}
if cursor.X == 0 {
return false
}
func AutocompleteRuneCheck(cursor *Cursor) bool {
r := cursor.RuneUnder(cursor.X)
prev := cursor.RuneUnder(cursor.X - 1)
if !util.IsAutocomplete(prev) || util.IsWordChar(r) {
Expand All @@ -39,6 +33,16 @@ func AutocompleteCheck(cursor *Cursor) bool {
return true
}

func AutocompleteCursorCheck(cursor *Cursor) bool {
if cursor.HasSelection() {
return false
}
if cursor.X == 0 {
return false
}
return true
}

func (b *Buffer) StartAutocomplete(c Completer) bool {
b.Completions, b.Suggestions = c(b)
if len(b.Completions) != len(b.Suggestions) || len(b.Completions) == 0 {
Expand Down

0 comments on commit 17335e3

Please sign in to comment.