Merge lines if you press delete at beginning of a line
All checks were successful
timw4mail/gilo/pipeline/head This commit looks good

This commit is contained in:
Timothy Warren 2021-04-02 16:14:19 -04:00
parent 787166df3a
commit 3c6568f8f7
3 changed files with 40 additions and 0 deletions

View File

@ -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
}

View File

@ -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

View File

@ -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
}
}