Improvement: move MoveLinesUp and MoveLinesDown to Buffer

enables Undo/Redo with EventHandler, #407
This commit is contained in:
Saeed Rasooli
2016-10-12 08:14:49 +03:30
parent e4386d9398
commit 0bf07eadcc
2 changed files with 38 additions and 25 deletions

View File

@@ -363,3 +363,41 @@ func (b *Buffer) Lines(start, end int) []string {
func (b *Buffer) Len() int {
return Count(b.String())
}
func (b *Buffer) MoveLinesUp(start int, end int) {
// 0 < start < end <= len(b.lines)
if start < 1 || start >= end || end > len(b.lines) {
return // what to do? FIXME
}
b.Insert(
Loc{0, end},
b.Line(start - 1) + "\n",
)
b.Remove(
Loc{0, start - 1},
Loc{0, start},
)
}
func (b *Buffer) MoveLinesDown(start int, end int) {
// 0 <= start < end < len(b.lines)
if start < 0 || start >= end || end >= len(b.lines) {
return // what to do? FIXME
}
b.Insert(
Loc{0, start},
b.Line(end) + "\n",
)
end += 1
rmEndLoc := Loc{0, end + 1}
if end >= len(b.lines)-1 {
rmEndLoc = Loc{
utf8.RuneCount(b.lines[end]) - 1,
end,
}
}
b.Remove(
Loc{0, end},
rmEndLoc,
)
}

View File

@@ -132,31 +132,6 @@ func (la *LineArray) DeleteByte(pos Loc) {
la.lines[pos.Y] = la.lines[pos.Y][:pos.X+copy(la.lines[pos.Y][pos.X:], la.lines[pos.Y][pos.X+1:])]
}
func (la *LineArray) MoveLinesUp(y0 int, y1 int) {
// 0 < y0 < y1 <= len(la.lines)
if y0 < 1 || y0 >= y1 || y1 > len(la.lines) {
return // what to do? FIXME
}
before := la.lines[y0-1]
for y := y0 ; y < y1 ; y ++ {
la.lines[y-1] = la.lines[y]
}
la.lines[y1-1] = before
}
func (la *LineArray) MoveLinesDown(y0 int, y1 int) {
// 0 <= y0 < y1 < len(la.lines)
if y0 < 0 || y0 >= y1 || y1 >= len(la.lines) {
return // what to do? FIXME
}
after := la.lines[y1]
for y := y1-1 ; y >= y0 ; y -- {
la.lines[y+1] = la.lines[y]
}
la.lines[y0] = after
}
// Substr returns the string representation between two locations
func (la *LineArray) Substr(start, end Loc) string {
startX := runeToByteIndex(start.X, la.lines[start.Y])