-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtab.go
More file actions
146 lines (116 loc) · 3.3 KB
/
tab.go
File metadata and controls
146 lines (116 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package readline
import (
"context"
"os"
)
// TabDisplayType defines how the autocomplete suggestions display
type TabDisplayType int
const (
// TabDisplayGrid is the default. It's where the screen below the prompt is
// divided into a grid with each suggestion occupying an individual cell.
TabDisplayGrid = iota
// TabDisplayList is where suggestions are displayed as a list with a
// description. The suggestion gets highlighted but both are searchable (ctrl+f)
TabDisplayList
// TabDisplayMap is where suggestions are displayed as a list with a
// description however the description is what gets highlighted and only
// that is searchable (ctrl+f). The benefit of TabDisplayMap is when your
// autocomplete suggestions are IDs rather than human terms.
TabDisplayMap
)
func (rl *Instance) getTabCompletion() {
rl.tcOffset = 0
if rl.TabCompleter == nil {
return
}
if rl.delayedTabContext.cancel != nil {
rl.delayedTabContext.cancel()
}
rl.delayedTabContext = DelayedTabContext{rl: rl}
rl.delayedTabContext.Context, rl.delayedTabContext.cancel = context.WithCancel(context.Background())
if rl.modeViMode == vimCommand {
rl.tcr = rl.vimCommandModeSuggestions()
} else {
rl.tcr = rl.TabCompleter(rl.line.Runes(), rl.line.RunePos(), rl.delayedTabContext)
}
if rl.tcr == nil {
return
}
rl.tabMutex.Lock()
rl.tcPrefix, rl.tcSuggestions, rl.tcDescriptions, rl.tcDisplayType = rl.tcr.Prefix, rl.tcr.Suggestions, rl.tcr.Descriptions, rl.tcr.DisplayType
if len(rl.tcDescriptions) == 0 {
// probably not needed, but just in case someone doesn't initialize the
// map in their API call.
rl.tcDescriptions = make(map[string]string)
}
rl.tabMutex.Unlock()
rl.initTabCompletion()
}
func (rl *Instance) initTabCompletion() {
rl.modeTabCompletion.Store(true)
rl.autocompleteHeightAdjust()
if rl.tcDisplayType == TabDisplayGrid {
rl.initTabGrid()
} else {
rl.initTabMap()
}
}
func (rl *Instance) autocompleteHeightAdjust() {
var (
height int
err error
)
if rl.isNoTty {
height = 25
} else {
_, height, err = GetSize(int(os.Stdout.Fd()))
if err != nil {
height = 25
}
}
rl.previewAutocompleteHeight(height)
switch {
case height <= 4:
rl.MaxTabCompleterRows = 1
case height-4 <= rl.MaxTabCompleterRows:
rl.MaxTabCompleterRows = height - 4
}
}
func (rl *Instance) moveTabCompletionHighlight(x, y int) {
if rl.tcDisplayType == TabDisplayGrid {
rl.moveTabGridHighlight(x, y)
} else {
rl.moveTabMapHighlight(x, y)
}
}
func (rl *Instance) writeTabCompletionStr() string {
if !rl.modeTabCompletion.Load() {
return ""
}
hintY := int(rl.hintY.Load())
posX, posY := rl.lineWrapCellPos()
_, lineY := rl.lineWrapCellLen()
output := moveCursorDownStr(hintY + lineY - posY)
output += "\r\n" + seqClearScreenBelow
switch rl.tcDisplayType {
case TabDisplayGrid:
output += rl.writeTabGridStr()
case TabDisplayMap:
output += rl.writeTabMapStr()
case TabDisplayList:
output += rl.writeTabMapStr()
default:
output += rl.writeTabGridStr()
}
output += moveCursorUpStr(hintY + int(rl.tcUsedY.Load()) + lineY - posY)
output += "\r" + moveCursorForwardsStr(posX)
return output
}
func (rl *Instance) resetTabCompletion() {
rl.modeTabCompletion.Store(false)
rl.tcOffset = 0
rl.tcUsedY.Store(0)
rl.modeTabFind = false
rl.modeAutoFind = false
rl.tfLine = []rune{}
}