buffer.go 11 KB

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