1
0

buffer.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. package buffer
  2. import (
  3. "slices"
  4. "unicode"
  5. "unicode/utf8"
  6. "github.com/zyedidia/rope"
  7. )
  8. type BufferManager struct {
  9. Buffers []Buffer
  10. CurrentIdx int
  11. PaletteBuffer Buffer
  12. }
  13. func (bm *BufferManager) Current() *Buffer {
  14. return &bm.Buffers[bm.CurrentIdx]
  15. }
  16. type Buffer struct {
  17. Rope *rope.Node
  18. LI *LineIndex
  19. CM CursorManager
  20. Path string
  21. TopLine int
  22. }
  23. func NewBuffer() Buffer {
  24. return Buffer{
  25. Rope: rope.New([]byte{}),
  26. LI: NewLineIndex(),
  27. CM: CursorManager{
  28. Cursors: []Cursor{{Offset: 0, Goal: 0}},
  29. PrimaryIdx: 0,
  30. },
  31. }
  32. }
  33. func (buf *Buffer) ensureRope() {
  34. if buf.Rope == nil {
  35. buf.Rope = rope.New([]byte{})
  36. }
  37. if buf.LI == nil {
  38. buf.LI = NewLineIndexFromRope(buf.Rope)
  39. }
  40. }
  41. func (buf *Buffer) Insert(content string) {
  42. buf.ensureRope()
  43. buf.CM.DeduplicateAndSort()
  44. delta := 0
  45. data := []byte(content)
  46. shift := len(data)
  47. for i := range buf.CM.Cursors {
  48. cur := &buf.CM.Cursors[i]
  49. pos := cur.Offset + delta
  50. if pos < 0 {
  51. pos = 0
  52. }
  53. if pos > buf.Rope.Len() {
  54. pos = buf.Rope.Len()
  55. }
  56. buf.Rope.Insert(pos, data)
  57. buf.LI.InsertAt(pos, data)
  58. cur.Offset = pos + shift
  59. _, goal := LineCol(buf, cur.Offset)
  60. cur.Goal = goal
  61. delta += shift
  62. }
  63. }
  64. func (buf *Buffer) Delete() {
  65. buf.ensureRope()
  66. buf.CM.DeduplicateAndSort()
  67. delta := 0
  68. for i := range buf.CM.Cursors {
  69. cur := &buf.CM.Cursors[i]
  70. pos := cur.Offset + delta
  71. if pos <= 0 {
  72. cur.Offset = 0
  73. continue
  74. }
  75. if pos > buf.Rope.Len() {
  76. pos = buf.Rope.Len()
  77. }
  78. left := buf.Rope.Slice(0, pos)
  79. _, size := utf8.DecodeLastRune(left)
  80. if size <= 0 {
  81. size = 1
  82. }
  83. start := pos - size
  84. if start < 0 {
  85. start = 0
  86. }
  87. buf.Rope.Remove(start, pos)
  88. buf.LI.RemoveAt(start, pos)
  89. deleted := pos - start
  90. delta -= deleted
  91. cur.Offset = start
  92. _, goal := LineCol(buf, cur.Offset)
  93. cur.Goal = goal
  94. }
  95. buf.CM.DeduplicateAndSort()
  96. }
  97. func (buf *Buffer) DeleteLine() {
  98. buf.ensureRope()
  99. buf.CM.DeduplicateAndSort()
  100. if buf.Rope.Len() == 0 || len(buf.CM.Cursors) == 0 {
  101. return
  102. }
  103. primary := buf.CM.Cursors[buf.CM.PrimaryIdx]
  104. line, _ := LineCol(buf, primary.Offset)
  105. start := OffsetForLine(buf, line)
  106. end := buf.Rope.Len()
  107. if line+1 < LineCount(buf) {
  108. end = OffsetForLine(buf, line+1)
  109. } else if line > 0 && start > 0 && buf.Rope.At(start-1) == '\n' {
  110. start--
  111. }
  112. if end <= start {
  113. return
  114. }
  115. buf.Rope.Remove(start, end)
  116. buf.LI.RemoveAt(start, end)
  117. newLine := line
  118. if newLine >= LineCount(buf) {
  119. newLine = LineCount(buf) - 1
  120. }
  121. if newLine < 0 {
  122. newLine = 0
  123. }
  124. newOffset := OffsetForLine(buf, newLine)
  125. for i := range buf.CM.Cursors {
  126. buf.CM.Cursors[i].Offset = newOffset
  127. buf.CM.Cursors[i].Goal = 0
  128. }
  129. buf.CM.DeduplicateAndSort()
  130. }
  131. func (buf *Buffer) Clear() {
  132. buf.ensureRope()
  133. primaryCursor := &Cursor{
  134. Offset: 0,
  135. Goal: 0,
  136. }
  137. buf.CM.Cursors = buf.CM.Cursors[:0]
  138. buf.CM.Cursors = append(buf.CM.Cursors, *primaryCursor)
  139. buf.CM.PrimaryIdx = 0
  140. buf.Rope.Remove(0, buf.Rope.Len())
  141. buf.LI = NewLineIndex()
  142. }
  143. func isWordRune(r rune) bool {
  144. return unicode.IsLetter(r) || unicode.IsDigit(r) || r == '_'
  145. }
  146. func (buf *Buffer) MoveWordHoriz(dir int) {
  147. buf.ensureRope()
  148. for i := range buf.CM.Cursors {
  149. cur := &buf.CM.Cursors[i]
  150. pos := normalizeOffset(buf.Rope, cur.Offset)
  151. switch {
  152. case dir > 0:
  153. for pos < buf.Rope.Len() {
  154. r, size := utf8.DecodeRune(buf.Rope.Slice(pos, buf.Rope.Len()))
  155. if size <= 0 {
  156. size = 1
  157. }
  158. if !isWordRune(r) {
  159. break
  160. }
  161. pos += size
  162. }
  163. for pos < buf.Rope.Len() {
  164. r, size := utf8.DecodeRune(buf.Rope.Slice(pos, buf.Rope.Len()))
  165. if size <= 0 {
  166. size = 1
  167. }
  168. if isWordRune(r) {
  169. break
  170. }
  171. pos += size
  172. }
  173. case dir < 0:
  174. for pos > 0 {
  175. start := prevRuneStart(buf.Rope, pos)
  176. r, _ := utf8.DecodeRune(buf.Rope.Slice(start, pos))
  177. if isWordRune(r) {
  178. break
  179. }
  180. pos = start
  181. }
  182. for pos > 0 {
  183. start := prevRuneStart(buf.Rope, pos)
  184. r, _ := utf8.DecodeRune(buf.Rope.Slice(start, pos))
  185. if !isWordRune(r) {
  186. break
  187. }
  188. pos = start
  189. }
  190. }
  191. cur.Offset = pos
  192. _, goal := LineCol(buf, cur.Offset)
  193. cur.Goal = goal
  194. }
  195. buf.CM.DeduplicateAndSort()
  196. }
  197. func (buf *Buffer) MoveHoriz(dir int) {
  198. buf.ensureRope()
  199. for i := range buf.CM.Cursors {
  200. cur := &buf.CM.Cursors[i]
  201. switch {
  202. case dir < 0:
  203. cur.Offset = prevRuneStart(buf.Rope, cur.Offset)
  204. case dir > 0:
  205. cur.Offset = nextRuneEnd(buf.Rope, cur.Offset)
  206. }
  207. _, goal := LineCol(buf, cur.Offset)
  208. cur.Goal = goal
  209. }
  210. buf.CM.DeduplicateAndSort()
  211. }
  212. func (buf *Buffer) MoveVert(dir int) {
  213. buf.ensureRope()
  214. for i := range buf.CM.Cursors {
  215. cur := &buf.CM.Cursors[i]
  216. line, _ := LineCol(buf, cur.Offset)
  217. targetLine := line + dir
  218. if targetLine < 0 || targetLine >= LineCount(buf) {
  219. continue
  220. }
  221. lineStart := OffsetForLine(buf, targetLine)
  222. lineEnd := lineContentEnd(buf, targetLine)
  223. lineLen := runeCount(buf.Rope, lineStart, lineEnd)
  224. goal := cur.Goal
  225. if goal > lineLen {
  226. goal = lineLen
  227. }
  228. cur.Offset = OffsetForLineCol(buf, targetLine, goal)
  229. }
  230. buf.CM.DeduplicateAndSort()
  231. }
  232. func (buf *Buffer) AddCursorVert(dir int) {
  233. buf.ensureRope()
  234. var newCursors []Cursor
  235. for i := range buf.CM.Cursors {
  236. cur := &buf.CM.Cursors[i]
  237. line, _ := LineCol(buf, cur.Offset)
  238. targetLine := line + dir
  239. if targetLine < 0 || targetLine >= LineCount(buf) {
  240. continue
  241. }
  242. goal := buf.CM.Cursors[buf.CM.PrimaryIdx].Goal
  243. newCursors = append(newCursors, Cursor{
  244. Offset: OffsetForLineCol(buf, targetLine, goal),
  245. Goal: goal,
  246. })
  247. }
  248. buf.CM.Cursors = slices.Concat(buf.CM.Cursors, newCursors)
  249. buf.CM.DeduplicateAndSort()
  250. }
  251. func (buf *Buffer) ScrollToShow(line, maxHeight int) {
  252. if maxHeight <= 0 {
  253. return
  254. }
  255. if line < buf.TopLine {
  256. buf.TopLine = line
  257. } else if line >= buf.TopLine+maxHeight {
  258. buf.TopLine = line - maxHeight + 1
  259. }
  260. if buf.TopLine < 0 {
  261. buf.TopLine = 0
  262. }
  263. }
  264. func (buf *Buffer) ClearCursors() {
  265. primaryCursor := &Cursor{
  266. Offset: buf.CM.Cursors[buf.CM.PrimaryIdx].Offset,
  267. Goal: buf.CM.Cursors[buf.CM.PrimaryIdx].Goal,
  268. }
  269. buf.CM.Cursors = buf.CM.Cursors[:0]
  270. buf.CM.Cursors = append(buf.CM.Cursors, *primaryCursor)
  271. buf.CM.PrimaryIdx = 0
  272. }
  273. func LineCount(buf *Buffer) int {
  274. return buf.LI.Count()
  275. }
  276. func LineCol(buf *Buffer, offset int) (line, col int) {
  277. offset = normalizeOffset(buf.Rope, offset)
  278. if offset < 0 {
  279. offset = 0
  280. }
  281. if offset > buf.Rope.Len() {
  282. offset = buf.Rope.Len()
  283. }
  284. line = buf.LI.LineForOffset(offset)
  285. lineStart := buf.LI.OffsetForLine(line)
  286. col = runeCount(buf.Rope, lineStart, offset)
  287. return
  288. }
  289. func OffsetForLine(buf *Buffer, targetLine int) int {
  290. if targetLine <= 0 {
  291. return 0
  292. }
  293. if targetLine >= buf.LI.Count() {
  294. return buf.Rope.Len()
  295. }
  296. return buf.LI.OffsetForLine(targetLine)
  297. }
  298. func OffsetForLineCol(buf *Buffer, line int, col int) int {
  299. if col <= 0 {
  300. return OffsetForLine(buf, line)
  301. }
  302. start := OffsetForLine(buf, line)
  303. end := lineContentEnd(buf, line)
  304. i := start
  305. for n := 0; i < end && n < col; n++ {
  306. _, size := utf8.DecodeRune(buf.Rope.Slice(i, end))
  307. if size <= 0 {
  308. size = 1
  309. }
  310. i += size
  311. }
  312. if i > end {
  313. return end
  314. }
  315. return i
  316. }
  317. func LineText(buf *Buffer, line int) string {
  318. start := OffsetForLine(buf, line)
  319. end := lineContentEnd(buf, line)
  320. if end < start {
  321. end = start
  322. }
  323. return string(buf.Rope.Slice(start, end))
  324. }
  325. func lineContentEnd(buf *Buffer, line int) int {
  326. nextStart := OffsetForLine(buf, line+1)
  327. if nextStart > 0 && nextStart <= buf.Rope.Len() && buf.Rope.At(nextStart-1) == '\n' {
  328. return nextStart - 1
  329. }
  330. return nextStart
  331. }
  332. func runeCount(r *rope.Node, start, end int) int {
  333. if start < 0 {
  334. start = 0
  335. }
  336. if end < start {
  337. end = start
  338. }
  339. if end > r.Len() {
  340. end = r.Len()
  341. }
  342. return utf8.RuneCount(r.Slice(start, end))
  343. }
  344. func normalizeOffset(r *rope.Node, offset int) int {
  345. if offset < 0 {
  346. return 0
  347. }
  348. if offset > r.Len() {
  349. return r.Len()
  350. }
  351. for offset > 0 && offset < r.Len() && !utf8.RuneStart(r.At(offset)) {
  352. offset--
  353. }
  354. return offset
  355. }
  356. func prevRuneStart(r *rope.Node, offset int) int {
  357. offset = normalizeOffset(r, offset)
  358. if offset <= 0 {
  359. return 0
  360. }
  361. left := r.Slice(0, offset)
  362. _, size := utf8.DecodeLastRune(left)
  363. if size <= 0 {
  364. size = 1
  365. }
  366. start := offset - size
  367. if start < 0 {
  368. start = 0
  369. }
  370. return start
  371. }
  372. func nextRuneEnd(r *rope.Node, offset int) int {
  373. offset = normalizeOffset(r, offset)
  374. if offset >= r.Len() {
  375. return r.Len()
  376. }
  377. _, size := utf8.DecodeRune(r.Slice(offset, r.Len()))
  378. if size <= 0 {
  379. size = 1
  380. }
  381. end := offset + size
  382. if end > r.Len() {
  383. end = r.Len()
  384. }
  385. return end
  386. }
  387. func RuneAt(buf *Buffer, offset int) rune {
  388. if buf.Rope == nil || offset < 0 || offset >= buf.Rope.Len() {
  389. return ' '
  390. }
  391. r, size := utf8.DecodeRune(buf.Rope.Slice(offset, buf.Rope.Len()))
  392. if size <= 0 || r == '\n' || r == '\t' {
  393. return ' '
  394. }
  395. return r
  396. }
  397. func (buf Buffer) String() string {
  398. if buf.Rope == nil {
  399. return ""
  400. }
  401. return string(buf.Rope.Value())
  402. }