diff --git a/internal/editor/document/document.go b/internal/editor/document/document.go index f0213b7..582ce7d 100644 --- a/internal/editor/document/document.go +++ b/internal/editor/document/document.go @@ -87,6 +87,11 @@ func (d *Document) AppendRow(s string) { d.dirty = true } +func (d *Document) MergeRows(to int, from int) { + d.rows[to].appendString(d.rows[from].toString()) + d.delRow(from) +} + func (d *Document) ToString() string { buf := gilo.NewBuffer() @@ -117,3 +122,18 @@ func (d *Document) DelChar(at *gilo.Point) { func (d *Document) IsDirty() bool { return d.dirty } + +func (d *Document) delRow(at int) { + var newSlice []*Row + + // Split the array of rows at the specified index + start := d.rows[0:at] + end := d.rows[at+1 : d.RowCount()] // Skip the index in question + + // Splice it back together + newSlice = append(newSlice, start...) + newSlice = append(newSlice, end...) + + d.rows = newSlice + d.dirty = true +} diff --git a/internal/editor/document/row.go b/internal/editor/document/row.go index 33d5b95..4f56391 100644 --- a/internal/editor/document/row.go +++ b/internal/editor/document/row.go @@ -59,6 +59,14 @@ func (r *Row) insertRune(ch rune, at int) { r.update() } +func (r *Row) appendString(str string) { + for _, ch := range str { + r.chars = append(r.chars, ch) + } + + r.update() +} + func (r *Row) deleteRune(at int) { if at < 0 || at >= r.Size() { return diff --git a/internal/editor/editor.go b/internal/editor/editor.go index 1cff8cb..a3a59c1 100644 --- a/internal/editor/editor.go +++ b/internal/editor/editor.go @@ -94,11 +94,23 @@ func (e *editor) delChar() { return } + if e.cursor.X == 0 && e.cursor.Y == 0 { + return + } + if e.cursor.X > 0 { at := e.cursor at.X -= 1 e.document.DelChar(at) e.cursor.X -= 1 + } else { + // Move cursor to the current end of the previous line + e.cursor.X = e.document.Row(e.cursor.Y - 1).Size() + + // Move the contents of the current row to the previous + e.document.MergeRows(e.cursor.Y - 1, e.cursor.Y) + + e.cursor.Y -= 1 } }