| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377 |
- package buffer
- import (
- "slices"
- "unicode/utf8"
- "github.com/zyedidia/rope"
- )
- type BufferManager struct {
- Buffers []Buffer
- CurrentIdx int
- PaletteBuffer Buffer
- }
- func (bm *BufferManager) Current() *Buffer {
- return &bm.Buffers[bm.CurrentIdx]
- }
- type Buffer struct {
- Rope *rope.Node
- CM CursorManager
- Path string
- }
- func NewBuffer() Buffer {
- return Buffer{
- Rope: rope.New([]byte{}),
- CM: CursorManager{
- Cursors: []Cursor{{Offset: 0, Goal: 0}},
- PrimaryIdx: 0,
- },
- }
- }
- func (buf *Buffer) ensureRope() {
- if buf.Rope == nil {
- buf.Rope = rope.New([]byte{})
- }
- }
- func (buf *Buffer) Insert(content rune) {
- buf.ensureRope()
- buf.CM.DeduplicateAndSort()
- delta := 0
- data := []byte(string(content))
- shift := len(data)
- for i := range buf.CM.Cursors {
- cur := &buf.CM.Cursors[i]
- pos := cur.Offset + delta
- if pos < 0 {
- pos = 0
- }
- if pos > buf.Rope.Len() {
- pos = buf.Rope.Len()
- }
- buf.Rope.Insert(pos, data)
- cur.Offset = pos + shift
- _, goal := LineCol(buf.Rope, cur.Offset)
- cur.Goal = goal
- delta += shift
- }
- }
- func (buf *Buffer) Paste(content string) {
- for _, r := range content {
- buf.Insert(r)
- }
- }
- func (buf *Buffer) Delete() {
- buf.ensureRope()
- buf.CM.DeduplicateAndSort()
- delta := 0
- for i := range buf.CM.Cursors {
- cur := &buf.CM.Cursors[i]
- pos := cur.Offset + delta
- if pos <= 0 {
- cur.Offset = 0
- continue
- }
- if pos > buf.Rope.Len() {
- pos = buf.Rope.Len()
- }
- left := buf.Rope.Slice(0, pos)
- _, size := utf8.DecodeLastRune(left)
- if size <= 0 {
- size = 1
- }
- start := pos - size
- if start < 0 {
- start = 0
- }
- buf.Rope.Remove(start, pos)
- deleted := pos - start
- delta -= deleted
- cur.Offset = start
- _, goal := LineCol(buf.Rope, cur.Offset)
- cur.Goal = goal
- }
- buf.CM.DeduplicateAndSort()
- }
- func (buf *Buffer) Clear() {
- primaryCursor := &Cursor{
- Offset: 0,
- Goal: 0,
- }
- buf.CM.Cursors = buf.CM.Cursors[:0]
- buf.CM.Cursors = append(buf.CM.Cursors, *primaryCursor)
- buf.CM.PrimaryIdx = 0
- buf.Rope.Remove(0, buf.Rope.Len())
- }
- func (buf *Buffer) MoveHoriz(dir int) {
- buf.ensureRope()
- for i := range buf.CM.Cursors {
- cur := &buf.CM.Cursors[i]
- switch {
- case dir < 0:
- cur.Offset = prevRuneStart(buf.Rope, cur.Offset)
- case dir > 0:
- cur.Offset = nextRuneEnd(buf.Rope, cur.Offset)
- }
- _, goal := LineCol(buf.Rope, cur.Offset)
- cur.Goal = goal
- }
- buf.CM.DeduplicateAndSort()
- }
- func (buf *Buffer) MoveVert(dir int) {
- buf.ensureRope()
- for i := range buf.CM.Cursors {
- cur := &buf.CM.Cursors[i]
- line, _ := LineCol(buf.Rope, cur.Offset)
- targetLine := line + dir
- if targetLine < 0 || targetLine >= LineCount(buf.Rope) {
- continue
- }
- lineStart := OffsetForLine(buf.Rope, targetLine)
- lineEnd := lineContentEnd(buf.Rope, targetLine)
- lineLen := runeCount(buf.Rope, lineStart, lineEnd)
- goal := cur.Goal
- if goal > lineLen {
- goal = lineLen
- }
- cur.Offset = OffsetForLineCol(buf.Rope, targetLine, goal)
- }
- buf.CM.DeduplicateAndSort()
- }
- func (buf *Buffer) AddCursorVert(dir int) {
- buf.ensureRope()
- var newCursors []Cursor
- for i := range buf.CM.Cursors {
- cur := &buf.CM.Cursors[i]
- line, _ := LineCol(buf.Rope, cur.Offset)
- targetLine := line + dir
- if targetLine < 0 || targetLine >= LineCount(buf.Rope) {
- continue
- }
- lineStart := OffsetForLine(buf.Rope, targetLine)
- lineEnd := lineContentEnd(buf.Rope, targetLine)
- lineLen := runeCount(buf.Rope, lineStart, lineEnd)
- goal := cur.Goal
- if goal > lineLen {
- goal = lineLen
- }
- newCursors = append(newCursors, Cursor{
- Offset: OffsetForLineCol(buf.Rope, targetLine, goal),
- Goal: goal,
- })
- }
- buf.CM.Cursors = slices.Concat(buf.CM.Cursors, newCursors)
- buf.CM.DeduplicateAndSort()
- }
- func (buf *Buffer) ClearCursors() {
- primaryCursor := &Cursor{
- Offset: buf.CM.Cursors[buf.CM.PrimaryIdx].Offset,
- Goal: buf.CM.Cursors[buf.CM.PrimaryIdx].Goal,
- }
- buf.CM.Cursors = buf.CM.Cursors[:0]
- buf.CM.Cursors = append(buf.CM.Cursors, *primaryCursor)
- buf.CM.PrimaryIdx = 0
- }
- func LineCount(r *rope.Node) int {
- return r.Count(0, r.Len(), []byte{'\n'}) + 1
- }
- func LineCol(r *rope.Node, offset int) (line, col int) {
- offset = normalizeOffset(r, offset)
- 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 = runeCount(r, lineStart, offset)
- return
- }
- 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()
- }
- func OffsetForLineCol(r *rope.Node, line int, col int) int {
- if col <= 0 {
- return OffsetForLine(r, line)
- }
- start := OffsetForLine(r, line)
- end := lineContentEnd(r, line)
- i := start
- for n := 0; i < end && n < col; n++ {
- _, size := utf8.DecodeRune(r.Slice(i, end))
- if size <= 0 {
- size = 1
- }
- i += size
- }
- if i > end {
- return end
- }
- return i
- }
- func lineContentEnd(r *rope.Node, line int) int {
- nextStart := OffsetForLine(r, line+1)
- if nextStart > 0 && nextStart <= r.Len() && r.At(nextStart-1) == '\n' {
- return nextStart - 1
- }
- return nextStart
- }
- func runeCount(r *rope.Node, start, end int) int {
- if start < 0 {
- start = 0
- }
- if end < start {
- end = start
- }
- if end > r.Len() {
- end = r.Len()
- }
- return utf8.RuneCount(r.Slice(start, end))
- }
- func normalizeOffset(r *rope.Node, offset int) int {
- if offset < 0 {
- return 0
- }
- if offset > r.Len() {
- return r.Len()
- }
- for offset > 0 && offset < r.Len() && !utf8.RuneStart(r.At(offset)) {
- offset--
- }
- return offset
- }
- func prevRuneStart(r *rope.Node, offset int) int {
- offset = normalizeOffset(r, offset)
- if offset <= 0 {
- return 0
- }
- left := r.Slice(0, offset)
- _, size := utf8.DecodeLastRune(left)
- if size <= 0 {
- size = 1
- }
- start := offset - size
- if start < 0 {
- start = 0
- }
- return start
- }
- func nextRuneEnd(r *rope.Node, offset int) int {
- offset = normalizeOffset(r, offset)
- if offset >= r.Len() {
- return r.Len()
- }
- _, size := utf8.DecodeRune(r.Slice(offset, r.Len()))
- if size <= 0 {
- size = 1
- }
- end := offset + size
- if end > r.Len() {
- end = r.Len()
- }
- return end
- }
- func (buf Buffer) String() string {
- if buf.Rope == nil {
- return ""
- }
- return string(buf.Rope.Value())
- }
|