forked from AllenDang/giu
-
Notifications
You must be signed in to change notification settings - Fork 2
/
CodeEditor.go
213 lines (170 loc) · 5.19 KB
/
CodeEditor.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
package giu
import (
"fmt"
"github.com/HACKERALERT/imgui-go"
)
// LanguageDefinition represents code editor's language definition
type LanguageDefinition byte
// language definitions:
const (
LanguageDefinitionSQL LanguageDefinition = iota
LanguageDefinitionCPP
LanguageDefinitionLua
LanguageDefinitionC
)
var _ Disposable = &codeEditorState{}
type codeEditorState struct {
editor imgui.TextEditor
}
// Dispose implements Disposable interface
func (s *codeEditorState) Dispose() {
// noop
}
// static check if code editor implements Widget interface
var _ Widget = &CodeEditorWidget{}
// CodeEditorWidget represents imgui.TextEditor
type CodeEditorWidget struct {
title string
width,
height float32
border bool
}
func CodeEditor() *CodeEditorWidget {
return &CodeEditorWidget{
title: GenAutoID("##CodeEditor"),
}
}
// ID allows to manually set editor's ID.
// It isn't necessary to use it in a normal conditions.
func (ce *CodeEditorWidget) ID(id string) *CodeEditorWidget {
ce.title = id
return ce
}
// ShowWhitespaces sets if whitespaces are shown in code editor
func (ce *CodeEditorWidget) ShowWhitespaces(s bool) *CodeEditorWidget {
ce.getState().editor.SetShowWhitespaces(s)
return ce
}
// TabSize sets editor's tab size
func (ce *CodeEditorWidget) TabSize(size int) *CodeEditorWidget {
ce.getState().editor.SetTabSize(size)
return ce
}
// LanguageDefinition sets code editor language definition
func (ce *CodeEditorWidget) LanguageDefinition(definition LanguageDefinition) *CodeEditorWidget {
s := ce.getState()
lookup := map[LanguageDefinition]func(){
LanguageDefinitionSQL: s.editor.SetLanguageDefinitionSQL,
LanguageDefinitionCPP: s.editor.SetLanguageDefinitionCPP,
LanguageDefinitionLua: s.editor.SetLanguageDefinitionLua,
LanguageDefinitionC: s.editor.SetLanguageDefinitionC,
}
setter, correctDefinition := lookup[definition]
if !correctDefinition {
panic(fmt.Sprintf("giu/CodeEditor.go: unknown language definition %d", definition))
}
setter()
return ce
}
// Text sets editor's text
func (ce *CodeEditorWidget) Text(str string) *CodeEditorWidget {
ce.getState().editor.SetText(str)
return ce
}
// ErrorMarkers sets error markers
func (ce *CodeEditorWidget) ErrorMarkers(markers imgui.ErrorMarkers) *CodeEditorWidget {
ce.getState().editor.SetErrorMarkers(markers)
return ce
}
// HandleKeyboardInputs sets if editor should handle keyboard input
func (ce *CodeEditorWidget) HandleKeyboardInputs(b bool) *CodeEditorWidget {
ce.getState().editor.SetHandleKeyboardInputs(b)
return ce
}
// Size sets editor's size
func (ce *CodeEditorWidget) Size(w, h float32) *CodeEditorWidget {
ce.width, ce.height = w, h
return ce
}
// Border sets editors borders
func (ce *CodeEditorWidget) Border(border bool) *CodeEditorWidget {
ce.border = border
return ce
}
// HasSelection returns true if some text is selected
func (ce *CodeEditorWidget) HasSelection() bool {
return ce.getState().editor.HasSelection()
}
// GetSelectedText returns selected text
func (ce *CodeEditorWidget) GetSelectedText() string {
return ce.getState().editor.GetSelectedText()
}
// GetText returns whole text from editor
func (ce *CodeEditorWidget) GetText() string {
return ce.getState().editor.GetText()
}
// GetCurrentLineText returns current line
func (ce *CodeEditorWidget) GetCurrentLineText() string {
return ce.getState().editor.GetCurrentLineText()
}
// GetCursorPos returns cursor position
func (ce *CodeEditorWidget) GetCursorPos() (x, y int) {
return ce.getState().editor.GetCursorPos()
}
// GetSelectionStart returns star pos of selection
func (ce *CodeEditorWidget) GetSelectionStart() (x, y int) {
return ce.getState().editor.GetSelectionStart()
}
// InsertText inserts the `text`
func (ce *CodeEditorWidget) InsertText(text string) {
ce.getState().editor.InsertText(text)
}
// GetWordUnderCursor returns the word under the cursor
func (ce *CodeEditorWidget) GetWordUnderCursor() string {
return ce.getState().editor.GetWordUnderCursor()
}
// SelectWordUnderCursor selects the word under cursor
func (ce *CodeEditorWidget) SelectWordUnderCursor() {
ce.getState().editor.SelectWordUnderCursor()
}
func (ce *CodeEditorWidget) IsTextChanged() bool {
return ce.getState().editor.IsTextChanged()
}
func (ce *CodeEditorWidget) GetScreenCursorPos() (x, y int) {
return ce.getState().editor.GetScreenCursorPos()
}
// Copy copies selection
func (ce *CodeEditorWidget) Copy() {
ce.getState().editor.Copy()
}
// Cut cuts selection
func (ce *CodeEditorWidget) Cut() {
ce.getState().editor.Cut()
}
// Paste does the same as Ctrl+V
func (ce *CodeEditorWidget) Paste() {
ce.getState().editor.Paste()
}
// Delete deletes the selection
func (ce *CodeEditorWidget) Delete() {
ce.getState().editor.Delete()
}
// Build implements Widget interface
func (ce *CodeEditorWidget) Build() {
s := ce.getState()
// register text in font atlas
tStr(s.editor.GetText())
// build editor
s.editor.Render(ce.title, imgui.Vec2{X: ce.width, Y: ce.height}, ce.border)
}
func (ce *CodeEditorWidget) getState() (state *codeEditorState) {
if s := Context.GetState(ce.title); s == nil {
state = &codeEditorState{
editor: imgui.NewTextEditor(),
}
Context.SetState(ce.title, state)
} else {
state = s.(*codeEditorState)
}
return state
}