-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcompletion.go
More file actions
144 lines (125 loc) · 4.25 KB
/
completion.go
File metadata and controls
144 lines (125 loc) · 4.25 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package gvcode
import (
"image"
"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 {
// AddCompletors adds Completors to Completion. Completors should run independently and return
// candidates to Completion. A popup is also required to present the cadidates to user.
AddCompletor(completor Completor, popup CompletionPopup) error
// 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 interface {
Layout(gtx layout.Context, items []CompletionCandidate) layout.Dimensions
}
// Position is a position in the eidtor. Line/column and Runes may not
// be set at the same time depending on the use cases.
type 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
// Runes is the rune offset in the editor text of the input.
Runes int
}
type EditRange struct {
Start Position
End Position
}
type CompletionContext struct {
// The last key input.
Input string
// // Prefix is the text before the caret.
// Prefix string
// // Suffix is the text after the caret.
// Suffix string
// 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
// The position of the caret in line/column and selection range.
Position Position
}
// CompletionCandidate are results returned from Completor, to be presented
// to the user to select from.
type CompletionCandidate struct {
// Label is a short text shown to user to indicate
// what the candicate looks like.
Label string
// TextEdit is the real text with range info to be
// inserted into the editor.
TextEdit TextEdit
// A short description of the candicate.
Description string
// Kind of the candicate, for example, function,
// class, keywords etc.
Kind string
// TextFormat defines whether the insert text in a completion item
// should be interpreted as plain text or a snippet. The possible values are
// PlainText or Snippet.
TextFormat string
}
// TextEdit is the text with range info to be
// inserted into the editor, used in auto-completion.
type TextEdit struct {
NewText string
EditRange EditRange
}
func NewTextEditWithRuneOffset(text string, start, end int) TextEdit {
return TextEdit{
NewText: text,
EditRange: EditRange{
Start: Position{Runes: start},
End: Position{Runes: end},
},
}
}
func NewTextEditWithPos(text string, start Position, end Position) TextEdit {
return TextEdit{
NewText: text,
EditRange: EditRange{
Start: start,
End: end,
},
}
}
// Completor defines a interface that each of the delegated completor must implement.
type Completor interface {
Trigger() Trigger
Suggest(ctx CompletionContext) []CompletionCandidate
// FilterAndRank filters the passed in candidates using the pattern, and then
// rank the result.
FilterAndRank(pattern string, candidates []CompletionCandidate) []CompletionCandidate
}
// Trigger
type Trigger struct {
// Characters that must be present before the caret to trigger the completion.
// If it is empty, any character will trigger the completion.
Characters []string
// Trigger completion even the caret is in side of comment.
Comment bool
// Trigger completion even the caret is in side of string(quote pair).
String bool
// Special key binding triggers the completion.
KeyBinding struct {
Name key.Name
Modifiers key.Modifiers
}
}
func (tr Trigger) ActivateOnKey(evt key.Event) bool {
return tr.KeyBinding.Name == evt.Name &&
evt.Modifiers.Contain(tr.KeyBinding.Modifiers)
}