forked from google/gxui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
code_editor.go
234 lines (201 loc) · 5.91 KB
/
code_editor.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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mixins
import (
"fmt"
"github.com/google/gxui"
"github.com/google/gxui/math"
"strings"
)
type CodeEditorOuter interface {
TextBoxOuter
CreateSuggestionList() gxui.List
}
type CodeEditor struct {
TextBox
outer CodeEditorOuter
layers gxui.CodeSyntaxLayers
suggestionAdapter *SuggestionAdapter
suggestionList gxui.List
suggestionProvider gxui.CodeSuggestionProvider
tabWidth int
theme gxui.Theme
}
func (t *CodeEditor) updateSpans(edits []gxui.TextBoxEdit) {
runeCount := len(t.controller.TextRunes())
for _, l := range t.layers {
l.UpdateSpans(runeCount, edits)
}
}
func (t *CodeEditor) Init(outer CodeEditorOuter, driver gxui.Driver, theme gxui.Theme, font gxui.Font) {
t.outer = outer
t.tabWidth = 2
t.theme = theme
t.suggestionAdapter = &SuggestionAdapter{}
t.suggestionList = t.outer.CreateSuggestionList()
t.suggestionList.SetAdapter(t.suggestionAdapter)
t.TextBox.Init(outer, driver, theme, font)
t.controller.OnTextChanged(t.updateSpans)
// Interface compliance test
_ = gxui.CodeEditor(t)
}
func (t *CodeEditor) ItemSize(theme gxui.Theme) math.Size {
return math.Size{W: math.MaxSize.W, H: t.font.GlyphMaxSize().H}
}
func (t *CodeEditor) CreateSuggestionList() gxui.List {
l := t.theme.CreateList()
l.SetBackgroundBrush(gxui.DefaultBrush)
l.SetBorderPen(gxui.DefaultPen)
return l
}
func (t *CodeEditor) SyntaxLayers() gxui.CodeSyntaxLayers {
return t.layers
}
func (t *CodeEditor) SetSyntaxLayers(layers gxui.CodeSyntaxLayers) {
t.layers = layers
t.onRedrawLines.Fire()
}
func (t *CodeEditor) TabWidth() int {
return t.tabWidth
}
func (t *CodeEditor) SetTabWidth(tabWidth int) {
t.tabWidth = tabWidth
}
func (t *CodeEditor) SuggestionProvider() gxui.CodeSuggestionProvider {
return t.suggestionProvider
}
func (t *CodeEditor) SetSuggestionProvider(provider gxui.CodeSuggestionProvider) {
if t.suggestionProvider != provider {
t.suggestionProvider = provider
if t.IsSuggestionListShowing() {
t.ShowSuggestionList() // Update list
}
}
}
func (t *CodeEditor) IsSuggestionListShowing() bool {
return t.outer.Children().Find(t.suggestionList) != nil
}
func (t *CodeEditor) SortSuggestionList() {
caret := t.controller.LastCaret()
partial := t.controller.TextRange(t.controller.WordAt(caret))
t.suggestionAdapter.Sort(partial)
}
func (t *CodeEditor) ShowSuggestionList() {
if t.suggestionProvider == nil || t.IsSuggestionListShowing() {
return
}
caret := t.controller.LastCaret()
s, _ := t.controller.WordAt(caret)
suggestions := t.suggestionProvider.SuggestionsAt(s)
if len(suggestions) == 0 {
t.HideSuggestionList()
return
}
t.suggestionAdapter.SetSuggestions(suggestions)
t.SortSuggestionList()
child := t.AddChild(t.suggestionList)
// Position the suggestion list below the last caret
lineIdx := t.controller.LineIndex(caret)
// TODO: What if the last caret is not visible?
bounds := t.Size().Rect().Contract(t.Padding())
line := t.Line(lineIdx)
lineOffset := gxui.ChildToParent(math.ZeroPoint, line, t.outer)
target := line.PositionAt(caret).Add(lineOffset)
cs := t.suggestionList.DesiredSize(math.ZeroSize, bounds.Size())
t.suggestionList.Select(t.suggestionList.Adapter().ItemAt(0))
t.suggestionList.SetSize(cs)
child.Layout(cs.Rect().Offset(target).Intersect(bounds))
}
func (t *CodeEditor) HideSuggestionList() {
if t.IsSuggestionListShowing() {
t.RemoveChild(t.suggestionList)
}
}
func (t *CodeEditor) Line(idx int) TextBoxLine {
return gxui.FindControl(t.ItemControl(idx).(gxui.Parent), func(c gxui.Control) bool {
_, b := c.(TextBoxLine)
return b
}).(TextBoxLine)
}
// mixins.List overrides
func (t *CodeEditor) Click(ev gxui.MouseEvent) (consume bool) {
t.HideSuggestionList()
return t.TextBox.Click(ev)
}
func (t *CodeEditor) KeyPress(ev gxui.KeyboardEvent) (consume bool) {
switch ev.Key {
case gxui.KeyTab:
replace := true
for _, sel := range t.controller.Selections() {
s, e := sel.Range()
if t.controller.LineIndex(s) != t.controller.LineIndex(e) {
replace = false
break
}
}
switch {
case replace:
t.controller.ReplaceAll(strings.Repeat(" ", t.tabWidth))
t.controller.Deselect(false)
case ev.Modifier.Shift():
t.controller.UnindentSelection(t.tabWidth)
default:
t.controller.IndentSelection(t.tabWidth)
}
return true
case gxui.KeySpace:
if ev.Modifier.Control() {
t.ShowSuggestionList()
return
}
case gxui.KeyUp:
fallthrough
case gxui.KeyDown:
if t.IsSuggestionListShowing() {
return t.suggestionList.KeyPress(ev)
}
case gxui.KeyLeft:
t.HideSuggestionList()
case gxui.KeyRight:
t.HideSuggestionList()
case gxui.KeyEnter:
controller := t.controller
if t.IsSuggestionListShowing() {
text := t.suggestionAdapter.Suggestion(t.suggestionList.Selected()).Code()
s, e := controller.WordAt(t.controller.LastCaret())
controller.SetSelection(gxui.CreateTextSelection(s, e, false))
controller.ReplaceAll(text)
controller.Deselect(false)
t.HideSuggestionList()
} else {
t.controller.ReplaceWithNewlineKeepIndent()
}
return true
case gxui.KeyEscape:
if t.IsSuggestionListShowing() {
t.HideSuggestionList()
return true
}
}
return t.TextBox.KeyPress(ev)
}
func (t *CodeEditor) KeyStroke(ev gxui.KeyStrokeEvent) (consume bool) {
consume = t.TextBox.KeyStroke(ev)
if t.IsSuggestionListShowing() {
t.SortSuggestionList()
}
return
}
// mixins.TextBox overrides
func (t *CodeEditor) CreateLine(theme gxui.Theme, index int) (TextBoxLine, gxui.Control) {
lineNumber := theme.CreateLabel()
lineNumber.SetText(fmt.Sprintf("%.4d", index+1)) // Displayed lines start at 1
line := &CodeEditorLine{}
line.Init(line, theme, t, index)
layout := theme.CreateLinearLayout()
layout.SetDirection(gxui.LeftToRight)
layout.AddChild(lineNumber)
layout.AddChild(line)
return line, layout
}