Skip to content

Commit 0d10ce2

Browse files
committed
Adding intellisense and the start of highlighting
1 parent ee7ad5a commit 0d10ce2

File tree

11 files changed

+272
-30
lines changed

11 files changed

+272
-30
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*.dll
44
*.so
55
*.dylib
6+
editor
67

78
# Test binary, build with `go test -c`
89
*.test

buffer/buffer.go

+72-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import (
66
"github.com/nsf/termbox-go"
77
)
88

9+
type Styler interface {
10+
Style(pos int, ifg, ibg termbox.Attribute) (fg, bg termbox.Attribute)
11+
}
12+
913
type Buffer struct {
1014
GB *gapbuffer.GapBuffer
1115

@@ -14,6 +18,8 @@ type Buffer struct {
1418
LineStart int
1519

1620
Dirty bool
21+
22+
stylers []Styler
1723
}
1824

1925
func GetPos(gp *gapbuffer.GapBuffer, x, y int) int {
@@ -85,6 +91,10 @@ func (b *Buffer) SetPos(p int) {
8591
b.CurX, b.CurY = GetCur(b.GB, p)
8692
}
8793

94+
func (b *Buffer) GetCur(p int) (x, y int) {
95+
return GetCur(b.GB, p)
96+
}
97+
8898
func New(buf []rune) *Buffer {
8999
return &Buffer{GB: gapbuffer.New(buf)}
90100
}
@@ -149,7 +159,12 @@ func (b *Buffer) Render(r core.Rect) {
149159
xPos += 4
150160
continue
151161
}
152-
termbox.SetCell(r.X+xPos, r.Y+yPos, b.GB.Get(l1), termbox.ColorWhite, termbox.ColorBlack)
162+
fg, bg := termbox.ColorDefault, termbox.ColorDefault
163+
for _, styler := range b.stylers {
164+
fg, bg = styler.Style(l1, fg, bg)
165+
}
166+
167+
termbox.SetCell(r.X+xPos, r.Y+yPos, b.GB.Get(l1), fg, bg)
153168
xPos++
154169
}
155170
if !curSet {
@@ -158,6 +173,20 @@ func (b *Buffer) Render(r core.Rect) {
158173

159174
}
160175

176+
func (b *Buffer) AddStyler(s Styler) {
177+
b.stylers = append(b.stylers, s)
178+
}
179+
180+
func (b *Buffer) InsertString(str string) {
181+
curPos := GetPos(b.GB, b.CurX, b.CurY)
182+
for _, ch := range str {
183+
b.GB.Insert(curPos, ch)
184+
curPos++
185+
}
186+
b.CurX, b.CurY = GetCur(b.GB, curPos)
187+
b.Dirty = true
188+
}
189+
161190
func (b *Buffer) Handle(r core.Rect, evt termbox.Event) bool {
162191
if evt.Type == termbox.EventKey {
163192
ch := evt.Ch
@@ -168,25 +197,44 @@ func (b *Buffer) Handle(r core.Rect, evt termbox.Event) bool {
168197
curPos--
169198
}
170199
b.CurX, b.CurY = GetCur(b.GB, curPos)
200+
return true
171201
case termbox.KeyArrowRight:
172202
curPos := GetPos(b.GB, b.CurX, b.CurY)
173203
if curPos < b.GB.Len() {
174204
curPos++
175205
}
176206
b.CurX, b.CurY = GetCur(b.GB, curPos)
207+
return true
177208
case termbox.KeyArrowUp:
178209
if b.CurY > 0 {
179210
b.CurY--
180211
}
212+
return true
213+
case termbox.KeyPgup:
214+
b.CurY -= 10
215+
if b.CurY < 0 {
216+
b.CurY = 0
217+
}
218+
return true
181219
case termbox.KeyArrowDown:
182220
if b.CurY < GetHeight(b.GB) {
183221
b.CurY++
184222
}
223+
return true
224+
case termbox.KeyPgdn:
225+
b.CurY += 10
226+
h := GetHeight(b.GB)
227+
if b.CurY > h {
228+
b.CurY = h
229+
}
230+
return true
185231
case termbox.KeyEnter:
186232
ch = '\n'
187233
case termbox.KeySpace:
188234
ch = ' '
189-
case termbox.KeyBackspace:
235+
case termbox.KeyTab:
236+
ch = '\t'
237+
case termbox.KeyBackspace, termbox.KeyBackspace2:
190238
curPos := GetPos(b.GB, b.CurX, b.CurY)
191239
if curPos <= 0 {
192240
break
@@ -195,6 +243,7 @@ func (b *Buffer) Handle(r core.Rect, evt termbox.Event) bool {
195243
curPos--
196244
b.CurX, b.CurY = GetCur(b.GB, curPos)
197245
b.Dirty = true
246+
return true
198247
case termbox.KeyDelete:
199248
curPos := GetPos(b.GB, b.CurX, b.CurY)
200249
if b.GB.Len()-curPos <= 0 {
@@ -203,19 +252,37 @@ func (b *Buffer) Handle(r core.Rect, evt termbox.Event) bool {
203252
b.GB.Delete(curPos + 1)
204253
b.CurX, b.CurY = GetCur(b.GB, curPos)
205254
b.Dirty = true
255+
return true
206256
}
207257
if ch != '\x00' {
208258
curPos := GetPos(b.GB, b.CurX, b.CurY)
209259
b.GB.Insert(curPos, ch)
210260
curPos++
211261
b.CurX, b.CurY = GetCur(b.GB, curPos)
212262
b.Dirty = true
263+
return true
213264
}
214265
}
215266
if evt.Type == termbox.EventMouse && evt.Key == termbox.MouseLeft && r.CheckEvent(evt) {
216-
curPos := GetPos(b.GB, evt.MouseX-r.X, evt.MouseY+b.Scroll-r.Y)
217-
b.CurX, b.CurY = GetCur(b.GB, curPos)
218-
return true
267+
switch evt.Key {
268+
case termbox.MouseLeft:
269+
curPos := GetPos(b.GB, evt.MouseX-r.X, evt.MouseY+b.Scroll-r.Y)
270+
b.CurX, b.CurY = GetCur(b.GB, curPos)
271+
return true
272+
case termbox.MouseWheelUp:
273+
b.CurY -= 10
274+
if b.CurY < 0 {
275+
b.CurY = 0
276+
}
277+
return true
278+
case termbox.MouseWheelDown:
279+
b.CurY += 10
280+
h := GetHeight(b.GB)
281+
if b.CurY > h {
282+
b.CurY = h
283+
}
284+
return true
285+
}
219286
}
220287
return false
221288
}

core/core.go

+17-9
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,28 @@ func RenderString(x, y int, text string, fg, bg termbox.Attribute) {
1414
// └ ┴ ┬ ├ ┼ ┘
1515

1616
func Frame(r Rect, fg, bg termbox.Attribute) {
17-
for l1 := r.X + 1; l1 < r.X+r.W; l1++ {
17+
for l1 := r.X + 1; l1 < r.X+r.W-1; l1++ {
1818
termbox.SetCell(l1, r.Y, '─', fg, bg)
19-
termbox.SetCell(l1, r.Y+r.H, '─', fg, bg)
19+
termbox.SetCell(l1, r.Y+r.H-1, '─', fg, bg)
2020
}
21-
for l1 := r.Y + 1; l1 < r.Y+r.H; l1++ {
21+
for l1 := r.Y + 1; l1 < r.Y+r.H-1; l1++ {
2222
termbox.SetCell(r.X, l1, '│', fg, bg)
23-
termbox.SetCell(r.X+r.W, l1, '│', fg, bg)
23+
termbox.SetCell(r.X+r.W-1, l1, '│', fg, bg)
2424
}
2525
termbox.SetCell(r.X, r.Y, '┌', fg, bg)
26-
termbox.SetCell(r.X+r.W, r.Y, '┐', fg, bg)
27-
termbox.SetCell(r.X, r.Y+r.H, '└', fg, bg)
28-
termbox.SetCell(r.X+r.W, r.Y+r.H, '┘', fg, bg)
29-
for l1 := r.Y + 1; l1 < r.Y+r.H; l1++ {
30-
for l2 := r.X + 1; l2 < r.X+r.W; l2++ {
26+
termbox.SetCell(r.X+r.W-1, r.Y, '┐', fg, bg)
27+
termbox.SetCell(r.X, r.Y+r.H-1, '└', fg, bg)
28+
termbox.SetCell(r.X+r.W-1, r.Y+r.H-1, '┘', fg, bg)
29+
for l1 := r.Y + 1; l1 < r.Y+r.H-1; l1++ {
30+
for l2 := r.X + 1; l2 < r.X+r.W-1; l2++ {
31+
termbox.SetCell(l2, l1, ' ', fg, bg)
32+
}
33+
}
34+
}
35+
36+
func FrameBorderless(r Rect, fg, bg termbox.Attribute) {
37+
for l1 := r.Y; l1 < r.Y+r.H; l1++ {
38+
for l2 := r.X; l2 < r.X+r.W; l2++ {
3139
termbox.SetCell(l2, l1, ' ', fg, bg)
3240
}
3341
}

dialogs/dialog.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ type Dialog struct {
1818
func (d *Dialog) Render(r core.Rect) {
1919
r.X, r.Y, r.W, r.H = r.X+10, r.Y+(r.H/2)-1, r.W-20, 3
2020

21-
core.Frame(r, termbox.ColorWhite, termbox.ColorBlue|termbox.AttrBold)
21+
core.Frame(r, termbox.ColorWhite, termbox.ColorBlue)
2222

2323
center := r.W/2 - len(d.Message)/2
24-
core.RenderString(r.X+center, r.Y+1, d.Message, termbox.ColorWhite, termbox.ColorBlue|termbox.AttrBold)
24+
core.RenderString(r.X+center, r.Y+1, d.Message, termbox.ColorWhite, termbox.ColorBlue)
2525

2626
for i, o := range d.Options {
2727
core.RenderString(r.X+r.W/2+int(15*(float64(i)-float64(len(d.Options)-1)/2))-len(o.Name)/2, r.Y+2, o.Name, termbox.ColorWhite, termbox.ColorBlue)

dialogs/open.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ func NewOpenDialog(path string) *OpenDialog {
3535
func (d *OpenDialog) Render(r core.Rect) {
3636
r = r.Shrink(10, 10)
3737

38-
core.Frame(r, termbox.ColorWhite, termbox.ColorBlue|termbox.AttrBold)
38+
core.Frame(r, termbox.ColorWhite, termbox.ColorBlue)
3939

40-
core.RenderString(11, 11, d.path, termbox.ColorWhite, termbox.ColorBlue|termbox.AttrBold)
40+
core.RenderString(11, 11, d.path, termbox.ColorWhite, termbox.ColorBlue)
4141

4242
if d.scroll == -1 {
4343
if d.selected == -1 {
4444
core.RenderString(12, 14, "...", termbox.ColorWhite, termbox.ColorBlue)
4545
} else {
46-
core.RenderString(11, 14, "...", termbox.ColorWhite, termbox.ColorBlue|termbox.AttrBold)
46+
core.RenderString(11, 14, "...", termbox.ColorWhite, termbox.ColorBlue)
4747
}
4848
}
4949
for y, fi := range d.files {
@@ -60,7 +60,7 @@ func (d *OpenDialog) Render(r core.Rect) {
6060
if d.selected == y {
6161
core.RenderString(12, y-d.scroll+14, name, termbox.ColorWhite, termbox.ColorBlue)
6262
} else {
63-
core.RenderString(11, y-d.scroll+14, name, termbox.ColorWhite, termbox.ColorBlue|termbox.AttrBold)
63+
core.RenderString(11, y-d.scroll+14, name, termbox.ColorWhite, termbox.ColorBlue)
6464
}
6565
}
6666
core.RenderString(r.X+r.W-10, r.Y+r.H-1, "Load", termbox.ColorWhite, termbox.ColorBlue)

dialogs/save.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ func NewSaveDialog(path string) *SaveDialog {
3838
func (d *SaveDialog) Render(r core.Rect) {
3939
r = r.Shrink(10, 10)
4040

41-
core.Frame(r, termbox.ColorWhite, termbox.ColorBlue|termbox.AttrBold)
41+
core.Frame(r, termbox.ColorWhite, termbox.ColorBlue)
4242

43-
core.RenderString(r.X+1, r.Y+1, d.path, termbox.ColorWhite, termbox.ColorBlue|termbox.AttrBold)
43+
core.RenderString(r.X+1, r.Y+1, d.path, termbox.ColorWhite, termbox.ColorBlue)
4444

4545
if d.scroll == -1 {
4646
if d.selected == -1 {
4747
core.RenderString(12, 14, "...", termbox.ColorWhite, termbox.ColorBlue)
4848
} else {
49-
core.RenderString(11, 14, "...", termbox.ColorWhite, termbox.ColorBlue|termbox.AttrBold)
49+
core.RenderString(11, 14, "...", termbox.ColorWhite, termbox.ColorBlue)
5050
}
5151
}
5252
for y, fi := range d.files {
@@ -63,7 +63,7 @@ func (d *SaveDialog) Render(r core.Rect) {
6363
if d.selected == y {
6464
core.RenderString(r.X+2, r.Y+y-d.scroll+4, name, termbox.ColorWhite, termbox.ColorBlue)
6565
} else {
66-
core.RenderString(r.X+1, r.Y+y-d.scroll+4, name, termbox.ColorWhite, termbox.ColorBlue|termbox.AttrBold)
66+
core.RenderString(r.X+1, r.Y+y-d.scroll+4, name, termbox.ColorWhite, termbox.ColorBlue)
6767
}
6868
}
6969

find/find.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func (f *FindPanel) Handle(r core.Rect, evt termbox.Event) bool {
9494
if f.selected && evt.Type == termbox.EventKey {
9595
ch := evt.Ch
9696
switch evt.Key {
97-
case termbox.KeyBackspace:
97+
case termbox.KeyBackspace, termbox.KeyBackspace2:
9898
if f.curPos > 0 {
9999
f.searchString = f.searchString[:f.curPos-1] + f.searchString[f.curPos:]
100100
f.curPos--

golight/golight.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package golight
2+
3+
import (
4+
"github.com/andyleap/editor/buffer"
5+
"github.com/nsf/termbox-go"
6+
)
7+
8+
type GoLight struct {
9+
b *buffer.Buffer
10+
}
11+
12+
func New(b *buffer.Buffer) *GoLight {
13+
return &GoLight{
14+
b: b,
15+
}
16+
}
17+
18+
func (gl *GoLight) Style(pos int, ifg, ibg termbox.Attribute) (fg, bg termbox.Attribute) {
19+
switch gl.b.GB.Get(pos) {
20+
case '{', '}', '[', ']', '(', ')':
21+
return termbox.ColorRed, ibg
22+
}
23+
24+
return ifg, ibg
25+
}

0 commit comments

Comments
 (0)