buffer.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. package buffer
  2. import (
  3. "slices"
  4. "unicode/utf8"
  5. "github.com/zyedidia/rope"
  6. )
  7. type BufferManager struct {
  8. Buffers []Buffer
  9. CurrentIdx int
  10. PaletteBuffer Buffer
  11. }
  12. func (bm *BufferManager) Current() *Buffer {
  13. return &bm.Buffers[bm.CurrentIdx]
  14. }
  15. type Buffer struct {
  16. Rope *rope.Node
  17. CM CursorManager
  18. Path string
  19. }
  20. func NewBuffer() Buffer {
  21. return Buffer{
  22. Rope: rope.New([]byte{}),
  23. CM: CursorManager{
  24. Cursors: []Cursor{{Offset: 0, Goal: 0}},
  25. PrimaryIdx: 0,
  26. },
  27. }
  28. }
  29. func (buf *Buffer) ensureRope() {
  30. if buf.Rope == nil {
  31. buf.Rope = rope.New([]byte{})
  32. }
  33. }
  34. func (buf *Buffer) Insert(content rune) {
  35. buf.ensureRope()
  36. buf.CM.DeduplicateAndSort()
  37. delta := 0
  38. data := []byte(string(content))
  39. shift := len(data)
  40. for i := range buf.CM.Cursors {
  41. cur := &buf.CM.Cursors[i]
  42. pos := cur.Offset + delta
  43. if pos < 0 {
  44. pos = 0
  45. }
  46. if pos > buf.Rope.Len() {
  47. pos = buf.Rope.Len()
  48. }
  49. buf.Rope.Insert(pos, data)
  50. cur.Offset = pos + shift
  51. _, goal := LineCol(buf.Rope, cur.Offset)
  52. cur.Goal = goal
  53. delta += shift
  54. }
  55. }
  56. func (buf *Buffer) Paste(content string) {
  57. for _, r := range content {
  58. buf.Insert(r)
  59. }
  60. }
  61. func (buf *Buffer) Delete() {
  62. buf.ensureRope()
  63. buf.CM.DeduplicateAndSort()
  64. delta := 0
  65. for i := range buf.CM.Cursors {
  66. cur := &buf.CM.Cursors[i]
  67. pos := cur.Offset + delta
  68. if pos <= 0 {
  69. cur.Offset = 0
  70. continue
  71. }
  72. if pos > buf.Rope.Len() {
  73. pos = buf.Rope.Len()
  74. }
  75. left := buf.Rope.Slice(0, pos)
  76. _, size := utf8.DecodeLastRune(left)
  77. if size <= 0 {
  78. size = 1
  79. }
  80. start := pos - size
  81. if start < 0 {
  82. start = 0
  83. }
  84. buf.Rope.Remove(start, pos)
  85. deleted := pos - start
  86. delta -= deleted
  87. cur.Offset = start
  88. _, goal := LineCol(buf.Rope, cur.Offset)
  89. cur.Goal = goal
  90. }
  91. buf.CM.DeduplicateAndSort()
  92. }
  93. func (buf *Buffer) Clear() {
  94. primaryCursor := &Cursor{
  95. Offset: 0,
  96. Goal: 0,
  97. }
  98. buf.CM.Cursors = buf.CM.Cursors[:0]
  99. buf.CM.Cursors = append(buf.CM.Cursors, *primaryCursor)
  100. buf.CM.PrimaryIdx = 0
  101. buf.Rope.Remove(0, buf.Rope.Len())
  102. }
  103. func (buf *Buffer) MoveHoriz(dir int) {
  104. buf.ensureRope()
  105. for i := range buf.CM.Cursors {
  106. cur := &buf.CM.Cursors[i]
  107. switch {
  108. case dir < 0:
  109. cur.Offset = prevRuneStart(buf.Rope, cur.Offset)
  110. case dir > 0:
  111. cur.Offset = nextRuneEnd(buf.Rope, cur.Offset)
  112. }
  113. _, goal := LineCol(buf.Rope, cur.Offset)
  114. cur.Goal = goal
  115. }
  116. buf.CM.DeduplicateAndSort()
  117. }
  118. func (buf *Buffer) MoveVert(dir int) {
  119. buf.ensureRope()
  120. for i := range buf.CM.Cursors {
  121. cur := &buf.CM.Cursors[i]
  122. line, _ := LineCol(buf.Rope, cur.Offset)
  123. targetLine := line + dir
  124. if targetLine < 0 || targetLine >= LineCount(buf.Rope) {
  125. continue
  126. }
  127. lineStart := OffsetForLine(buf.Rope, targetLine)
  128. lineEnd := lineContentEnd(buf.Rope, targetLine)
  129. lineLen := runeCount(buf.Rope, lineStart, lineEnd)
  130. goal := cur.Goal
  131. if goal > lineLen {
  132. goal = lineLen
  133. }
  134. cur.Offset = OffsetForLineCol(buf.Rope, targetLine, goal)
  135. }
  136. buf.CM.DeduplicateAndSort()
  137. }
  138. func (buf *Buffer) AddCursorVert(dir int) {
  139. buf.ensureRope()
  140. var newCursors []Cursor
  141. for i := range buf.CM.Cursors {
  142. cur := &buf.CM.Cursors[i]
  143. line, _ := LineCol(buf.Rope, cur.Offset)
  144. targetLine := line + dir
  145. if targetLine < 0 || targetLine >= LineCount(buf.Rope) {
  146. continue
  147. }
  148. lineStart := OffsetForLine(buf.Rope, targetLine)
  149. lineEnd := lineContentEnd(buf.Rope, targetLine)
  150. lineLen := runeCount(buf.Rope, lineStart, lineEnd)
  151. goal := cur.Goal
  152. if goal > lineLen {
  153. goal = lineLen
  154. }
  155. newCursors = append(newCursors, Cursor{
  156. Offset: OffsetForLineCol(buf.Rope, targetLine, goal),
  157. Goal: goal,
  158. })
  159. }
  160. buf.CM.Cursors = slices.Concat(buf.CM.Cursors, newCursors)
  161. buf.CM.DeduplicateAndSort()
  162. }
  163. func (buf *Buffer) ClearCursors() {
  164. primaryCursor := &Cursor{
  165. Offset: buf.CM.Cursors[buf.CM.PrimaryIdx].Offset,
  166. Goal: buf.CM.Cursors[buf.CM.PrimaryIdx].Goal,
  167. }
  168. buf.CM.Cursors = buf.CM.Cursors[:0]
  169. buf.CM.Cursors = append(buf.CM.Cursors, *primaryCursor)
  170. buf.CM.PrimaryIdx = 0
  171. }
  172. func LineCount(r *rope.Node) int {
  173. return r.Count(0, r.Len(), []byte{'\n'}) + 1
  174. }
  175. func LineCol(r *rope.Node, offset int) (line, col int) {
  176. offset = normalizeOffset(r, offset)
  177. if offset < 0 {
  178. offset = 0
  179. }
  180. if offset > r.Len() {
  181. offset = r.Len()
  182. }
  183. line = r.Count(0, offset, []byte{'\n'})
  184. lineStart := 0
  185. for i := offset - 1; i >= 0; i-- {
  186. if r.At(i) == '\n' {
  187. lineStart = i + 1
  188. break
  189. }
  190. }
  191. col = runeCount(r, lineStart, offset)
  192. return
  193. }
  194. func OffsetForLine(r *rope.Node, targetLine int) int {
  195. if targetLine <= 0 {
  196. return 0
  197. }
  198. line := 0
  199. for i := 0; i < r.Len(); i++ {
  200. if r.At(i) == '\n' {
  201. line++
  202. if line == targetLine {
  203. return i + 1
  204. }
  205. }
  206. }
  207. return r.Len()
  208. }
  209. func OffsetForLineCol(r *rope.Node, line int, col int) int {
  210. if col <= 0 {
  211. return OffsetForLine(r, line)
  212. }
  213. start := OffsetForLine(r, line)
  214. end := lineContentEnd(r, line)
  215. i := start
  216. for n := 0; i < end && n < col; n++ {
  217. _, size := utf8.DecodeRune(r.Slice(i, end))
  218. if size <= 0 {
  219. size = 1
  220. }
  221. i += size
  222. }
  223. if i > end {
  224. return end
  225. }
  226. return i
  227. }
  228. func lineContentEnd(r *rope.Node, line int) int {
  229. nextStart := OffsetForLine(r, line+1)
  230. if nextStart > 0 && nextStart <= r.Len() && r.At(nextStart-1) == '\n' {
  231. return nextStart - 1
  232. }
  233. return nextStart
  234. }
  235. func runeCount(r *rope.Node, start, end int) int {
  236. if start < 0 {
  237. start = 0
  238. }
  239. if end < start {
  240. end = start
  241. }
  242. if end > r.Len() {
  243. end = r.Len()
  244. }
  245. return utf8.RuneCount(r.Slice(start, end))
  246. }
  247. func normalizeOffset(r *rope.Node, offset int) int {
  248. if offset < 0 {
  249. return 0
  250. }
  251. if offset > r.Len() {
  252. return r.Len()
  253. }
  254. for offset > 0 && offset < r.Len() && !utf8.RuneStart(r.At(offset)) {
  255. offset--
  256. }
  257. return offset
  258. }
  259. func prevRuneStart(r *rope.Node, offset int) int {
  260. offset = normalizeOffset(r, offset)
  261. if offset <= 0 {
  262. return 0
  263. }
  264. left := r.Slice(0, offset)
  265. _, size := utf8.DecodeLastRune(left)
  266. if size <= 0 {
  267. size = 1
  268. }
  269. start := offset - size
  270. if start < 0 {
  271. start = 0
  272. }
  273. return start
  274. }
  275. func nextRuneEnd(r *rope.Node, offset int) int {
  276. offset = normalizeOffset(r, offset)
  277. if offset >= r.Len() {
  278. return r.Len()
  279. }
  280. _, size := utf8.DecodeRune(r.Slice(offset, r.Len()))
  281. if size <= 0 {
  282. size = 1
  283. }
  284. end := offset + size
  285. if end > r.Len() {
  286. end = r.Len()
  287. }
  288. return end
  289. }
  290. func (buf Buffer) String() string {
  291. if buf.Rope == nil {
  292. return ""
  293. }
  294. return string(buf.Rope.Value())
  295. }