|
@@ -1,15 +1,15 @@
|
|
|
package buffer
|
|
package buffer
|
|
|
|
|
|
|
|
import (
|
|
import (
|
|
|
- "unicode/utf8"
|
|
|
|
|
"slices"
|
|
"slices"
|
|
|
|
|
+ "unicode/utf8"
|
|
|
|
|
|
|
|
"github.com/zyedidia/rope"
|
|
"github.com/zyedidia/rope"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
type Buffer struct {
|
|
type Buffer struct {
|
|
|
Rope *rope.Node
|
|
Rope *rope.Node
|
|
|
- CM CursorManager
|
|
|
|
|
|
|
+ CM CursorManager
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (buf *Buffer) ensureRope() {
|
|
func (buf *Buffer) ensureRope() {
|
|
@@ -93,7 +93,7 @@ func (buf *Buffer) MoveHoriz(dir int) {
|
|
|
|
|
|
|
|
for i := range buf.CM.Cursors {
|
|
for i := range buf.CM.Cursors {
|
|
|
cur := &buf.CM.Cursors[i]
|
|
cur := &buf.CM.Cursors[i]
|
|
|
- if cur.Offset + dir < 0 || cur.Offset + dir > buf.Rope.Len() {
|
|
|
|
|
|
|
+ if cur.Offset+dir < 0 || cur.Offset+dir > buf.Rope.Len() {
|
|
|
continue
|
|
continue
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -138,7 +138,7 @@ func (buf *Buffer) MoveVert(dir int) {
|
|
|
func (buf *Buffer) AddCursorVert(dir int) {
|
|
func (buf *Buffer) AddCursorVert(dir int) {
|
|
|
buf.ensureRope()
|
|
buf.ensureRope()
|
|
|
|
|
|
|
|
- var newCursors []Cursor
|
|
|
|
|
|
|
+ var newCursors []Cursor
|
|
|
|
|
|
|
|
for i := range buf.CM.Cursors {
|
|
for i := range buf.CM.Cursors {
|
|
|
cur := &buf.CM.Cursors[i]
|
|
cur := &buf.CM.Cursors[i]
|
|
@@ -164,7 +164,7 @@ func (buf *Buffer) AddCursorVert(dir int) {
|
|
|
|
|
|
|
|
newCursors = append(newCursors, Cursor{
|
|
newCursors = append(newCursors, Cursor{
|
|
|
Offset: lineStart + goal,
|
|
Offset: lineStart + goal,
|
|
|
- Goal: goal,
|
|
|
|
|
|
|
+ Goal: goal,
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -176,7 +176,7 @@ func (buf *Buffer) AddCursorVert(dir int) {
|
|
|
func (buf *Buffer) ClearCursors() {
|
|
func (buf *Buffer) ClearCursors() {
|
|
|
primaryCursor := &Cursor{
|
|
primaryCursor := &Cursor{
|
|
|
Offset: buf.CM.Cursors[buf.CM.PrimaryIdx].Offset,
|
|
Offset: buf.CM.Cursors[buf.CM.PrimaryIdx].Offset,
|
|
|
- Goal: buf.CM.Cursors[buf.CM.PrimaryIdx].Goal,
|
|
|
|
|
|
|
+ Goal: buf.CM.Cursors[buf.CM.PrimaryIdx].Goal,
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
buf.CM.Cursors = buf.CM.Cursors[:0]
|
|
buf.CM.Cursors = buf.CM.Cursors[:0]
|
|
@@ -189,43 +189,43 @@ func LineCount(r *rope.Node) int {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func LineCol(r *rope.Node, offset int) (line, col int) {
|
|
func LineCol(r *rope.Node, offset int) (line, col int) {
|
|
|
- if offset < 0 {
|
|
|
|
|
- offset = 0
|
|
|
|
|
- }
|
|
|
|
|
- if offset > r.Len() {
|
|
|
|
|
- offset = r.Len()
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- line = r.Count(0, offset, []byte{'\n'})
|
|
|
|
|
-
|
|
|
|
|
- lineStart := 0
|
|
|
|
|
- for i := offset - 1; i >= 0; i-- {
|
|
|
|
|
- if r.At(i) == '\n' {
|
|
|
|
|
- lineStart = i + 1
|
|
|
|
|
- break
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- col = offset - lineStart
|
|
|
|
|
- return
|
|
|
|
|
|
|
+ if offset < 0 {
|
|
|
|
|
+ offset = 0
|
|
|
|
|
+ }
|
|
|
|
|
+ if offset > r.Len() {
|
|
|
|
|
+ offset = r.Len()
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ line = r.Count(0, offset, []byte{'\n'})
|
|
|
|
|
+
|
|
|
|
|
+ lineStart := 0
|
|
|
|
|
+ for i := offset - 1; i >= 0; i-- {
|
|
|
|
|
+ if r.At(i) == '\n' {
|
|
|
|
|
+ lineStart = i + 1
|
|
|
|
|
+ break
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ col = offset - lineStart
|
|
|
|
|
+ return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func OffsetForLine(r *rope.Node, targetLine int) int {
|
|
func OffsetForLine(r *rope.Node, targetLine int) int {
|
|
|
- if targetLine <= 0 {
|
|
|
|
|
- return 0
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- line := 0
|
|
|
|
|
- for i := 0; i < r.Len(); i++ {
|
|
|
|
|
- if r.At(i) == '\n' {
|
|
|
|
|
- line++
|
|
|
|
|
- if line == targetLine {
|
|
|
|
|
- return i + 1
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return r.Len()
|
|
|
|
|
|
|
+ if targetLine <= 0 {
|
|
|
|
|
+ return 0
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ line := 0
|
|
|
|
|
+ for i := 0; i < r.Len(); i++ {
|
|
|
|
|
+ if r.At(i) == '\n' {
|
|
|
|
|
+ line++
|
|
|
|
|
+ if line == targetLine {
|
|
|
|
|
+ return i + 1
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return r.Len()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (buf Buffer) String() string {
|
|
func (buf Buffer) String() string {
|