buffer.go 5.9 KB

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