-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy patheditor.go
More file actions
347 lines (293 loc) · 7.75 KB
/
Copy patheditor.go
File metadata and controls
347 lines (293 loc) · 7.75 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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package wig
import (
"os"
"path"
"path/filepath"
"strings"
"github.com/gdamore/tcell/v2"
)
type EditorConfig struct {
Theme string
ShowLineNumbers bool
RelativeLineNumbers bool
CurrentLineAbsolute bool // If true, shows absolute line number on current line when relative is on
FormatOnSave bool
GitStatusView string // "full" or "split"
GitBlameView string // "full" or "split"
IndentGuides bool
FloatingWindowWidth float64
FloatingWindowHeight float64
}
type View interface {
SetContent(x, y int, str string, st tcell.Style)
Size() (width, height int)
Resize(x, y, width, height int)
}
type RenderPlane int
const (
PlaneWin RenderPlane = 0
PlaneEditor RenderPlane = 1
)
type UiComponent interface {
Mode() Mode
Keymap() *KeyHandler
Render(view View)
Plane() RenderPlane
}
type Context struct {
Editor *Editor
Buf *Buffer
Win *Window
Count uint32
Char string
}
type AutocompleteFn func(Context) bool
// MarksPopupFactory allows the `ui` package to register a popup for marks
// without causing a circular import.
var MarksPopupFactory func(ctx Context, marks map[rune]Mark)
var EditorInst *Editor
type Layout int
const (
LayoutHorizontal Layout = 0
LayoutVertical Layout = 1
)
type Workspace struct {
Num int
Windows []*Window
ActiveWindow *Window
}
type Editor struct {
View View
Keys *KeyHandler
Buffers []*Buffer
UiComponents []UiComponent
ExitCh chan int
RedrawCh chan int
ScreenSyncCh chan int
Layout Layout
Yanks List[yank]
Projects ProjectManager
Message string // display in echo area
Lsp *LspManager
Events *EventsManager
AutocompleteTrigger AutocompleteFn
Snippets *SnippetsManager
Config EditorConfig
Workspaces []Workspace
ActiveWorkspace int
}
func NewEditor(
view View,
keys *KeyHandler,
) *Editor {
windows := []*Window{CreateWindow(nil)}
workspaces := make([]Workspace, 10)
workspaces[1].Windows = windows
EditorInst = &Editor{
View: view,
Keys: keys,
Buffers: make([]*Buffer, 0, 32),
Yanks: List[yank]{},
Layout: LayoutVertical,
Projects: NewProjectManager(),
ExitCh: make(chan int),
RedrawCh: make(chan int, 10),
ScreenSyncCh: make(chan int),
Events: NewEventsManager(),
Snippets: NewSnippetsManager(),
Workspaces: workspaces,
ActiveWorkspace: 1,
}
EditorInst.Lsp = NewLspManager(EditorInst)
TreeSitterHighlighterGo(EditorInst)
EditorInst.SetWindows(windows)
EditorInst.SetActiveWindow(windows[0])
return EditorInst
}
func (e *Editor) Windows() []*Window {
return e.Workspaces[e.ActiveWorkspace].Windows
}
func (e *Editor) SetWindows(w []*Window) {
e.Workspaces[e.ActiveWorkspace].Windows = w
}
func (e *Editor) ReadConfigFile() {
e.Config = EditorConfig{
Theme: "naysayer",
ShowLineNumbers: true,
RelativeLineNumbers: true,
CurrentLineAbsolute: true,
FormatOnSave: false,
GitStatusView: "full",
GitBlameView: "split",
IndentGuides: true,
FloatingWindowWidth: 0.8,
FloatingWindowHeight: 0.8,
}
}
func (e *Editor) OpenFile(path string) (*Buffer, error) {
absPath, err := filepath.Abs(path)
if err == nil {
path = absPath
}
if fbuf := e.BufferFindByFilePath(path, false); fbuf != nil {
return fbuf, nil
}
buf, err := BufferReadFile(path)
if err != nil {
e.LogError(err)
return nil, err
}
e.Buffers = append(e.Buffers, buf)
e.Lsp.DidOpen(buf)
hl := TreeSitterHighlighterInitBuffer(e, buf)
if hl != nil {
buf.Highlighter = hl
}
// Broadcast event so GitGutterManager calculates signs for newly opened file
e.Events.Broadcast(EventBufferReloaded{Buf: buf})
return buf, nil
}
func (e *Editor) NewContext() Context {
return Context{
Editor: e,
Buf: e.ActiveBuffer(),
Win: e.ActiveWindow(),
Count: 0,
}
}
// Find or create new buffer by its full file path
func (e *Editor) BufferFindByFilePath(fp string, create bool) *Buffer {
for _, b := range e.Buffers {
if b.FilePath == fp {
return b
}
}
if !create {
return nil
}
b := NewBuffer()
b.FilePath = fp
e.Buffers = append(e.Buffers, b)
return b
}
// Returns active window buffer.
// May return nil when the active workspace has no active window/buffer,
// e.g. transiently after ":q" closed the last window.
func (e *Editor) ActiveBuffer() *Buffer {
win := e.ActiveWindow()
if win == nil {
return nil
}
return win.Buffer()
}
func (e *Editor) GetActiveWorkspace() *Workspace {
return &e.Workspaces[e.ActiveWorkspace]
}
func (e *Editor) GetWorkspace(num int) *Workspace {
return &e.Workspaces[num]
}
func (e *Editor) ActiveWindow() *Window {
return e.GetActiveWorkspace().ActiveWindow
}
func (e *Editor) SetActiveWindow(w *Window) {
e.Workspaces[e.ActiveWorkspace].ActiveWindow = w
}
func (e *Editor) PushUi(c UiComponent) {
e.UiComponents = append(e.UiComponents, c)
}
func (e *Editor) PopUi() {
if len(e.UiComponents) > 0 {
e.UiComponents = e.UiComponents[:len(e.UiComponents)-1]
}
}
// PopUiComponent removes a specific UI component from the stack.
// This is required for WhichKey because commands executed from WhichKey
// can push their own UI components (Picker, CommandLine, etc.) before
// WhichKey.Close is called. Using PopUi in that case removes the newly
// pushed component instead of WhichKey, leaving WhichKey stuck on screen.
func (e *Editor) PopUiComponent(c UiComponent) {
for i := len(e.UiComponents) - 1; i >= 0; i-- {
if e.UiComponents[i] == c {
e.UiComponents = append(e.UiComponents[:i], e.UiComponents[i+1:]...)
break
}
}
}
func (e *Editor) EnsureBufferIsVisible(b *Buffer) {
for _, win := range e.Windows() {
if win.Buffer() == b {
return
}
}
if len(e.Windows()) > 1 {
e.Windows()[len(e.Windows())-1].ShowBuffer(b)
return
}
win := CreateWindow(nil)
win.buf = b
windows := e.Windows()
windows = append(windows, win)
}
func (e *Editor) HandleInput(ev *tcell.EventKey) {
var k *KeyHandler
win := e.ActiveWindow()
if win == nil {
// Workspace teardown in progress (e.g. right after ":q"): drop event.
return
}
buf := win.Buffer()
if buf == nil {
// No active buffer yet: drop event instead of nil-deref in buf.Mode().
return
}
mode := buf.Mode()
e.Message = ""
if buf.KeyHandler != nil {
k = buf.KeyHandler
} else {
k = e.Keys
}
if len(e.UiComponents) > 0 {
comp := e.UiComponents[len(e.UiComponents)-1]
k = comp.Keymap()
mode = comp.Mode()
}
k.HandleKey(e, ev, mode)
}
func (e *Editor) LogError(err error, echo ...bool) {
buf := e.BufferFindByFilePath("[Messages]", true)
buf.Append("error: " + err.Error())
if len(echo) > 0 && echo[0] == true {
e.EchoMessage(err.Error())
}
}
func (e *Editor) LogMessage(msg ...string) {
for _, m := range msg {
buf := e.BufferFindByFilePath("[Messages]", true)
buf.Append(m)
}
}
func (e *Editor) RuntimeDir(elems ...string) string {
p := []string{os.Getenv("HOME"), ".config", "wig"}
elems = append(p, elems...)
return path.Join(elems...)
}
func (e *Editor) EchoMessage(msg string) {
msg = strings.ReplaceAll(msg, "\n", " ")
buf := e.BufferFindByFilePath("[Messages]", true)
buf.Append(msg)
e.Message = msg
}
func (e *Editor) Redraw() {
e.RedrawCh <- 1
}
func (e *Editor) ScreenSync() {
e.ScreenSyncCh <- 1
}
// CmdToggleIndentGuides flips the IndentGuides config flag at runtime so
// you can show/hide virtual indent guides without restarting. The initial
// value comes from the "indent_guides" key in config.toml (default: true).
func CmdToggleIndentGuides(ctx Context) {
ctx.Editor.Config.IndentGuides = !ctx.Editor.Config.IndentGuides
ctx.Editor.Redraw()
}