-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
compl.go
274 lines (247 loc) · 5.04 KB
/
compl.go
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// Copyright 2016, Gdlv Authors
package main
import (
"fmt"
"io"
"os"
"path/filepath"
"sort"
"strings"
)
var fullpathCompl []string
var pathCompl []string
var funcCompl []string
func completeLocationSetup() {
fullpathCompl = []string{}
pathCompl = []string{}
funcCompl = []string{}
for _, source := range sourcesPanel.slice {
fullpathCompl = append(fullpathCompl, source)
pathCompl = append(pathCompl, strings.Split(source, "/")...)
}
for _, name := range funcsPanel.slice {
funcCompl = append(funcCompl, name)
for _, seg0 := range strings.Split(name, "/") {
funcCompl = append(funcCompl, seg0)
funcCompl = append(funcCompl, strings.Split(seg0, ".")...)
}
}
}
func completeAny() {
buf := commandLineEditor.Buffer
if len(buf) == commandLineEditor.Cursor {
completeCommand()
}
for i := range buf {
if buf[i] == ' ' {
if commandLineEditor.Cursor <= i {
return
}
cmdstr := string(buf[:i])
for _, v := range cmds.cmds {
if v.match(cmdstr) {
if v.complete != nil {
v.complete()
}
return
}
}
}
}
}
func completeLocation() {
word := lastWord([]rune{' ', ':'})
if len(word) > 0 && word[0] == '/' {
completeWord(word, fullpathCompl)
return
}
word = lastWord([]rune{' ', ':', '/'})
if len(word) > 0 {
completeWord(word, pathCompl, funcCompl)
}
}
func lastWord(seps []rune) string {
buf := commandLineEditor.Buffer
start := commandLineEditor.Cursor
if start >= len(buf) {
start--
}
for i := start; i > 0; i-- {
for _, sep := range seps {
if buf[i] == sep {
return string(buf[i+1 : start+1])
}
}
}
return ""
}
func completeWord(word string, completionLists ...[]string) {
cm := completeMachine{word: word}
for _, completionList := range completionLists {
for _, compl := range completionList {
cm.add(compl)
}
}
cm.finish(cleins, &editorWriter{false})
}
func completeVariable() {
word := lastWord([]rune{' '})
cm := completeMachine{word: word}
completeAddVariables(&cm)
cm.finish(cleins, &editorWriter{false})
}
func completeAddVariables(cm *completeMachine) {
func() {
localsPanel.asyncLoad.mu.Lock()
defer localsPanel.asyncLoad.mu.Unlock()
if !localsPanel.asyncLoad.loaded {
return
}
for i := range localsPanel.locals {
cm.add(localsPanel.locals[i].Name)
}
}()
func() {
globalsPanel.asyncLoad.mu.Lock()
defer globalsPanel.asyncLoad.mu.Unlock()
if !globalsPanel.asyncLoad.loaded {
return
}
for i := range globalsPanel.globals {
cm.add(globalsPanel.globals[i].Name)
}
}()
}
func completeCommand() {
if cmds == nil || len(commandLineEditor.Buffer) == 0 {
return
}
cm := completeMachine{word: string(commandLineEditor.Buffer)}
for _, cmd := range cmds.cmds {
for _, alias := range cmd.aliases {
cm.add(alias)
}
}
cm.finish(cleins, &editorWriter{false})
}
func completeWindow() {
if cmds == nil || len(commandLineEditor.Buffer) == 0 {
return
}
cm := completeMachine{word: lastWord([]rune{' '})}
for _, w := range infoModes {
cm.add(strings.ToLower(w))
}
cm.finish(cleins, &editorWriter{false})
}
func completeFilesystem() {
word := expandTilde(lastWord([]rune{' '}))
dir := filepath.Dir(word)
dh, err := os.Open(dir)
if err != nil {
return
}
fis, err := dh.Readdir(-1)
dh.Close()
if err != nil {
return
}
compl := make([]string, 0, len(fis))
for _, fi := range fis {
switch n := fi.Name(); n {
case ".", "..":
default:
n := filepath.Join(dir, n)
if fi.IsDir() {
n = n + "/"
}
compl = append(compl, n)
}
}
completeWord(word, compl)
}
func expandTilde(path string) string {
if len(path) < 2 {
return path
}
if path[0] == '~' && path[1] == '/' {
homedir := os.Getenv("HOME")
if homedir != "" {
return homedir + path[1:]
}
}
return path
}
type completeMachine struct {
word string
compls []string
}
func (cm *completeMachine) add(compl string) {
if strings.HasPrefix(compl, cm.word) {
cm.compls = append(cm.compls, compl)
}
}
func cleins(s string) {
commandLineEditor.Text([]rune(s))
}
func (cm *completeMachine) finish(insert func(string), additional io.Writer) {
cm.compls = dedup(cm.compls)
switch len(cm.compls) {
case 0:
return
case 1:
insert(cm.compls[0][len(cm.word):])
default:
compl := commonPrefix(cm.compls)
insert(compl[len(cm.word):])
more := ""
if len(cm.compls) > 5 {
more = "..."
cm.compls = cm.compls[:5]
}
if additional != nil {
fmt.Fprintf(additional, "Completions: %s%s\n", strings.Join(cm.compls, ", "), more)
}
}
}
func dedup(v []string) []string {
if len(v) == 0 {
return v
}
sort.Strings(v)
dst := 0
var prev *string = nil
for src := 0; src < len(v); src++ {
if (prev == nil) || (v[src] != *prev) {
v[dst] = v[src]
dst++
}
prev = &v[dst-1]
}
return v[:dst]
}
func commonPrefix(in []string) string {
if len(in) <= 0 {
return ""
}
r := in[0]
for _, x := range in {
r = commonPrefix2(r, x)
if r == "" {
break
}
}
return r
}
func commonPrefix2(a, b string) string {
l := len(a)
if l > len(b) {
l = len(b)
}
for i := 0; i < l; i++ {
if a[i] != b[i] {
return a[:i]
}
}
return a[:l]
}