-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor.go
More file actions
225 lines (189 loc) · 5.73 KB
/
editor.go
File metadata and controls
225 lines (189 loc) · 5.73 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
package main
import "github.com/spf13/afero"
// Settings stores settings for the editor
// Borders draws pretty borders around panes but takes up some screen space.
// OuterBorder when false turns off just the outer border.
// ShiftWidth is the number of spaces each tab will be displayed as.
// ScrollOffset is the minimum number of lines that will be visible above or below the cursor.
type Settings struct {
Borders bool
OuterBorder bool
ShiftWidth int
ScrollOffset int
}
// DefaultSettings constructs a default settings.
func DefaultSettings() Settings {
return Settings{
Borders: true,
OuterBorder: true,
ShiftWidth: 4,
ScrollOffset: 0,
}
}
// Cursor stores a position in a buffer and handles movement.
type Cursor struct {
x int
line int
buffer *Buffer
}
// Position reutrns the cursors current position.
func (cursor *Cursor) Position() (xPos int, lineNumber int) {
return cursor.x, cursor.line + 1
}
// Move repositions the cursor at the given coordinates.
func (cursor *Cursor) Move(xPos int, lineNumber int) {
cursor.x = xPos
cursor.line = lineNumber - 1
}
// DownLine returns the cursors position one line down.
func (cursor *Cursor) DownLine() (xPos int, lineNumber int) {
_, err := cursor.buffer.GetLine(cursor.line + 2)
if err != nil {
return cursor.Position()
}
return cursor.x, cursor.line + 2
}
// UpLine returns the cursors position one line up.
func (cursor *Cursor) UpLine() (xPos int, lineNumber int) {
if cursor.line == 0 {
return cursor.Position()
}
return cursor.x, cursor.line
}
// BackCharacter returns the cursors position one character back.
func (cursor *Cursor) BackCharacter() (xPos int, lineNumber int) {
if cursor.x == 0 {
return cursor.Position()
}
return cursor.x - 1, cursor.line + 1
}
// ForwardCharacter returns the cursors position one character forward.
func (cursor *Cursor) ForwardCharacter() (xPos int, lineNumber int) {
line, _ := cursor.buffer.GetLine(cursor.line + 1)
if len(line)-1 < cursor.x {
return cursor.Position()
}
return cursor.x + 1, cursor.line + 1
}
// BeginningOfLine returns the cursors position at the beginning of the line
func (cursor *Cursor) BeginningOfLine() (xPos int, lineNumber int) {
return 0, cursor.line + 1
}
// EndOfLine returns the cursors position at the end of the line
func (cursor *Cursor) EndOfLine() (xPos int, lineNumber int) {
line, _ := cursor.buffer.GetLine(cursor.line + 1)
return len(line) - 1, cursor.line + 1
}
// Pane represents a 'Window' in the editor. It has a Buffer.
type Pane struct {
buffer *Buffer
cursors map[*Buffer]*Cursor
topLine int
}
// NewPane constructs and initilizes a NewPane
func NewPane() Pane {
return Pane{
buffer: nil,
cursors: make(map[*Buffer]*Cursor),
topLine: 1,
}
}
// Cursor returns the Cursor into the Panes current buffer.
func (pane *Pane) Cursor() *Cursor {
return pane.cursors[pane.Buffer()]
}
// Buffer returns the Buffer of the Pane
func (pane *Pane) Buffer() *Buffer {
return pane.buffer
}
// SetBuffer binds a Buffer to the Pane and creates a Cursor if needed.
func (pane *Pane) SetBuffer(buffer *Buffer) {
pane.buffer = buffer
if pane.Cursor() == nil {
pane.cursors[pane.buffer] = &Cursor{buffer: pane.buffer}
}
}
// TopLine returns the line number of the first line visible at the top of the Pane.
func (pane *Pane) TopLine() int {
return pane.topLine
}
// SetTopLine sets the line number of the first line visiable at the top of the Pane.
func (pane *Pane) SetTopLine(topLine int) {
pane.topLine = topLine
}
// Editor is the core of Jkl. Maintains buffers, panes and manipluates them.
type Editor struct {
fs afero.Fs
currentPane *Pane
buffers []*Buffer
panes []*Pane
settings Settings
}
// New constructs a new editor.
func NewEditor(filesystem afero.Fs) Editor {
pane := NewPane()
return Editor{
fs: filesystem,
currentPane: &pane,
settings: DefaultSettings(),
}
}
// SetFS sets the filesystem handler of the Editor.
func (editor *Editor) SetFS(fs afero.Fs) {
editor.fs = fs
}
// Settings returns the settings.
func (editor *Editor) Settings() *Settings {
return &editor.settings
}
// OpenFiles opens a list of files into buffers and sets the current buffer to the first of the new buffers.
func (editor *Editor) OpenFiles(filenames []string) {
for i, filename := range filenames {
newBuffer := editor.openFile(filename)
buffer := editor.AddBuffer(&newBuffer)
if i == 0 {
editor.CurrentPane().SetBuffer(buffer)
}
}
}
// openFile reads a file, loads it into a new buffer and adds it to the list of buffers.
func (editor *Editor) openFile(filename string) Buffer {
buffer := NewBuffer()
buffer.SetFilename(filename)
if data, err := editor.fs.Open(filename); err == nil {
buffer.ReadData(data)
data.Close()
} else {
buffer.SetData([]byte{})
}
return buffer
}
// OpenFile opens a file and sets it to the current buffer.
func (editor *Editor) OpenFile(filename string) {
editor.OpenFiles([]string{filename})
}
// AddBuffer adds a buffer to the list of buffers.
func (editor *Editor) AddBuffer(buffer *Buffer) *Buffer {
editor.buffers = append(editor.buffers, buffer)
return editor.LastBuffer()
}
// LastBuffer returns a pointer to the last buffer in the list of buffers.
func (editor *Editor) LastBuffer() *Buffer {
return editor.buffers[len(editor.buffers)-1]
}
// CurrentPane returns the current pane.
func (editor *Editor) CurrentPane() *Pane {
return editor.currentPane
}
// SetCurrentPane sets the currently visible pane.
func (editor *Editor) SetCurrentPane(pane *Pane) {
editor.currentPane = pane
}
// Buffers returns a slice containing the buffers.
func (editor *Editor) Buffers() []*Buffer {
return editor.buffers
}
// Panes returns a slice containing the panes.
func (editor *Editor) Panes() []*Pane {
return editor.panes
}