Skip to content

Commit 5967646

Browse files
Extracting autocomplete functions...
Extracting autocomplete functions, Adding autocomplete checks to each cursor
1 parent fa02380 commit 5967646

File tree

3 files changed

+71
-38
lines changed

3 files changed

+71
-38
lines changed

internal/action/actions.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -850,25 +850,23 @@ func (h *BufPane) OutdentSelection() bool {
850850
func (h *BufPane) Autocomplete() bool {
851851
b := h.Buf
852852

853-
if h.Cursor.HasSelection() {
853+
// Don't autocomplete at all if the active cursor cannot be autocomplete
854+
if !buffer.AutocompleteCheck(h.Cursor) {
854855
return false
855856
}
856857

857-
if h.Cursor.X == 0 {
858-
return false
859-
}
860-
r := h.Cursor.RuneUnder(h.Cursor.X)
861-
prev := h.Cursor.RuneUnder(h.Cursor.X - 1)
862-
if !util.IsAutocomplete(prev) || util.IsWordChar(r) {
863-
// don't autocomplete if cursor is within a word
858+
if !b.HasSuggestions && !b.StartAutocomplete(buffer.BufferComplete) {
864859
return false
865860
}
866861

867-
if b.HasSuggestions {
868-
b.CycleAutocomplete(true)
869-
return true
862+
prevSuggestion := b.CycleAutocomplete(true)
863+
for i := 0; i < b.NumCursors(); i++ {
864+
if buffer.AutocompleteCheck(b.GetCursor(i)) {
865+
b.PerformSingleAutocomplete(prevSuggestion, b.GetCursor(i))
866+
}
870867
}
871-
return b.Autocomplete(buffer.BufferComplete)
868+
869+
return true
872870
}
873871

874872
// CycleAutocompleteBack cycles back in the autocomplete suggestion list
@@ -877,8 +875,14 @@ func (h *BufPane) CycleAutocompleteBack() bool {
877875
return false
878876
}
879877

880-
if h.Buf.HasSuggestions {
881-
h.Buf.CycleAutocomplete(false)
878+
b := h.Buf
879+
if b.HasSuggestions {
880+
prevSuggestion := b.CycleAutocomplete(false)
881+
for i := 0; i < b.NumCursors(); i++ {
882+
if buffer.AutocompleteCheck(b.GetCursor(i)) {
883+
b.PerformSingleAutocomplete(prevSuggestion, b.GetCursor(i))
884+
}
885+
}
882886
return true
883887
}
884888
return false

internal/action/infopane.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -191,30 +191,44 @@ func (h *InfoPane) HistorySearchDown() {
191191
// Autocomplete begins autocompletion
192192
func (h *InfoPane) CommandComplete() {
193193
b := h.Buf
194+
c := b.GetActiveCursor()
195+
196+
// Cycling commands
197+
if !buffer.AutocompleteCheck(c) {
198+
return
199+
}
194200
if b.HasSuggestions {
195-
b.CycleAutocomplete(true)
201+
prevSuggestion := b.CycleAutocomplete(true)
202+
b.PerformSingleAutocomplete(prevSuggestion, c)
196203
return
197204
}
198205

199-
c := b.GetActiveCursor()
206+
// Otherwise start autocomplete
200207
l := b.LineBytes(0)
201208
l = util.SliceStart(l, c.X)
202-
203209
args := bytes.Split(l, []byte{' '})
204210
cmd := string(args[0])
205211

212+
var completer buffer.Completer = nil
213+
206214
if h.PromptType == "Command" {
207215
if len(args) == 1 {
208-
b.Autocomplete(CommandComplete)
216+
completer = CommandComplete
209217
} else if action, ok := commands[cmd]; ok {
210-
if action.completer != nil {
211-
b.Autocomplete(action.completer)
212-
}
218+
completer = action.completer
213219
}
214220
} else {
215221
// by default use filename autocompletion
216-
b.Autocomplete(buffer.FileComplete)
222+
completer = buffer.FileComplete
223+
}
224+
if completer == nil {
225+
return
226+
}
227+
if !b.StartAutocomplete(completer) {
228+
return
217229
}
230+
prevSuggestion := b.CycleAutocomplete(true)
231+
b.PerformSingleAutocomplete(prevSuggestion, c)
218232
}
219233

220234
// ExecuteCommand completes the prompt

internal/buffer/autocomplete.go

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,33 @@ func (b *Buffer) GetSuggestions() {
2323

2424
}
2525

26-
// Autocomplete starts the autocomplete process
27-
func (b *Buffer) Autocomplete(c Completer) bool {
26+
func AutocompleteCheck(cursor *Cursor) bool {
27+
if cursor.HasSelection() {
28+
return false
29+
}
30+
if cursor.X == 0 {
31+
return false
32+
}
33+
r := cursor.RuneUnder(cursor.X)
34+
prev := cursor.RuneUnder(cursor.X - 1)
35+
if !util.IsAutocomplete(prev) || util.IsWordChar(r) {
36+
// don't autocomplete if cursor is within a word
37+
return false
38+
}
39+
return true
40+
}
41+
42+
func (b *Buffer) StartAutocomplete(c Completer) bool {
2843
b.Completions, b.Suggestions = c(b)
2944
if len(b.Completions) != len(b.Suggestions) || len(b.Completions) == 0 {
3045
return false
3146
}
3247
b.CurSuggestion = -1
33-
b.CycleAutocomplete(true)
3448
return true
3549
}
3650

37-
// CycleAutocomplete moves to the next suggestion
38-
func (b *Buffer) CycleAutocomplete(forward bool) {
51+
// CycleAutocomplete moves to the next suggestion and return the previous suggestion
52+
func (b *Buffer) CycleAutocomplete(forward bool) int {
3953
prevSuggestion := b.CurSuggestion
4054

4155
if forward {
@@ -49,24 +63,25 @@ func (b *Buffer) CycleAutocomplete(forward bool) {
4963
b.CurSuggestion = len(b.Suggestions) - 1
5064
}
5165

52-
b.performAutoComplete(prevSuggestion)
53-
5466
if len(b.Suggestions) > 1 {
5567
b.HasSuggestions = true
5668
}
69+
70+
return prevSuggestion
5771
}
5872

59-
func (b *Buffer) performAutoComplete(prevSuggestion int) {
60-
for _, cur := range b.cursors {
61-
curLoc := cur.Loc
62-
curStart := curLoc
63-
curEnd := curLoc
73+
func (b *Buffer) PerformSingleAutocomplete(prevSuggestion int, cursor *Cursor) {
74+
curLoc := cursor.Loc
75+
curStart := curLoc
76+
curEnd := curLoc
6477

65-
if prevSuggestion < len(b.Suggestions) && prevSuggestion >= 0 {
66-
curStart = curEnd.Move(-util.CharacterCountInString(b.Completions[prevSuggestion]), b)
67-
}
68-
b.Replace(curStart, curEnd, b.Completions[b.CurSuggestion])
78+
if prevSuggestion < len(b.Suggestions) && prevSuggestion >= 0 {
79+
curStart = curEnd.Move(-util.CharacterCountInString(b.Completions[prevSuggestion]), b)
6980
}
81+
82+
hasSuggestions := b.HasSuggestions
83+
b.Replace(curStart, curEnd, b.Completions[b.CurSuggestion])
84+
b.HasSuggestions = hasSuggestions
7085
}
7186

7287
// GetWord gets the most recent word separated by any separator

0 commit comments

Comments
 (0)