buffer.go 5.6 KB

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