buffer.go 10 KB

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