-
Notifications
You must be signed in to change notification settings - Fork 0
/
complete.go
120 lines (107 loc) · 3.86 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
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
package giv
import (
"fmt"
"github.com/goki/gi/gi"
"github.com/goki/gi/giv/textbuf"
"github.com/goki/pi/complete"
"github.com/goki/pi/lex"
"github.com/goki/pi/parse"
"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.Matches) {
sfs := data.(*pi.FileStates)
if sfs == nil {
// log.Printf("CompletePi: data is nil not FileStates or is nil - can't complete\n")
return md
}
lp, err := pi.LangSupport.Props(sfs.Sup)
if err != nil {
// log.Printf("CompletePi: %v\n", err)
return md
}
if lp.Lang == nil {
return md
}
// note: must have this set to ture to allow viewing of AST
// must set it in pi/parse directly -- so it is changed in the fileparse too
parse.GuiActive = true // note: this is key for debugging -- runs slower but makes the tree unique
md = lp.Lang.CompleteLine(sfs, text, lex.Pos{posLn, posCh})
return md
}
// CompleteEditPi uses the selected completion to edit the text
func CompleteEditPi(data interface{}, text string, cursorPos int, comp complete.Completion, seed string) (ed complete.Edit) {
sfs := data.(*pi.FileStates)
if sfs == nil {
// log.Printf("CompleteEditPi: data is nil not FileStates or is nil - can't complete\n")
return ed
}
lp, err := pi.LangSupport.Props(sfs.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)
}
// LookupPi 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 LookupPi(data interface{}, text string, posLn, posCh int) (ld complete.Lookup) {
sfs := data.(*pi.FileStates)
if sfs == nil {
// log.Printf("LookupPi: data is nil not FileStates or is nil - can't lookup\n")
return ld
}
lp, err := pi.LangSupport.Props(sfs.Sup)
if err != nil {
// log.Printf("LookupPi: %v\n", err)
return ld
}
if lp.Lang == nil {
return ld
}
// note: must have this set to ture to allow viewing of AST
// must set it in pi/parse directly -- so it is changed in the fileparse too
parse.GuiActive = true // note: this is key for debugging -- runs slower but makes the tree unique
ld = lp.Lang.Lookup(sfs, text, lex.Pos{posLn, posCh})
if len(ld.Text) > 0 {
TextViewDialog(nil, ld.Text, DlgOpts{Title: "Lookup: " + text})
return ld
}
if ld.Filename != "" {
txt := textbuf.FileRegionBytes(ld.Filename, ld.StLine, ld.EdLine, true, 10) // comments, 10 lines back max
prmpt := fmt.Sprintf("%v [%d:%d]", ld.Filename, ld.StLine, ld.EdLine)
TextViewDialog(nil, txt, DlgOpts{Title: "Lookup: " + text, Prompt: prmpt, Filename: ld.Filename, LineNos: true, Data: prmpt})
return ld
}
return ld
}
// CompleteText does completion for text files
func CompleteText(data interface{}, text string, posLn, posCh int) (md complete.Matches) {
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 := gi.CompleteText(md.Seed)
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.Edit) {
ed = gi.CompleteEditText(text, cursorPos, completion.Text, seed)
return ed
}