-
Notifications
You must be signed in to change notification settings - Fork 0
/
autocomplete.go
executable file
·59 lines (44 loc) · 1.15 KB
/
autocomplete.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
package main
import (
"bytes"
"encoding/json"
"os/exec"
"strings"
)
type AutocompleteResponse struct {
Candidates []*Candidate
}
type Candidate struct {
Caption string `json:"caption"`
Snippet string `json:"snippet"`
Meta string `json:"meta"`
}
func autoComplete(fileName string, content []byte, offset string) *AutocompleteResponse {
cmd := exec.Command(gocodePath, "-f=json", "--in="+goPath+fileName, "autocomplete", goPath+fileName, "c"+offset)
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
logger.Fatal(err)
}
result := &AutocompleteResponse{}
var v []interface{}
json.Unmarshal(out.Bytes(), &v)
if len(v) == 0 {
return nil
}
candidates := v[1].([]interface{})
for _, gc := range candidates {
m := gc.(map[string]interface{})
c := &Candidate{}
c.Meta = m["class"].(string)
c.Caption = m["name"].(string)
c.Snippet = m["name"].(string)
typ := m["type"].(string)
if strings.HasPrefix(typ, c.Meta) {
c.Caption = c.Snippet + strings.TrimPrefix(typ, c.Meta)
}
result.Candidates = append(result.Candidates, c)
}
return result
}