This repository was archived by the owner on Oct 2, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathcompletion.go
More file actions
177 lines (152 loc) · 5.54 KB
/
Copy pathcompletion.go
File metadata and controls
177 lines (152 loc) · 5.54 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
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
// A string that should be used when comparing this item with other items. When
// it's not provided, Label is used.
SortText string
// A string that should be used when filtering a set of completion items. When
// it's not provided, Label is used.
FilterText 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 returns the Trigger object used to detect the input can trigger a completion
// session.
Trigger() Trigger
// Suggest calls the backend completion engine to fetch possible candicates
// given the completion context. In the case of LSP, it also needs to sync the
// editor content by calling notification "textDocument/didChange", or "textDocument/didOpen",
// before calling the "textDocument/completion" method.
Suggest(ctx CompletionContext) []CompletionCandidate
// FilterAndRank filters the passed in candidates using the pattern, and then
// rank the result.
FilterAndRank(pattern string, candidates []CompletionCandidate) []CompletionCandidate
}
type CompletionTriggerKind uint8
const (
AutoTrigger CompletionTriggerKind = iota
CharTrigger
KeyTrigger
)
// TriggerPolicy controls how a completion session starts, where its filtering
// prefix begins, and when the active session should be canceled.
type TriggerPolicy interface {
CanStart(trigger Trigger, ctx CompletionContext) bool
PrefixRange(trigger Trigger, kind CompletionTriggerKind, ctx CompletionContext) EditRange
ShouldCancel(trigger Trigger, kind CompletionTriggerKind, triggerInput string, ctx CompletionContext, prefix string) bool
}
// Trigger
type Trigger struct {
// Characters that may start completion under the default policy.
Characters []string
// Policy controls completion session activation and prefix handling. When
// nil, the default completion policy is used.
Policy TriggerPolicy
// 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)
}