buffer.go 6.3 KB

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