-
Notifications
You must be signed in to change notification settings - Fork 0
/
complete.go
72 lines (64 loc) · 2.12 KB
/
complete.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
package giv
import (
"fmt"
"go/token"
"log"
"github.com/goki/gi/complete"
"github.com/goki/gi/gi"
)
// CompleteGo uses github.com/mdempsky/gocode to do code completion
// gocode requires the entire file and the position of the cursor within the file
func CompleteGo(data interface{}, text string, pos token.Position) (md complete.MatchData) {
var txbuf *TextBuf
switch t := data.(type) {
case *TextView:
txbuf = t.Buf
case *TextBuf:
txbuf = t
}
if txbuf == nil {
log.Printf("complete.Complete: txbuf is nil - can't complete\n")
return md
}
md.Seed = complete.SeedGolang(text)
textbytes := make([]byte, 0, txbuf.NLines*40)
for _, lr := range txbuf.Lines {
textbytes = append(textbytes, []byte(string(lr))...)
textbytes = append(textbytes, '\n')
}
md.Matches = complete.CompleteGo(textbytes, pos)
return md
}
// CompleteGoEdit uses the selected completion to edit the text
func CompleteGoEdit(data interface{}, text string, cursorPos int, completion complete.Completion, seed string) (ed complete.EditData) {
ed = complete.EditGoCode(text, cursorPos, completion, seed)
return ed
}
// CompleteText does completion for text files
func CompleteText(data interface{}, text string, pos token.Position) (md complete.MatchData) {
err := gi.InitSpell() // text completion uses the spell code to generate completions and suggestions
if err != nil {
fmt.Printf("Could not initialize spelling model: Spelling model needed for text completion: %v", err)
return md
}
md.Seed = complete.SeedWhiteSpace(text)
if md.Seed == "" {
return md
}
result, err := complete.CompleteText(md.Seed)
if err != nil {
fmt.Printf("Error completing text: %v", err)
return md
}
possibles := complete.MatchSeedString(result, md.Seed)
for _, p := range possibles {
m := complete.Completion{Text: p, Icon: ""}
md.Matches = append(md.Matches, m)
}
return md
}
// CompleteTextEdit uses the selected completion to edit the text
func CompleteTextEdit(data interface{}, text string, cursorPos int, completion complete.Completion, seed string) (ed complete.EditData) {
ed = complete.EditText(text, cursorPos, completion.Text, seed)
return ed
}