mirror of
https://github.com/zyedidia/micro.git
synced 2026-02-06 07:00:24 +09:00
Improvement: move MoveLinesUp and MoveLinesDown to Buffer
enables Undo/Redo with EventHandler, #407
This commit is contained in:
@@ -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,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -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])
|
||||
|
||||
Reference in New Issue
Block a user