-
Notifications
You must be signed in to change notification settings - Fork 0
/
complete.go
80 lines (73 loc) · 2.34 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
73
74
75
76
77
78
79
80
package giv
import (
"fmt"
"log"
"github.com/goki/gi/gi"
"github.com/goki/pi/complete"
"github.com/goki/pi/lex"
"github.com/goki/pi/pi"
)
// CompletePi uses GoPi symbols and language -- the string is a line of text
// up to point where user has typed.
// The data must be the *FileState from which the language type is obtained.
func CompletePi(data interface{}, text string, posLn, posCh int) (md complete.MatchData) {
sfs := data.(*pi.FileState)
if sfs == nil {
log.Printf("CompletePi: data is nil not FileState or is nil - can't complete\n")
return md
}
lp, err := pi.LangSupport.Props(sfs.Src.Sup)
if err != nil {
log.Printf("CompletePi: %v\n", err)
return md
}
if lp.Lang == nil {
return md
}
return lp.Lang.CompleteLine(sfs, text, lex.Pos{posLn, posCh})
}
// CompleteEditPi uses the selected completion to edit the text
func CompleteEditPi(data interface{}, text string, cursorPos int, comp complete.Completion, seed string) (ed complete.EditData) {
sfs := data.(*pi.FileState)
if sfs == nil {
log.Printf("CompleteEditPi: data is nil not FileState or is nil - can't complete\n")
return ed
}
lp, err := pi.LangSupport.Props(sfs.Src.Sup)
if err != nil {
log.Printf("CompleteEditPi: %v\n", err)
return ed
}
if lp.Lang == nil {
return ed
}
return lp.Lang.CompleteEdit(sfs, text, cursorPos, comp, seed)
}
// CompleteText does completion for text files
func CompleteText(data interface{}, text string, posLn, posCh int) (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 := gi.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 = gi.CompleteEditText(text, cursorPos, completion.Text, seed)
return ed
}