-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompleter.go
More file actions
70 lines (58 loc) · 1.36 KB
/
completer.go
File metadata and controls
70 lines (58 loc) · 1.36 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
package main
import (
"strings"
"github.com/peterh/liner"
)
func newWordCompleter() liner.WordCompleter {
return func(line string, pos int) (head string, completions []string, tail string) {
head, partial, tail := splitLine(line, pos)
if partial == "" {
// match to nothing
return
}
// function auto-completes
for name := range funcMap() {
if strings.HasPrefix(name, partial) {
completions = append(completions, name)
}
}
return
}
}
func splitLine(line string, pos int) (head, partial, tail string) {
startPos := partialStartPos(line, pos, "{}()<>=| ")
endPos := partialEndPos(line, pos, "{}()<>=| ")
head = subStr(line, 0, startPos)
tail = subStr(line, endPos, len(line))
partial = subStr(line, startPos, endPos)
return
}
func partialStartPos(line string, pos int, delims string) int {
delimPos := strings.LastIndexAny(subStr(line, 0, pos), delims)
if delimPos < 0 {
return 0
}
return delimPos + 1
}
func partialEndPos(line string, pos int, delims string) int {
relativeDelimPos := strings.IndexAny(subStr(line, pos, len(line)), delims)
if relativeDelimPos < 0 {
return len(line)
}
return pos + relativeDelimPos
}
func subStr(str string, start, end int) string {
if start >= len(str) {
return ""
}
if end <= 0 {
return ""
}
if end > len(str) {
end = len(str)
}
if start < 0 {
start = 0
}
return str[start:end]
}