Allow inserting new lines
All checks were successful
timw4mail/gilo/pipeline/head This commit looks good

This commit is contained in:
Timothy Warren 2021-04-02 18:43:49 -04:00
parent 3c6568f8f7
commit 19ab06ac4f
2 changed files with 38 additions and 0 deletions

View File

@ -87,6 +87,41 @@ func (d *Document) AppendRow(s string) {
d.dirty = true
}
func (d *Document) InsertRow(at int, s string) {
if at < 0 || at > d.RowCount() {
return
}
var newRows []*Row
// Split the array of rows at the specified index
start := d.rows[0:at]
end := d.rows[at : d.RowCount()]
// Splice it back together
newRows = append(newRows, start...)
newRows = append(newRows, newRow(s))
newRows = append(newRows, end...)
d.rows = newRows
d.dirty = true
}
func (d *Document) InsertNewline(at *gilo.Point) {
if at.X == 0 {
d.InsertRow(at.Y, "")
} else {
row := d.rows[at.Y]
// Insert the characters right of the cursor to the next line
d.InsertRow(at.Y + 1, string(row.chars[at.X:row.Size()]))
// Remove the characters copied to the next line
row.chars = row.chars[0:at.X]
row.update()
}
}
func (d *Document) MergeRows(to int, from int) {
d.rows[to].appendString(d.rows[from].toString())
d.delRow(from)

View File

@ -45,6 +45,9 @@ func (e *editor) processKeypressChar(ch rune) bool {
e.Save()
case key.Enter:
e.document.InsertNewline(e.cursor)
e.cursor.Y += 1
e.cursor.X = 0
case key.Backspace, key.Ctrl('h'):
e.delChar()