-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcompletion.go
111 lines (93 loc) · 3.23 KB
/
completion.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
package gvcode
import (
"image"
"slices"
"gioui.org/io/key"
"gioui.org/layout"
)
// Completion is the main auto-completion interface for the editor. A Completion object
// schedules flow between the editor, the visual popup widget and completion algorithms(the Completor).
type Completion interface {
// Set in what conditions the completion should be activated.
SetTriggers(triggers ...Trigger)
// SetCompletors adds Completors to Completion. Completors should run independently and return
// candicates to Completion. All candicates are then re-ranked and presented to the user.
SetCompletors(completors ...Completor)
// SetPopup set the popup widget to be displayed when completion is activated.
SetPopup(popup CompletionPopup)
// OnText update the completion context. If there is no ongoing session, it should start one.
OnText(ctx CompletionContext)
// OnConfirm set a callback which is called when the user selected the candidates.
OnConfirm(idx int)
// Cancel cancels the current completion session.
Cancel()
// IsActive reports if the completion popup is visible.
IsActive() bool
// Offset returns the offset used to locate the popup when painting.
Offset() image.Point
// Layout layouts the completion selection box as popup near the caret.
Layout(gtx layout.Context) layout.Dimensions
}
type CompletionPopup func(gtx layout.Context, items []CompletionCandidate) layout.Dimensions
type CompletionContext struct {
// start new session if there is no active session.
New bool
// Input is the text to complete.
Input string
Position struct {
// Line number of the caret where the typing is happening.
Line int
// Column is the rune offset from the start of the line.
Column int
// Coordinates of the caret. Scroll off will change after we update the position,
// so we use doc view position instead of viewport position.
Coords image.Point
// Start is the start offset in the editor text of the input, measured in runes.
Start int
// End is the end offset in the editor text of the input, measured in runes.
End int
}
}
type CompletionCandidate struct {
Label string
InsertText string
Description string
Kind string
}
type Completor interface {
Suggest(ctx CompletionContext) []CompletionCandidate
}
type Trigger interface {
ImplementsTrigger()
}
// Completion works as you type.
// This is mutually exclusive with PrefixTrigger.
type AutoTrigger struct {
// The minimum length in runes of the input to trigger completion.
MinSize int
}
// Special prefix characters triggers the completion.
// This is mutually exclusive with AutoTrigger.
type PrefixTrigger struct {
// Prefix that must be present to trigger the completion.
// If it is empty, any character will trigger the completion.
Prefix string
}
// Special key binding triggers the completion.
type KeyTrigger struct {
Name key.Name
Modifiers key.Modifiers
}
func GetCompletionTrigger[T Trigger](triggers []Trigger) (T, bool) {
idx := slices.IndexFunc(triggers, func(item Trigger) bool {
_, ok := item.(T)
return ok
})
if idx < 0 {
return *new(T), false
}
return triggers[idx].(T), true
}
func (a AutoTrigger) ImplementsTrigger() {}
func (p PrefixTrigger) ImplementsTrigger() {}
func (k KeyTrigger) ImplementsTrigger() {}