This repository has been archived by the owner on Feb 17, 2021. It is now read-only.
forked from mdempsky/gocode
-
Notifications
You must be signed in to change notification settings - Fork 28
/
apropos.go
150 lines (137 loc) · 2.69 KB
/
apropos.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
package main
import (
"utf8"
"unicode"
"go/parser"
)
type DeclApropos struct {
Decl *Decl
Partial string
}
func utf8MoveBackwards(file []byte, cursor int) int {
for {
cursor--
if cursor <= 0 {
return 0
}
if utf8.RuneStart(file[cursor]) {
return cursor
}
}
return 0
}
func isIdent(rune int) bool {
return unicode.IsDigit(rune) || unicode.IsLetter(rune) || rune == '_'
}
func skipIdent(file []byte, cursor int) int {
for {
letter, _ := utf8.DecodeRune(file[cursor:])
if !isIdent(letter) {
return cursor
}
cursor = utf8MoveBackwards(file, cursor)
if cursor <= 0 {
return 0
}
}
return 0
}
var pairs = map[byte]byte{
')': '(',
']': '[',
}
func skipToPair(file []byte, cursor int) int {
right := file[cursor]
left := pairs[file[cursor]]
balance := 1
for balance != 0 {
cursor--
if cursor <= 0 {
return 0
}
switch file[cursor] {
case right:
balance++
case left:
balance--
}
}
return cursor
}
func findExpr(file []byte) []byte {
const (
LAST_NONE = iota
LAST_DOT
LAST_PAREN
LAST_IDENT
)
last := LAST_NONE
cursor := len(file)
cursor = utf8MoveBackwards(file, cursor)
loop:
for {
c := file[cursor]
letter, _ := utf8.DecodeRune(file[cursor:])
switch c {
case '.':
cursor = utf8MoveBackwards(file, cursor)
last = LAST_DOT
case ')', ']':
if last == LAST_IDENT {
break loop
}
cursor = utf8MoveBackwards(file, skipToPair(file, cursor))
last = LAST_PAREN
default:
if isIdent(letter) {
cursor = skipIdent(file, cursor)
last = LAST_IDENT
} else {
break loop
}
}
}
return file[cursor+1:]
}
func (c *AutoCompleteContext) deduceExpr(file []byte, partial string) *DeclApropos {
e := findExpr(file)
expr, err := parser.ParseExpr("", e)
if err != nil {
return nil
}
typedecl := exprToDecl(expr, c.current.scope)
if typedecl != nil {
return &DeclApropos{typedecl, partial}
}
return nil
}
func (c *AutoCompleteContext) deduceDecl(file []byte, cursor int) *DeclApropos {
orig := cursor
if cursor < 0 {
return nil
}
if cursor == 0 {
return &DeclApropos{nil, ""}
}
// figure out what is just before the cursor
cursor = utf8MoveBackwards(file, cursor)
if file[cursor] == '.' {
// we're '<whatever>.'
// figure out decl, Parital is ""
return c.deduceExpr(file[:cursor], "")
} else {
letter, _ := utf8.DecodeRune(file[cursor:])
if isIdent(letter) {
// we're '<whatever>.<ident>'
// parse <ident> as Partial and figure out decl
cursor = skipIdent(file, cursor)
partial := string(file[cursor+1 : orig])
if file[cursor] == '.' {
return c.deduceExpr(file[:cursor], partial)
} else {
return &DeclApropos{nil, partial}
}
}
}
return &DeclApropos{nil, ""}
}